Force reset on decompression error
[linux-2.6/tcp-comp.git] / drivers / net / pppol2tp.c
blob7da0d07587ff0c973e0582f675170641ae1ad36e
1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
7 * Version: 1.0.0
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
11 * Contributors:
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
16 * License:
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;
40 * int fd;
41 * int session_fd;
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>
82 #include <linux/ip.h>
83 #include <linux/udp.h>
84 #include <linux/if_pppox.h>
85 #include <linux/if_pppol2tp.h>
86 #include <net/sock.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>
94 #include <net/dst.h>
95 #include <net/ip.h>
96 #include <net/udp.h>
97 #include <net/xfrm.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...) \
129 do { \
130 if ((_mask) & (_type)) \
131 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
132 } while(0)
134 /* Number of bytes to build transmit L2TP headers.
135 * Unfortunately the size is different depending on whether sequence numbers
136 * are enabled.
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
145 * outgoing ones.
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
154 * PPPoX socket */
155 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
156 * socket */
158 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
160 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
161 * context */
163 char name[20]; /* "sess xxxxx/yyyyy", where
164 * x=tunnel_id, y=session_id */
165 int mtu;
166 int mru;
167 int flags; /* accessed by PPPIOCGFLAGS.
168 * Unused. */
169 unsigned recv_seq:1; /* expect receive packets with
170 * sequence numbers? */
171 unsigned send_seq:1; /* send packets with sequence
172 * numbers? */
173 unsigned lns_mode:1; /* behave as LNS? LAC enables
174 * sequence numbers under
175 * control of LNS. */
176 int debug; /* bitmask of debug message
177 * categories */
178 int reorder_timeout; /* configured reorder timeout
179 * (in jiffies) */
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,
196 * hashed by id */
197 int debug; /* bitmask of debug message
198 * categories */
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 */
208 atomic_t ref_count;
211 /* Private data stored for received packets in the skb.
213 struct pppol2tp_skb_cb {
214 u16 ns;
215 u16 nr;
216 u16 has_seq;
217 u16 length;
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;
239 if (sk == NULL)
240 return NULL;
242 session = (struct pppol2tp_session *)(sk->sk_user_data);
243 if (session == NULL)
244 return NULL;
246 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
248 return session;
251 static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk)
253 struct pppol2tp_tunnel *tunnel;
255 if (sk == NULL)
256 return NULL;
258 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data);
259 if (tunnel == NULL)
260 return NULL;
262 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
264 return tunnel;
267 /* Tunnel reference counts. Incremented per session that is added to
268 * the tunnel.
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
285 * simple bitmask.
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);
308 return session;
311 read_unlock(&tunnel->hlist_lock);
313 return NULL;
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);
326 return tunnel;
329 read_unlock(&pppol2tp_tunnel_list_lock);
331 return NULL;
334 /*****************************************************************************
335 * Receive data handling
336 *****************************************************************************/
338 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
339 * number.
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++;
355 goto out;
359 __skb_queue_tail(&session->reorder_q, skb);
361 out:
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);
377 skb_orphan(skb);
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) {
385 /* Bump our Nr */
386 session->nr++;
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.
404 * Namely we need to:
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
409 * of the UDP tunnel
410 * - reset netfilter information as it doesn't apply
411 * to the inner packet either
413 secpath_reset(skb);
414 dst_release(skb->dst);
415 skb->dst = NULL;
416 nf_reset(skb);
418 po = pppox_sk(session_sock);
419 ppp_input(&po->chan, skb);
420 } else {
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++;
426 kfree_skb(skb);
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)
437 struct sk_buff *skb;
438 struct sk_buff *tmp;
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);
456 kfree_skb(skb);
457 continue;
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));
468 goto out;
471 spin_unlock(&session->reorder_q.lock);
472 pppol2tp_recv_dequeue_skb(session, skb);
473 spin_lock(&session->reorder_q.lock);
476 out:
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;
490 unsigned char *ptr, *optr;
491 u16 hdrflags;
492 u16 tunnel_id, session_id;
493 int length;
494 int offset;
496 tunnel = pppol2tp_sock_to_tunnel(sock);
497 if (tunnel == NULL)
498 goto no_tunnel;
500 /* UDP always verifies the packet length. */
501 __skb_pull(skb, sizeof(struct udphdr));
503 /* Short packet? */
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);
507 goto error;
510 /* Point to L2TP header */
511 optr = ptr = skb->data;
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))
520 goto error;
522 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
524 offset = 0;
525 do {
526 printk(" %02X", ptr[offset]);
527 } while (++offset < length);
529 printk("\n");
532 /* Get length of L2TP packet */
533 length = skb->len;
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);
539 goto error;
542 /* Skip flags */
543 ptr += 2;
545 /* If length is present, skip it */
546 if (hdrflags & L2TP_HDRFLAG_L)
547 ptr += 2;
549 /* Extract tunnel and session ID */
550 tunnel_id = ntohs(*(__be16 *) ptr);
551 ptr += 2;
552 session_id = ntohs(*(__be16 *) ptr);
553 ptr += 2;
555 /* Find the session context */
556 session = pppol2tp_session_find(tunnel, session_id);
557 if (!session) {
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);
562 goto error;
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
574 * frame.
576 if (hdrflags & L2TP_HDRFLAG_S) {
577 u16 ns, nr;
578 ns = ntohs(*(__be16 *) ptr);
579 ptr += 2;
580 nr = ntohs(*(__be16 *) ptr);
581 ptr += 2;
583 /* Received a packet with sequence numbers. If we're the LNS,
584 * check if we sre sending sequence numbers and if not,
585 * configure it so.
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",
590 session->name);
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);
602 } else {
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++;
611 goto discard;
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",
622 session->name);
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++;
629 goto discard;
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 ptr += 2 + offset;
642 offset = ptr - optr;
643 if (!pskb_may_pull(skb, offset))
644 goto discard;
646 __skb_pull(skb, offset);
648 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
649 * don't send the PPP header (PPP header compression enabled), but
650 * other clients can include the header. So we cope with both cases
651 * here. The PPP header is always FF03 when using L2TP.
653 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
654 * the field may be unaligned.
656 if (!pskb_may_pull(skb, 2))
657 goto discard;
659 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
660 skb_pull(skb, 2);
662 /* Prepare skb for adding to the session's reorder_q. Hold
663 * packets for max reorder_timeout or 1 second if not
664 * reordering.
666 PPPOL2TP_SKB_CB(skb)->length = length;
667 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
668 (session->reorder_timeout ? session->reorder_timeout : HZ);
670 /* Add packet to the session's receive queue. Reordering is done here, if
671 * enabled. Saved L2TP protocol info is stored in skb->sb[].
673 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
674 if (session->reorder_timeout != 0) {
675 /* Packet reordering enabled. Add skb to session's
676 * reorder queue, in order of ns.
678 pppol2tp_recv_queue_skb(session, skb);
679 } else {
680 /* Packet reordering disabled. Discard out-of-sequence
681 * packets
683 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
684 session->stats.rx_seq_discards++;
685 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
686 "%s: oos pkt %hu len %d discarded, "
687 "waiting for %hu, reorder_q_len=%d\n",
688 session->name, PPPOL2TP_SKB_CB(skb)->ns,
689 PPPOL2TP_SKB_CB(skb)->length, session->nr,
690 skb_queue_len(&session->reorder_q));
691 goto discard;
693 skb_queue_tail(&session->reorder_q, skb);
695 } else {
696 /* No sequence numbers. Add the skb to the tail of the
697 * reorder queue. This ensures that it will be
698 * delivered after all previous sequenced skbs.
700 skb_queue_tail(&session->reorder_q, skb);
703 /* Try to dequeue as many skbs from reorder_q as we can. */
704 pppol2tp_recv_dequeue(session);
706 return 0;
708 discard:
709 session->stats.rx_errors++;
710 kfree_skb(skb);
711 sock_put(session->sock);
713 return 0;
715 error:
716 /* Put UDP header back */
717 __skb_push(skb, sizeof(struct udphdr));
719 no_tunnel:
720 return 1;
723 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
724 * Return codes:
725 * 0 : success.
726 * <0: error
727 * >0: skb should be passed up to userspace as UDP.
729 static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
731 struct pppol2tp_tunnel *tunnel;
733 tunnel = pppol2tp_sock_to_tunnel(sk);
734 if (tunnel == NULL)
735 goto pass_up;
737 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
738 "%s: received %d bytes\n", tunnel->name, skb->len);
740 if (pppol2tp_recv_core(sk, skb))
741 goto pass_up;
743 return 0;
745 pass_up:
746 return 1;
749 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
751 static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
752 struct msghdr *msg, size_t len,
753 int flags)
755 int err;
756 struct sk_buff *skb;
757 struct sock *sk = sock->sk;
759 err = -EIO;
760 if (sk->sk_state & PPPOX_BOUND)
761 goto end;
763 msg->msg_namelen = 0;
765 err = 0;
766 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
767 flags & MSG_DONTWAIT, &err);
768 if (skb) {
769 err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data,
770 skb->len);
771 if (err < 0)
772 goto do_skb_free;
773 err = skb->len;
775 do_skb_free:
776 kfree_skb(skb);
777 end:
778 return err;
781 /************************************************************************
782 * Transmit handling
783 ***********************************************************************/
785 /* Tell how big L2TP headers are for a particular session. This
786 * depends on whether sequence numbers are being used.
788 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
790 if (session->send_seq)
791 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
793 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
796 /* Build an L2TP header for the session into the buffer provided.
798 static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
799 void *buf)
801 __be16 *bufp = buf;
802 u16 flags = L2TP_HDR_VER;
804 if (session->send_seq)
805 flags |= L2TP_HDRFLAG_S;
807 /* Setup L2TP header.
808 * FIXME: Can this ever be unaligned? Is direct dereferencing of
809 * 16-bit header fields safe here for all architectures?
811 *bufp++ = htons(flags);
812 *bufp++ = htons(session->tunnel_addr.d_tunnel);
813 *bufp++ = htons(session->tunnel_addr.d_session);
814 if (session->send_seq) {
815 *bufp++ = htons(session->ns);
816 *bufp++ = 0;
817 session->ns++;
818 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
819 "%s: updated ns to %hu\n", session->name, session->ns);
823 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
824 * when a user application does a sendmsg() on the session socket. L2TP and
825 * PPP headers must be inserted into the user's data.
827 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
828 size_t total_len)
830 static const unsigned char ppph[2] = { 0xff, 0x03 };
831 struct sock *sk = sock->sk;
832 struct inet_sock *inet;
833 __wsum csum = 0;
834 struct sk_buff *skb;
835 int error;
836 int hdr_len;
837 struct pppol2tp_session *session;
838 struct pppol2tp_tunnel *tunnel;
839 struct udphdr *uh;
840 unsigned int len;
842 error = -ENOTCONN;
843 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
844 goto error;
846 /* Get session and tunnel contexts */
847 error = -EBADF;
848 session = pppol2tp_sock_to_session(sk);
849 if (session == NULL)
850 goto error;
852 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
853 if (tunnel == NULL)
854 goto error;
856 /* What header length is configured for this session? */
857 hdr_len = pppol2tp_l2tp_header_len(session);
859 /* Allocate a socket buffer */
860 error = -ENOMEM;
861 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
862 sizeof(struct udphdr) + hdr_len +
863 sizeof(ppph) + total_len,
864 0, GFP_KERNEL);
865 if (!skb)
866 goto error;
868 /* Reserve space for headers. */
869 skb_reserve(skb, NET_SKB_PAD);
870 skb_reset_network_header(skb);
871 skb_reserve(skb, sizeof(struct iphdr));
872 skb_reset_transport_header(skb);
874 /* Build UDP header */
875 inet = inet_sk(session->tunnel_sock);
876 uh = (struct udphdr *) skb->data;
877 uh->source = inet->sport;
878 uh->dest = inet->dport;
879 uh->len = htons(hdr_len + sizeof(ppph) + total_len);
880 uh->check = 0;
881 skb_put(skb, sizeof(struct udphdr));
883 /* Build L2TP header */
884 pppol2tp_build_l2tp_header(session, skb->data);
885 skb_put(skb, hdr_len);
887 /* Add PPP header */
888 skb->data[0] = ppph[0];
889 skb->data[1] = ppph[1];
890 skb_put(skb, 2);
892 /* Copy user data into skb */
893 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
894 if (error < 0) {
895 kfree_skb(skb);
896 goto error;
898 skb_put(skb, total_len);
900 /* Calculate UDP checksum if configured to do so */
901 if (session->tunnel_sock->sk_no_check != UDP_CSUM_NOXMIT)
902 csum = udp_csum_outgoing(sk, skb);
904 /* Debug */
905 if (session->send_seq)
906 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
907 "%s: send %Zd bytes, ns=%hu\n", session->name,
908 total_len, session->ns - 1);
909 else
910 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
911 "%s: send %Zd bytes\n", session->name, total_len);
913 if (session->debug & PPPOL2TP_MSG_DATA) {
914 int i;
915 unsigned char *datap = skb->data;
917 printk(KERN_DEBUG "%s: xmit:", session->name);
918 for (i = 0; i < total_len; i++) {
919 printk(" %02X", *datap++);
920 if (i == 15) {
921 printk(" ...");
922 break;
925 printk("\n");
928 /* Queue the packet to IP for output */
929 len = skb->len;
930 error = ip_queue_xmit(skb, 1);
932 /* Update stats */
933 if (error >= 0) {
934 tunnel->stats.tx_packets++;
935 tunnel->stats.tx_bytes += len;
936 session->stats.tx_packets++;
937 session->stats.tx_bytes += len;
938 } else {
939 tunnel->stats.tx_errors++;
940 session->stats.tx_errors++;
943 error:
944 return error;
947 /* Transmit function called by generic PPP driver. Sends PPP frame
948 * over PPPoL2TP socket.
950 * This is almost the same as pppol2tp_sendmsg(), but rather than
951 * being called with a msghdr from userspace, it is called with a skb
952 * from the kernel.
954 * The supplied skb from ppp doesn't have enough headroom for the
955 * insertion of L2TP, UDP and IP headers so we need to allocate more
956 * headroom in the skb. This will create a cloned skb. But we must be
957 * careful in the error case because the caller will expect to free
958 * the skb it supplied, not our cloned skb. So we take care to always
959 * leave the original skb unfreed if we return an error.
961 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
963 static const u8 ppph[2] = { 0xff, 0x03 };
964 struct sock *sk = (struct sock *) chan->private;
965 struct sock *sk_tun;
966 int hdr_len;
967 struct pppol2tp_session *session;
968 struct pppol2tp_tunnel *tunnel;
969 int rc;
970 int headroom;
971 int data_len = skb->len;
972 struct inet_sock *inet;
973 __wsum csum = 0;
974 struct udphdr *uh;
975 unsigned int len;
977 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
978 goto abort;
980 /* Get session and tunnel contexts from the socket */
981 session = pppol2tp_sock_to_session(sk);
982 if (session == NULL)
983 goto abort;
985 sk_tun = session->tunnel_sock;
986 if (sk_tun == NULL)
987 goto abort;
988 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
989 if (tunnel == NULL)
990 goto abort;
992 /* What header length is configured for this session? */
993 hdr_len = pppol2tp_l2tp_header_len(session);
995 /* Check that there's enough headroom in the skb to insert IP,
996 * UDP and L2TP and PPP headers. If not enough, expand it to
997 * make room. Note that a new skb (or a clone) is
998 * allocated. If we return an error from this point on, make
999 * sure we free the new skb but do not free the original skb
1000 * since that is done by the caller for the error case.
1002 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1003 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
1004 if (skb_cow_head(skb, headroom))
1005 goto abort;
1007 /* Setup PPP header */
1008 __skb_push(skb, sizeof(ppph));
1009 skb->data[0] = ppph[0];
1010 skb->data[1] = ppph[1];
1012 /* Setup L2TP header */
1013 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
1015 /* Setup UDP header */
1016 inet = inet_sk(sk_tun);
1017 __skb_push(skb, sizeof(*uh));
1018 skb_reset_transport_header(skb);
1019 uh = udp_hdr(skb);
1020 uh->source = inet->sport;
1021 uh->dest = inet->dport;
1022 uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len);
1023 uh->check = 0;
1025 /* *BROKEN* Calculate UDP checksum if configured to do so */
1026 if (sk_tun->sk_no_check != UDP_CSUM_NOXMIT)
1027 csum = udp_csum_outgoing(sk_tun, skb);
1029 /* Debug */
1030 if (session->send_seq)
1031 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1032 "%s: send %d bytes, ns=%hu\n", session->name,
1033 data_len, session->ns - 1);
1034 else
1035 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1036 "%s: send %d bytes\n", session->name, data_len);
1038 if (session->debug & PPPOL2TP_MSG_DATA) {
1039 int i;
1040 unsigned char *datap = skb->data;
1042 printk(KERN_DEBUG "%s: xmit:", session->name);
1043 for (i = 0; i < data_len; i++) {
1044 printk(" %02X", *datap++);
1045 if (i == 31) {
1046 printk(" ...");
1047 break;
1050 printk("\n");
1053 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1054 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1055 IPSKB_REROUTED);
1056 nf_reset(skb);
1058 /* Get routing info from the tunnel socket */
1059 dst_release(skb->dst);
1060 skb->dst = sk_dst_get(sk_tun);
1061 skb_orphan(skb);
1062 skb->sk = sk_tun;
1064 /* Queue the packet to IP for output */
1065 len = skb->len;
1066 rc = ip_queue_xmit(skb, 1);
1068 /* Update stats */
1069 if (rc >= 0) {
1070 tunnel->stats.tx_packets++;
1071 tunnel->stats.tx_bytes += len;
1072 session->stats.tx_packets++;
1073 session->stats.tx_bytes += len;
1074 } else {
1075 tunnel->stats.tx_errors++;
1076 session->stats.tx_errors++;
1079 return 1;
1081 abort:
1082 /* Free the original skb */
1083 kfree_skb(skb);
1084 return 1;
1087 /*****************************************************************************
1088 * Session (and tunnel control) socket create/destroy.
1089 *****************************************************************************/
1091 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1092 * too.
1094 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1096 int hash;
1097 struct hlist_node *walk;
1098 struct hlist_node *tmp;
1099 struct pppol2tp_session *session;
1100 struct sock *sk;
1102 if (tunnel == NULL)
1103 BUG();
1105 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1106 "%s: closing all sessions...\n", tunnel->name);
1108 write_lock(&tunnel->hlist_lock);
1109 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1110 again:
1111 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1112 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1114 sk = session->sock;
1116 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1117 "%s: closing session\n", session->name);
1119 hlist_del_init(&session->hlist);
1121 /* Since we should hold the sock lock while
1122 * doing any unbinding, we need to release the
1123 * lock we're holding before taking that lock.
1124 * Hold a reference to the sock so it doesn't
1125 * disappear as we're jumping between locks.
1127 sock_hold(sk);
1128 write_unlock(&tunnel->hlist_lock);
1129 lock_sock(sk);
1131 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1132 pppox_unbind_sock(sk);
1133 sk->sk_state = PPPOX_DEAD;
1134 sk->sk_state_change(sk);
1137 /* Purge any queued data */
1138 skb_queue_purge(&sk->sk_receive_queue);
1139 skb_queue_purge(&sk->sk_write_queue);
1140 skb_queue_purge(&session->reorder_q);
1142 release_sock(sk);
1143 sock_put(sk);
1145 /* Now restart from the beginning of this hash
1146 * chain. We always remove a session from the
1147 * list so we are guaranteed to make forward
1148 * progress.
1150 write_lock(&tunnel->hlist_lock);
1151 goto again;
1154 write_unlock(&tunnel->hlist_lock);
1157 /* Really kill the tunnel.
1158 * Come here only when all sessions have been cleared from the tunnel.
1160 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1162 /* Remove from socket list */
1163 write_lock(&pppol2tp_tunnel_list_lock);
1164 list_del_init(&tunnel->list);
1165 write_unlock(&pppol2tp_tunnel_list_lock);
1167 atomic_dec(&pppol2tp_tunnel_count);
1168 kfree(tunnel);
1171 /* Tunnel UDP socket destruct hook.
1172 * The tunnel context is deleted only when all session sockets have been
1173 * closed.
1175 static void pppol2tp_tunnel_destruct(struct sock *sk)
1177 struct pppol2tp_tunnel *tunnel;
1179 tunnel = pppol2tp_sock_to_tunnel(sk);
1180 if (tunnel == NULL)
1181 goto end;
1183 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1184 "%s: closing...\n", tunnel->name);
1186 /* Close all sessions */
1187 pppol2tp_tunnel_closeall(tunnel);
1189 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1190 (udp_sk(sk))->encap_type = 0;
1191 (udp_sk(sk))->encap_rcv = NULL;
1193 /* Remove hooks into tunnel socket */
1194 tunnel->sock = NULL;
1195 sk->sk_destruct = tunnel->old_sk_destruct;
1196 sk->sk_user_data = NULL;
1198 /* Call original (UDP) socket descructor */
1199 if (sk->sk_destruct != NULL)
1200 (*sk->sk_destruct)(sk);
1202 pppol2tp_tunnel_dec_refcount(tunnel);
1204 end:
1205 return;
1208 /* Really kill the session socket. (Called from sock_put() if
1209 * refcnt == 0.)
1211 static void pppol2tp_session_destruct(struct sock *sk)
1213 struct pppol2tp_session *session = NULL;
1215 if (sk->sk_user_data != NULL) {
1216 struct pppol2tp_tunnel *tunnel;
1218 session = pppol2tp_sock_to_session(sk);
1219 if (session == NULL)
1220 goto out;
1222 /* Don't use pppol2tp_sock_to_tunnel() here to
1223 * get the tunnel context because the tunnel
1224 * socket might have already been closed (its
1225 * sk->sk_user_data will be NULL) so use the
1226 * session's private tunnel ptr instead.
1228 tunnel = session->tunnel;
1229 if (tunnel != NULL) {
1230 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1232 /* If session_id is zero, this is a null
1233 * session context, which was created for a
1234 * socket that is being used only to manage
1235 * tunnels.
1237 if (session->tunnel_addr.s_session != 0) {
1238 /* Delete the session socket from the
1239 * hash
1241 write_lock(&tunnel->hlist_lock);
1242 hlist_del_init(&session->hlist);
1243 write_unlock(&tunnel->hlist_lock);
1245 atomic_dec(&pppol2tp_session_count);
1248 /* This will delete the tunnel context if this
1249 * is the last session on the tunnel.
1251 session->tunnel = NULL;
1252 session->tunnel_sock = NULL;
1253 pppol2tp_tunnel_dec_refcount(tunnel);
1257 kfree(session);
1258 out:
1259 return;
1262 /* Called when the PPPoX socket (session) is closed.
1264 static int pppol2tp_release(struct socket *sock)
1266 struct sock *sk = sock->sk;
1267 int error;
1269 if (!sk)
1270 return 0;
1272 error = -EBADF;
1273 lock_sock(sk);
1274 if (sock_flag(sk, SOCK_DEAD) != 0)
1275 goto error;
1277 pppox_unbind_sock(sk);
1279 /* Signal the death of the socket. */
1280 sk->sk_state = PPPOX_DEAD;
1281 sock_orphan(sk);
1282 sock->sk = NULL;
1284 /* Purge any queued data */
1285 skb_queue_purge(&sk->sk_receive_queue);
1286 skb_queue_purge(&sk->sk_write_queue);
1288 release_sock(sk);
1290 /* This will delete the session context via
1291 * pppol2tp_session_destruct() if the socket's refcnt drops to
1292 * zero.
1294 sock_put(sk);
1296 return 0;
1298 error:
1299 release_sock(sk);
1300 return error;
1303 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1304 * sockets attached to it.
1306 static struct sock *pppol2tp_prepare_tunnel_socket(int fd, u16 tunnel_id,
1307 int *error)
1309 int err;
1310 struct socket *sock = NULL;
1311 struct sock *sk;
1312 struct pppol2tp_tunnel *tunnel;
1313 struct sock *ret = NULL;
1315 /* Get the tunnel UDP socket from the fd, which was opened by
1316 * the userspace L2TP daemon.
1318 err = -EBADF;
1319 sock = sockfd_lookup(fd, &err);
1320 if (!sock) {
1321 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1322 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1323 tunnel_id, fd, err);
1324 goto err;
1327 sk = sock->sk;
1329 /* Quick sanity checks */
1330 err = -EPROTONOSUPPORT;
1331 if (sk->sk_protocol != IPPROTO_UDP) {
1332 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1333 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1334 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1335 goto err;
1337 err = -EAFNOSUPPORT;
1338 if (sock->ops->family != AF_INET) {
1339 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1340 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1341 tunnel_id, fd, sock->ops->family, AF_INET);
1342 goto err;
1345 err = -ENOTCONN;
1347 /* Check if this socket has already been prepped */
1348 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1349 if (tunnel != NULL) {
1350 /* User-data field already set */
1351 err = -EBUSY;
1352 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1354 /* This socket has already been prepped */
1355 ret = tunnel->sock;
1356 goto out;
1359 /* This socket is available and needs prepping. Create a new tunnel
1360 * context and init it.
1362 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1363 if (sk->sk_user_data == NULL) {
1364 err = -ENOMEM;
1365 goto err;
1368 tunnel->magic = L2TP_TUNNEL_MAGIC;
1369 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1371 tunnel->stats.tunnel_id = tunnel_id;
1372 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1374 /* Hook on the tunnel socket destructor so that we can cleanup
1375 * if the tunnel socket goes away.
1377 tunnel->old_sk_destruct = sk->sk_destruct;
1378 sk->sk_destruct = &pppol2tp_tunnel_destruct;
1380 tunnel->sock = sk;
1381 sk->sk_allocation = GFP_ATOMIC;
1383 /* Misc init */
1384 rwlock_init(&tunnel->hlist_lock);
1386 /* Add tunnel to our list */
1387 INIT_LIST_HEAD(&tunnel->list);
1388 write_lock(&pppol2tp_tunnel_list_lock);
1389 list_add(&tunnel->list, &pppol2tp_tunnel_list);
1390 write_unlock(&pppol2tp_tunnel_list_lock);
1391 atomic_inc(&pppol2tp_tunnel_count);
1393 /* Bump the reference count. The tunnel context is deleted
1394 * only when this drops to zero.
1396 pppol2tp_tunnel_inc_refcount(tunnel);
1398 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1399 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1400 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1402 ret = tunnel->sock;
1404 *error = 0;
1405 out:
1406 if (sock)
1407 sockfd_put(sock);
1409 return ret;
1411 err:
1412 *error = err;
1413 goto out;
1416 static struct proto pppol2tp_sk_proto = {
1417 .name = "PPPOL2TP",
1418 .owner = THIS_MODULE,
1419 .obj_size = sizeof(struct pppox_sock),
1422 /* socket() handler. Initialize a new struct sock.
1424 static int pppol2tp_create(struct socket *sock)
1426 int error = -ENOMEM;
1427 struct sock *sk;
1429 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, 1);
1430 if (!sk)
1431 goto out;
1433 sock_init_data(sock, sk);
1435 sock->state = SS_UNCONNECTED;
1436 sock->ops = &pppol2tp_ops;
1438 sk->sk_backlog_rcv = pppol2tp_recv_core;
1439 sk->sk_protocol = PX_PROTO_OL2TP;
1440 sk->sk_family = PF_PPPOX;
1441 sk->sk_state = PPPOX_NONE;
1442 sk->sk_type = SOCK_STREAM;
1443 sk->sk_destruct = pppol2tp_session_destruct;
1445 error = 0;
1447 out:
1448 return error;
1451 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1453 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1454 int sockaddr_len, int flags)
1456 struct sock *sk = sock->sk;
1457 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1458 struct pppox_sock *po = pppox_sk(sk);
1459 struct sock *tunnel_sock = NULL;
1460 struct pppol2tp_session *session = NULL;
1461 struct pppol2tp_tunnel *tunnel;
1462 struct dst_entry *dst;
1463 int error = 0;
1465 lock_sock(sk);
1467 error = -EINVAL;
1468 if (sp->sa_protocol != PX_PROTO_OL2TP)
1469 goto end;
1471 /* Check for already bound sockets */
1472 error = -EBUSY;
1473 if (sk->sk_state & PPPOX_CONNECTED)
1474 goto end;
1476 /* We don't supporting rebinding anyway */
1477 error = -EALREADY;
1478 if (sk->sk_user_data)
1479 goto end; /* socket is already attached */
1481 /* Don't bind if s_tunnel is 0 */
1482 error = -EINVAL;
1483 if (sp->pppol2tp.s_tunnel == 0)
1484 goto end;
1486 /* Special case: prepare tunnel socket if s_session and
1487 * d_session is 0. Otherwise look up tunnel using supplied
1488 * tunnel id.
1490 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
1491 tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.fd,
1492 sp->pppol2tp.s_tunnel,
1493 &error);
1494 if (tunnel_sock == NULL)
1495 goto end;
1497 tunnel = tunnel_sock->sk_user_data;
1498 } else {
1499 tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel);
1501 /* Error if we can't find the tunnel */
1502 error = -ENOENT;
1503 if (tunnel == NULL)
1504 goto end;
1506 tunnel_sock = tunnel->sock;
1509 /* Check that this session doesn't already exist */
1510 error = -EEXIST;
1511 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1512 if (session != NULL)
1513 goto end;
1515 /* Allocate and initialize a new session context. */
1516 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1517 if (session == NULL) {
1518 error = -ENOMEM;
1519 goto end;
1522 skb_queue_head_init(&session->reorder_q);
1524 session->magic = L2TP_SESSION_MAGIC;
1525 session->owner = current->pid;
1526 session->sock = sk;
1527 session->tunnel = tunnel;
1528 session->tunnel_sock = tunnel_sock;
1529 session->tunnel_addr = sp->pppol2tp;
1530 sprintf(&session->name[0], "sess %hu/%hu",
1531 session->tunnel_addr.s_tunnel,
1532 session->tunnel_addr.s_session);
1534 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1535 session->stats.session_id = session->tunnel_addr.s_session;
1537 INIT_HLIST_NODE(&session->hlist);
1539 /* Inherit debug options from tunnel */
1540 session->debug = tunnel->debug;
1542 /* Default MTU must allow space for UDP/L2TP/PPP
1543 * headers.
1545 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1547 /* If PMTU discovery was enabled, use the MTU that was discovered */
1548 dst = sk_dst_get(sk);
1549 if (dst != NULL) {
1550 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1551 if (pmtu != 0)
1552 session->mtu = session->mru = pmtu -
1553 PPPOL2TP_HEADER_OVERHEAD;
1554 dst_release(dst);
1557 /* Special case: if source & dest session_id == 0x0000, this socket is
1558 * being created to manage the tunnel. Don't add the session to the
1559 * session hash list, just set up the internal context for use by
1560 * ioctl() and sockopt() handlers.
1562 if ((session->tunnel_addr.s_session == 0) &&
1563 (session->tunnel_addr.d_session == 0)) {
1564 error = 0;
1565 sk->sk_user_data = session;
1566 goto out_no_ppp;
1569 /* Get tunnel context from the tunnel socket */
1570 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1571 if (tunnel == NULL) {
1572 error = -EBADF;
1573 goto end;
1576 /* Right now, because we don't have a way to push the incoming skb's
1577 * straight through the UDP layer, the only header we need to worry
1578 * about is the L2TP header. This size is different depending on
1579 * whether sequence numbers are enabled for the data channel.
1581 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1583 po->chan.private = sk;
1584 po->chan.ops = &pppol2tp_chan_ops;
1585 po->chan.mtu = session->mtu;
1587 error = ppp_register_channel(&po->chan);
1588 if (error)
1589 goto end;
1591 /* This is how we get the session context from the socket. */
1592 sk->sk_user_data = session;
1594 /* Add session to the tunnel's hash list */
1595 write_lock(&tunnel->hlist_lock);
1596 hlist_add_head(&session->hlist,
1597 pppol2tp_session_id_hash(tunnel,
1598 session->tunnel_addr.s_session));
1599 write_unlock(&tunnel->hlist_lock);
1601 atomic_inc(&pppol2tp_session_count);
1603 out_no_ppp:
1604 pppol2tp_tunnel_inc_refcount(tunnel);
1605 sk->sk_state = PPPOX_CONNECTED;
1606 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1607 "%s: created\n", session->name);
1609 end:
1610 release_sock(sk);
1612 if (error != 0)
1613 PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1614 "%s: connect failed: %d\n", session->name, error);
1616 return error;
1619 /* getname() support.
1621 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1622 int *usockaddr_len, int peer)
1624 int len = sizeof(struct sockaddr_pppol2tp);
1625 struct sockaddr_pppol2tp sp;
1626 int error = 0;
1627 struct pppol2tp_session *session;
1629 error = -ENOTCONN;
1630 if (sock->sk->sk_state != PPPOX_CONNECTED)
1631 goto end;
1633 session = pppol2tp_sock_to_session(sock->sk);
1634 if (session == NULL) {
1635 error = -EBADF;
1636 goto end;
1639 sp.sa_family = AF_PPPOX;
1640 sp.sa_protocol = PX_PROTO_OL2TP;
1641 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1642 sizeof(struct pppol2tp_addr));
1644 memcpy(uaddr, &sp, len);
1646 *usockaddr_len = len;
1648 error = 0;
1650 end:
1651 return error;
1654 /****************************************************************************
1655 * ioctl() handlers.
1657 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1658 * sockets. However, in order to control kernel tunnel features, we allow
1659 * userspace to create a special "tunnel" PPPoX socket which is used for
1660 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1661 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1662 * calls.
1663 ****************************************************************************/
1665 /* Session ioctl helper.
1667 static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1668 unsigned int cmd, unsigned long arg)
1670 struct ifreq ifr;
1671 int err = 0;
1672 struct sock *sk = session->sock;
1673 int val = (int) arg;
1675 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1676 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1677 session->name, cmd, arg);
1679 sock_hold(sk);
1681 switch (cmd) {
1682 case SIOCGIFMTU:
1683 err = -ENXIO;
1684 if (!(sk->sk_state & PPPOX_CONNECTED))
1685 break;
1687 err = -EFAULT;
1688 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1689 break;
1690 ifr.ifr_mtu = session->mtu;
1691 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1692 break;
1694 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1695 "%s: get mtu=%d\n", session->name, session->mtu);
1696 err = 0;
1697 break;
1699 case SIOCSIFMTU:
1700 err = -ENXIO;
1701 if (!(sk->sk_state & PPPOX_CONNECTED))
1702 break;
1704 err = -EFAULT;
1705 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1706 break;
1708 session->mtu = ifr.ifr_mtu;
1710 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1711 "%s: set mtu=%d\n", session->name, session->mtu);
1712 err = 0;
1713 break;
1715 case PPPIOCGMRU:
1716 err = -ENXIO;
1717 if (!(sk->sk_state & PPPOX_CONNECTED))
1718 break;
1720 err = -EFAULT;
1721 if (put_user(session->mru, (int __user *) arg))
1722 break;
1724 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1725 "%s: get mru=%d\n", session->name, session->mru);
1726 err = 0;
1727 break;
1729 case PPPIOCSMRU:
1730 err = -ENXIO;
1731 if (!(sk->sk_state & PPPOX_CONNECTED))
1732 break;
1734 err = -EFAULT;
1735 if (get_user(val,(int __user *) arg))
1736 break;
1738 session->mru = val;
1739 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1740 "%s: set mru=%d\n", session->name, session->mru);
1741 err = 0;
1742 break;
1744 case PPPIOCGFLAGS:
1745 err = -EFAULT;
1746 if (put_user(session->flags, (int __user *) arg))
1747 break;
1749 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1750 "%s: get flags=%d\n", session->name, session->flags);
1751 err = 0;
1752 break;
1754 case PPPIOCSFLAGS:
1755 err = -EFAULT;
1756 if (get_user(val, (int __user *) arg))
1757 break;
1758 session->flags = val;
1759 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1760 "%s: set flags=%d\n", session->name, session->flags);
1761 err = 0;
1762 break;
1764 case PPPIOCGL2TPSTATS:
1765 err = -ENXIO;
1766 if (!(sk->sk_state & PPPOX_CONNECTED))
1767 break;
1769 if (copy_to_user((void __user *) arg, &session->stats,
1770 sizeof(session->stats)))
1771 break;
1772 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1773 "%s: get L2TP stats\n", session->name);
1774 err = 0;
1775 break;
1777 default:
1778 err = -ENOSYS;
1779 break;
1782 sock_put(sk);
1784 return err;
1787 /* Tunnel ioctl helper.
1789 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1790 * specifies a session_id, the session ioctl handler is called. This allows an
1791 * application to retrieve session stats via a tunnel socket.
1793 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1794 unsigned int cmd, unsigned long arg)
1796 int err = 0;
1797 struct sock *sk = tunnel->sock;
1798 struct pppol2tp_ioc_stats stats_req;
1800 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1801 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1802 cmd, arg);
1804 sock_hold(sk);
1806 switch (cmd) {
1807 case PPPIOCGL2TPSTATS:
1808 err = -ENXIO;
1809 if (!(sk->sk_state & PPPOX_CONNECTED))
1810 break;
1812 if (copy_from_user(&stats_req, (void __user *) arg,
1813 sizeof(stats_req))) {
1814 err = -EFAULT;
1815 break;
1817 if (stats_req.session_id != 0) {
1818 /* resend to session ioctl handler */
1819 struct pppol2tp_session *session =
1820 pppol2tp_session_find(tunnel, stats_req.session_id);
1821 if (session != NULL)
1822 err = pppol2tp_session_ioctl(session, cmd, arg);
1823 else
1824 err = -EBADR;
1825 break;
1827 #ifdef CONFIG_XFRM
1828 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1829 #endif
1830 if (copy_to_user((void __user *) arg, &tunnel->stats,
1831 sizeof(tunnel->stats))) {
1832 err = -EFAULT;
1833 break;
1835 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1836 "%s: get L2TP stats\n", tunnel->name);
1837 err = 0;
1838 break;
1840 default:
1841 err = -ENOSYS;
1842 break;
1845 sock_put(sk);
1847 return err;
1850 /* Main ioctl() handler.
1851 * Dispatch to tunnel or session helpers depending on the socket.
1853 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1854 unsigned long arg)
1856 struct sock *sk = sock->sk;
1857 struct pppol2tp_session *session;
1858 struct pppol2tp_tunnel *tunnel;
1859 int err;
1861 if (!sk)
1862 return 0;
1864 err = -EBADF;
1865 if (sock_flag(sk, SOCK_DEAD) != 0)
1866 goto end;
1868 err = -ENOTCONN;
1869 if ((sk->sk_user_data == NULL) ||
1870 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1871 goto end;
1873 /* Get session context from the socket */
1874 err = -EBADF;
1875 session = pppol2tp_sock_to_session(sk);
1876 if (session == NULL)
1877 goto end;
1879 /* Special case: if session's session_id is zero, treat ioctl as a
1880 * tunnel ioctl
1882 if ((session->tunnel_addr.s_session == 0) &&
1883 (session->tunnel_addr.d_session == 0)) {
1884 err = -EBADF;
1885 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
1886 if (tunnel == NULL)
1887 goto end;
1889 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1890 goto end;
1893 err = pppol2tp_session_ioctl(session, cmd, arg);
1895 end:
1896 return err;
1899 /*****************************************************************************
1900 * setsockopt() / getsockopt() support.
1902 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1903 * sockets. In order to control kernel tunnel features, we allow userspace to
1904 * create a special "tunnel" PPPoX socket which is used for control only.
1905 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1906 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1907 *****************************************************************************/
1909 /* Tunnel setsockopt() helper.
1911 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1912 struct pppol2tp_tunnel *tunnel,
1913 int optname, int val)
1915 int err = 0;
1917 switch (optname) {
1918 case PPPOL2TP_SO_DEBUG:
1919 tunnel->debug = val;
1920 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1921 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1922 break;
1924 default:
1925 err = -ENOPROTOOPT;
1926 break;
1929 return err;
1932 /* Session setsockopt helper.
1934 static int pppol2tp_session_setsockopt(struct sock *sk,
1935 struct pppol2tp_session *session,
1936 int optname, int val)
1938 int err = 0;
1940 switch (optname) {
1941 case PPPOL2TP_SO_RECVSEQ:
1942 if ((val != 0) && (val != 1)) {
1943 err = -EINVAL;
1944 break;
1946 session->recv_seq = val ? -1 : 0;
1947 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1948 "%s: set recv_seq=%d\n", session->name,
1949 session->recv_seq);
1950 break;
1952 case PPPOL2TP_SO_SENDSEQ:
1953 if ((val != 0) && (val != 1)) {
1954 err = -EINVAL;
1955 break;
1957 session->send_seq = val ? -1 : 0;
1959 struct sock *ssk = session->sock;
1960 struct pppox_sock *po = pppox_sk(ssk);
1961 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1962 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1964 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1965 "%s: set send_seq=%d\n", session->name, session->send_seq);
1966 break;
1968 case PPPOL2TP_SO_LNSMODE:
1969 if ((val != 0) && (val != 1)) {
1970 err = -EINVAL;
1971 break;
1973 session->lns_mode = val ? -1 : 0;
1974 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1975 "%s: set lns_mode=%d\n", session->name,
1976 session->lns_mode);
1977 break;
1979 case PPPOL2TP_SO_DEBUG:
1980 session->debug = val;
1981 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1982 "%s: set debug=%x\n", session->name, session->debug);
1983 break;
1985 case PPPOL2TP_SO_REORDERTO:
1986 session->reorder_timeout = msecs_to_jiffies(val);
1987 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1988 "%s: set reorder_timeout=%d\n", session->name,
1989 session->reorder_timeout);
1990 break;
1992 default:
1993 err = -ENOPROTOOPT;
1994 break;
1997 return err;
2000 /* Main setsockopt() entry point.
2001 * Does API checks, then calls either the tunnel or session setsockopt
2002 * handler, according to whether the PPPoL2TP socket is a for a regular
2003 * session or the special tunnel type.
2005 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
2006 char __user *optval, int optlen)
2008 struct sock *sk = sock->sk;
2009 struct pppol2tp_session *session = sk->sk_user_data;
2010 struct pppol2tp_tunnel *tunnel;
2011 int val;
2012 int err;
2014 if (level != SOL_PPPOL2TP)
2015 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2017 if (optlen < sizeof(int))
2018 return -EINVAL;
2020 if (get_user(val, (int __user *)optval))
2021 return -EFAULT;
2023 err = -ENOTCONN;
2024 if (sk->sk_user_data == NULL)
2025 goto end;
2027 /* Get session context from the socket */
2028 err = -EBADF;
2029 session = pppol2tp_sock_to_session(sk);
2030 if (session == NULL)
2031 goto end;
2033 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2035 if ((session->tunnel_addr.s_session == 0) &&
2036 (session->tunnel_addr.d_session == 0)) {
2037 err = -EBADF;
2038 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2039 if (tunnel == NULL)
2040 goto end;
2042 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2043 } else
2044 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2046 err = 0;
2048 end:
2049 return err;
2052 /* Tunnel getsockopt helper. Called with sock locked.
2054 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2055 struct pppol2tp_tunnel *tunnel,
2056 int optname, int *val)
2058 int err = 0;
2060 switch (optname) {
2061 case PPPOL2TP_SO_DEBUG:
2062 *val = tunnel->debug;
2063 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2064 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2065 break;
2067 default:
2068 err = -ENOPROTOOPT;
2069 break;
2072 return err;
2075 /* Session getsockopt helper. Called with sock locked.
2077 static int pppol2tp_session_getsockopt(struct sock *sk,
2078 struct pppol2tp_session *session,
2079 int optname, int *val)
2081 int err = 0;
2083 switch (optname) {
2084 case PPPOL2TP_SO_RECVSEQ:
2085 *val = session->recv_seq;
2086 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2087 "%s: get recv_seq=%d\n", session->name, *val);
2088 break;
2090 case PPPOL2TP_SO_SENDSEQ:
2091 *val = session->send_seq;
2092 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2093 "%s: get send_seq=%d\n", session->name, *val);
2094 break;
2096 case PPPOL2TP_SO_LNSMODE:
2097 *val = session->lns_mode;
2098 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2099 "%s: get lns_mode=%d\n", session->name, *val);
2100 break;
2102 case PPPOL2TP_SO_DEBUG:
2103 *val = session->debug;
2104 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2105 "%s: get debug=%d\n", session->name, *val);
2106 break;
2108 case PPPOL2TP_SO_REORDERTO:
2109 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2110 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2111 "%s: get reorder_timeout=%d\n", session->name, *val);
2112 break;
2114 default:
2115 err = -ENOPROTOOPT;
2118 return err;
2121 /* Main getsockopt() entry point.
2122 * Does API checks, then calls either the tunnel or session getsockopt
2123 * handler, according to whether the PPPoX socket is a for a regular session
2124 * or the special tunnel type.
2126 static int pppol2tp_getsockopt(struct socket *sock, int level,
2127 int optname, char __user *optval, int __user *optlen)
2129 struct sock *sk = sock->sk;
2130 struct pppol2tp_session *session = sk->sk_user_data;
2131 struct pppol2tp_tunnel *tunnel;
2132 int val, len;
2133 int err;
2135 if (level != SOL_PPPOL2TP)
2136 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2138 if (get_user(len, (int __user *) optlen))
2139 return -EFAULT;
2141 len = min_t(unsigned int, len, sizeof(int));
2143 if (len < 0)
2144 return -EINVAL;
2146 err = -ENOTCONN;
2147 if (sk->sk_user_data == NULL)
2148 goto end;
2150 /* Get the session context */
2151 err = -EBADF;
2152 session = pppol2tp_sock_to_session(sk);
2153 if (session == NULL)
2154 goto end;
2156 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2157 if ((session->tunnel_addr.s_session == 0) &&
2158 (session->tunnel_addr.d_session == 0)) {
2159 err = -EBADF;
2160 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2161 if (tunnel == NULL)
2162 goto end;
2164 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2165 } else
2166 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2168 err = -EFAULT;
2169 if (put_user(len, (int __user *) optlen))
2170 goto end;
2172 if (copy_to_user((void __user *) optval, &val, len))
2173 goto end;
2175 err = 0;
2176 end:
2177 return err;
2180 /*****************************************************************************
2181 * /proc filesystem for debug
2182 *****************************************************************************/
2184 #ifdef CONFIG_PROC_FS
2186 #include <linux/seq_file.h>
2188 struct pppol2tp_seq_data {
2189 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2190 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
2193 static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2195 struct pppol2tp_session *session = NULL;
2196 struct hlist_node *walk;
2197 int found = 0;
2198 int next = 0;
2199 int i;
2201 read_lock(&tunnel->hlist_lock);
2202 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2203 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2204 if (curr == NULL) {
2205 found = 1;
2206 goto out;
2208 if (session == curr) {
2209 next = 1;
2210 continue;
2212 if (next) {
2213 found = 1;
2214 goto out;
2218 out:
2219 read_unlock(&tunnel->hlist_lock);
2220 if (!found)
2221 session = NULL;
2223 return session;
2226 static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_tunnel *curr)
2228 struct pppol2tp_tunnel *tunnel = NULL;
2230 read_lock(&pppol2tp_tunnel_list_lock);
2231 if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) {
2232 goto out;
2234 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2235 out:
2236 read_unlock(&pppol2tp_tunnel_list_lock);
2238 return tunnel;
2241 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2243 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
2244 loff_t pos = *offs;
2246 if (!pos)
2247 goto out;
2249 BUG_ON(m->private == NULL);
2250 pd = m->private;
2252 if (pd->tunnel == NULL) {
2253 if (!list_empty(&pppol2tp_tunnel_list))
2254 pd->tunnel = list_entry(pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
2255 } else {
2256 pd->session = next_session(pd->tunnel, pd->session);
2257 if (pd->session == NULL) {
2258 pd->tunnel = next_tunnel(pd->tunnel);
2262 /* NULL tunnel and session indicates end of list */
2263 if ((pd->tunnel == NULL) && (pd->session == NULL))
2264 pd = NULL;
2266 out:
2267 return pd;
2270 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2272 (*pos)++;
2273 return NULL;
2276 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2278 /* nothing to do */
2281 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2283 struct pppol2tp_tunnel *tunnel = v;
2285 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2286 tunnel->name,
2287 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2288 atomic_read(&tunnel->ref_count) - 1);
2289 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2290 tunnel->debug,
2291 tunnel->stats.tx_packets, tunnel->stats.tx_bytes,
2292 tunnel->stats.tx_errors,
2293 tunnel->stats.rx_packets, tunnel->stats.rx_bytes,
2294 tunnel->stats.rx_errors);
2297 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2299 struct pppol2tp_session *session = v;
2301 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2302 "%04X/%04X %d %c\n",
2303 session->name,
2304 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2305 ntohs(session->tunnel_addr.addr.sin_port),
2306 session->tunnel_addr.s_tunnel,
2307 session->tunnel_addr.s_session,
2308 session->tunnel_addr.d_tunnel,
2309 session->tunnel_addr.d_session,
2310 session->sock->sk_state,
2311 (session == session->sock->sk_user_data) ?
2312 'Y' : 'N');
2313 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2314 session->mtu, session->mru,
2315 session->recv_seq ? 'R' : '-',
2316 session->send_seq ? 'S' : '-',
2317 session->lns_mode ? "LNS" : "LAC",
2318 session->debug,
2319 jiffies_to_msecs(session->reorder_timeout));
2320 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2321 session->nr, session->ns,
2322 session->stats.tx_packets,
2323 session->stats.tx_bytes,
2324 session->stats.tx_errors,
2325 session->stats.rx_packets,
2326 session->stats.rx_bytes,
2327 session->stats.rx_errors);
2330 static int pppol2tp_seq_show(struct seq_file *m, void *v)
2332 struct pppol2tp_seq_data *pd = v;
2334 /* display header on line 1 */
2335 if (v == SEQ_START_TOKEN) {
2336 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2337 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2338 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2339 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2340 "dest-tid/sid state user-data-ok\n");
2341 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2342 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2343 goto out;
2346 /* Show the tunnel or session context.
2348 if (pd->session == NULL)
2349 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2350 else
2351 pppol2tp_seq_session_show(m, pd->session);
2353 out:
2354 return 0;
2357 static struct seq_operations pppol2tp_seq_ops = {
2358 .start = pppol2tp_seq_start,
2359 .next = pppol2tp_seq_next,
2360 .stop = pppol2tp_seq_stop,
2361 .show = pppol2tp_seq_show,
2364 /* Called when our /proc file is opened. We allocate data for use when
2365 * iterating our tunnel / session contexts and store it in the private
2366 * data of the seq_file.
2368 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2370 struct seq_file *m;
2371 struct pppol2tp_seq_data *pd;
2372 int ret = 0;
2374 ret = seq_open(file, &pppol2tp_seq_ops);
2375 if (ret < 0)
2376 goto out;
2378 m = file->private_data;
2380 /* Allocate and fill our proc_data for access later */
2381 ret = -ENOMEM;
2382 m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL);
2383 if (m->private == NULL)
2384 goto out;
2386 pd = m->private;
2387 ret = 0;
2389 out:
2390 return ret;
2393 /* Called when /proc file access completes.
2395 static int pppol2tp_proc_release(struct inode *inode, struct file *file)
2397 struct seq_file *m = (struct seq_file *)file->private_data;
2399 kfree(m->private);
2400 m->private = NULL;
2402 return seq_release(inode, file);
2405 static struct file_operations pppol2tp_proc_fops = {
2406 .owner = THIS_MODULE,
2407 .open = pppol2tp_proc_open,
2408 .read = seq_read,
2409 .llseek = seq_lseek,
2410 .release = pppol2tp_proc_release,
2413 static struct proc_dir_entry *pppol2tp_proc;
2415 #endif /* CONFIG_PROC_FS */
2417 /*****************************************************************************
2418 * Init and cleanup
2419 *****************************************************************************/
2421 static struct proto_ops pppol2tp_ops = {
2422 .family = AF_PPPOX,
2423 .owner = THIS_MODULE,
2424 .release = pppol2tp_release,
2425 .bind = sock_no_bind,
2426 .connect = pppol2tp_connect,
2427 .socketpair = sock_no_socketpair,
2428 .accept = sock_no_accept,
2429 .getname = pppol2tp_getname,
2430 .poll = datagram_poll,
2431 .listen = sock_no_listen,
2432 .shutdown = sock_no_shutdown,
2433 .setsockopt = pppol2tp_setsockopt,
2434 .getsockopt = pppol2tp_getsockopt,
2435 .sendmsg = pppol2tp_sendmsg,
2436 .recvmsg = pppol2tp_recvmsg,
2437 .mmap = sock_no_mmap,
2438 .ioctl = pppox_ioctl,
2441 static struct pppox_proto pppol2tp_proto = {
2442 .create = pppol2tp_create,
2443 .ioctl = pppol2tp_ioctl
2446 static int __init pppol2tp_init(void)
2448 int err;
2450 err = proto_register(&pppol2tp_sk_proto, 0);
2451 if (err)
2452 goto out;
2453 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2454 if (err)
2455 goto out_unregister_pppol2tp_proto;
2457 #ifdef CONFIG_PROC_FS
2458 pppol2tp_proc = create_proc_entry("pppol2tp", 0, proc_net);
2459 if (!pppol2tp_proc) {
2460 err = -ENOMEM;
2461 goto out_unregister_pppox_proto;
2463 pppol2tp_proc->proc_fops = &pppol2tp_proc_fops;
2464 #endif /* CONFIG_PROC_FS */
2465 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2466 PPPOL2TP_DRV_VERSION);
2468 out:
2469 return err;
2471 out_unregister_pppox_proto:
2472 unregister_pppox_proto(PX_PROTO_OL2TP);
2473 out_unregister_pppol2tp_proto:
2474 proto_unregister(&pppol2tp_sk_proto);
2475 goto out;
2478 static void __exit pppol2tp_exit(void)
2480 unregister_pppox_proto(PX_PROTO_OL2TP);
2482 #ifdef CONFIG_PROC_FS
2483 remove_proc_entry("pppol2tp", proc_net);
2484 #endif
2485 proto_unregister(&pppol2tp_sk_proto);
2488 module_init(pppol2tp_init);
2489 module_exit(pppol2tp_exit);
2491 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>,"
2492 "James Chapman <jchapman@katalix.com>");
2493 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2494 MODULE_LICENSE("GPL");
2495 MODULE_VERSION(PPPOL2TP_DRV_VERSION);