K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / net / pppol2tp.c
blobefd8b553356e1be6c56153a59ee82cdd5a215869
1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
8 * Version: 0.17.1
10 * 251003 : Copied from pppoe.c version 0.6.9.
12 * Authors: Martijn van Oosterhout <kleptog@svana.org>
13 * James Chapman (jchapman@katalix.com)
14 * Contributors:
15 * Michal Ostrowski <mostrows@speakeasy.net>
16 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
17 * David S. Miller (davem@redhat.com)
19 * License:
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
27 /* This driver handles only L2TP data frames; control frames are handled by a
28 * userspace application.
30 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
31 * attaches it to a bound UDP socket with local tunnel_id / session_id and
32 * peer tunnel_id / session_id set. Data can then be sent or received using
33 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
34 * can be read or modified using ioctl() or [gs]etsockopt() calls.
36 * When a PPPoL2TP socket is connected with local and peer session_id values
37 * zero, the socket is treated as a special tunnel management socket.
39 * Here's example userspace code to create a socket for sending/receiving data
40 * over an L2TP session:-
42 * struct sockaddr_pppol2tp sax;
43 * int fd;
44 * int session_fd;
46 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
48 * sax.sa_family = AF_PPPOX;
49 * sax.sa_protocol = PX_PROTO_OL2TP;
50 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
51 * sax.pppol2tp.pid = 0; // current pid owns UDP socket
52 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
53 * sax.pppol2tp.addr.sin_port = addr->sin_port;
54 * sax.pppol2tp.addr.sin_family = AF_INET;
55 * sax.pppol2tp.s_tunnel = tunnel_id;
56 * sax.pppol2tp.s_session = session_id;
57 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
58 * sax.pppol2tp.d_session = peer_session_id;
60 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
64 #include <linux/module.h>
65 #include <linux/version.h>
66 #include <linux/string.h>
67 #include <linux/list.h>
68 #include <asm/uaccess.h>
70 #include <linux/kernel.h>
71 #include <linux/spinlock.h>
72 #include <linux/kthread.h>
73 #include <linux/sched.h>
74 #include <linux/slab.h>
75 #include <linux/errno.h>
77 #include <linux/netdevice.h>
78 #include <linux/net.h>
79 #include <linux/inetdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/init.h>
82 #include <linux/udp.h>
83 #include <linux/if_pppox.h>
84 #include <linux/if_pppol2tp.h>
85 #include <net/sock.h>
86 #include <linux/ppp_channel.h>
87 #include <linux/ppp_defs.h>
88 #include <linux/if_ppp.h>
89 #include <linux/file.h>
90 #include <linux/hash.h>
91 #include <linux/proc_fs.h>
92 #include <net/dst.h>
93 #include <net/ip.h>
94 #include <net/udp.h>
95 #include <net/xfrm.h>
97 #include <asm/byteorder.h>
98 #include <asm/atomic.h>
101 #define PPPOL2TP_DRV_VERSION "V0.17"
103 /* Developer debug code. */
104 #if 0
105 #define DEBUG /* Define to compile in very verbose developer debug */
106 #endif
108 /* Timeouts are specified in milliseconds to/from userspace */
109 #define JIFFIES_TO_MS(t) ((t) * 1000 / HZ)
110 #define MS_TO_JIFFIES(j) ((j * HZ) / 1000)
112 /* L2TP header constants */
113 #define L2TP_HDRFLAG_T 0x8000
114 #define L2TP_HDRFLAG_L 0x4000
115 #define L2TP_HDRFLAG_S 0x0800
116 #define L2TP_HDRFLAG_O 0x0200
117 #define L2TP_HDRFLAG_P 0x0100
119 #define L2TP_HDR_VER_MASK 0x000F
120 #define L2TP_HDR_VER 0x0002
122 /* Space for UDP, L2TP and PPP headers */
123 #define PPPOL2TP_HEADER_OVERHEAD 40
125 /* Just some random numbers */
126 #define L2TP_TUNNEL_MAGIC 0x42114DDA
127 #define L2TP_SESSION_MAGIC 0x0C04EB7D
129 #define PPPOL2TP_HASH_BITS 4
130 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
132 /* Default trace flags */
133 #ifdef DEBUG
134 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS -1
135 #else
136 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
137 #endif
140 /* Debug kernel message control.
141 * Verbose debug messages (L2TP_MSG_DEBUG flag) are optionally compiled in.
143 #ifdef DEBUG
144 #define DPRINTK(_mask, _fmt, args...) \
145 do { \
146 if ((_mask) & PPPOL2TP_MSG_DEBUG) \
147 printk(KERN_DEBUG "PPPOL2TP %s: " _fmt, \
148 __FUNCTION__, ##args); \
149 } while(0)
150 #else
151 #define DPRINTK(_mask, _fmt, args...) do { } while(0)
152 #endif /* DEBUG */
154 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
155 do { \
156 if ((_mask) & (_type)) \
157 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
158 } while(0)
160 /* Extra driver debug. Should only be enabled by developers working on
161 * this driver.
163 #ifdef DEBUG
164 #define ENTER_FUNCTION printk(KERN_DEBUG "PPPOL2TP: --> %s\n", __FUNCTION__)
165 #define EXIT_FUNCTION printk(KERN_DEBUG "PPPOL2TP: <-- %s\n", __FUNCTION__)
166 #else
167 #define ENTER_FUNCTION do { } while(0)
168 #define EXIT_FUNCTION do { } while(0)
169 #endif
171 /* Number of bytes to build transmit L2TP headers.
172 * Unfortunately the size is different depending on whether sequence numbers
173 * are enabled.
175 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
176 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
178 struct pppol2tp_send {
179 struct pppol2tp_session *session;
180 struct pppol2tp_tunnel *tunnel;
181 struct msghdr *msg;
182 struct sk_buff *skb;
183 struct sock *tunnel_sock;
184 struct iovec *iov;
185 struct work_struct send_task;
186 int total_len;
187 struct kiocb iocb;
188 struct sock_iocb siocb;
189 u8 hdr[PPPOL2TP_L2TP_HDR_SIZE_SEQ];
192 struct pppol2tp_tunnel;
194 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
195 * socket. Contains information to determine incoming packets and transmit
196 * outgoing ones.
198 struct pppol2tp_session
200 int magic; /* should be
201 * L2TP_SESSION_MAGIC */
202 int owner; /* pid that opened the socket */
204 struct sock *sock; /* Pointer to the session
205 * PPPoX socket */
206 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
207 * socket */
209 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
211 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
212 * context */
214 char name[20]; /* "sess xxxxx/yyyyy", where
215 * x=tunnel_id, y=session_id */
216 int mtu;
217 int mru;
218 int flags; /* accessed by PPPIOCGFLAGS.
219 * Unused. */
220 int recv_seq:1; /* expect receive packets with
221 * sequence numbers? */
222 int send_seq:1; /* send packets with sequence
223 * numbers? */
224 int lns_mode:1; /* behave as LNS? LAC enables
225 * sequence numbers under
226 * control of LNS. */
227 int debug; /* bitmask of debug message
228 * categories */
229 int reorder_timeout; /* configured reorder timeout
230 * (in jiffies) */
231 u16 nr; /* session NR state (receive) */
232 u16 ns; /* session NR state (send) */
233 struct sk_buff_head reorder_q; /* receive reorder queue */
234 struct pppol2tp_ioc_stats stats;
235 struct hlist_node hlist; /* Hash list node */
238 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
239 * all the associated sessions so incoming packets can be sorted out
241 struct pppol2tp_tunnel
243 int magic; /* Should be L2TP_TUNNEL_MAGIC */
245 struct workqueue_struct *wq; /* Per-tunnel work queue */
247 struct proto *old_proto; /* original proto */
248 struct proto l2tp_proto; /* L2TP proto */
249 rwlock_t hlist_lock; /* protect session_hlist */
250 struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE];
251 /* hashed list of sessions,
252 * hashed by id */
253 int debug; /* bitmask of debug message
254 * categories */
255 char name[12]; /* "tunl xxxxx" */
256 struct pppol2tp_ioc_stats stats;
258 #ifndef UDP_ENCAP_L2TPINUDP
259 void (*old_data_ready)(struct sock *, int);
260 #endif
261 void (*old_sk_destruct)(struct sock *);
263 struct sock *sock; /* Parent socket */
264 struct list_head list; /* Keep a list of all open
265 * prepared sockets */
267 atomic_t session_count;
270 /* Private data stored for received packets in the skb.
272 struct pppol2tp_skb_cb {
273 u16 ns;
274 u16 nr;
275 int has_seq;
276 int length;
277 unsigned long expires;
280 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
282 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
284 static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
285 static struct proto_ops pppol2tp_ops;
286 static LIST_HEAD(pppol2tp_tunnel_list);
288 /* Macros to derive session/tunnel context pointers from a socket. */
289 #define SOCK_2_SESSION(sock, session, err, errval, label, quiet) \
290 session = (struct pppol2tp_session *)((sock)->sk_user_data); \
291 if (!session || session->magic != L2TP_SESSION_MAGIC) { \
292 if (!quiet) \
293 printk(KERN_ERR "%s: %s:%d: BAD SESSION MAGIC " \
294 "(" #sock "=%p) session=%p magic=%x\n", \
295 __FUNCTION__, __FILE__, __LINE__, sock, \
296 session, session ? session->magic : 0); \
297 err = errval; \
298 goto label; \
301 #define SOCK_2_TUNNEL(sock, tunnel, err, errval, label, quiet) \
302 tunnel = (struct pppol2tp_tunnel *)((sock)->sk_user_data); \
303 if (!tunnel || tunnel->magic != L2TP_TUNNEL_MAGIC) { \
304 if (!quiet) \
305 printk(KERN_ERR "%s: %s:%d: BAD TUNNEL MAGIC " \
306 "(" #sock "=%p) tunnel=%p magic=%x\n", \
307 __FUNCTION__, __FILE__, __LINE__, sock, \
308 tunnel, tunnel ? tunnel->magic : 0); \
309 err = errval; \
310 goto label; \
313 /* Session hash list.
314 * The session_id SHOULD be random according to RFC2661, but several
315 * L2TP implementations (Cisco and Microsoft) use incrementing
316 * session_ids. So we do a real hash on the session_id, rather than a
317 * simple bitmask.
319 static inline struct hlist_head *
320 pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id)
322 unsigned long hash_val = (unsigned long) session_id;
323 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)];
326 /* Lookup a session by id
328 static struct pppol2tp_session *
329 pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id)
331 struct hlist_head *session_list =
332 pppol2tp_session_id_hash(tunnel, session_id);
333 struct hlist_node *tmp;
334 struct hlist_node *walk;
335 struct pppol2tp_session *session;
337 hlist_for_each_safe(walk, tmp, session_list) {
338 session = hlist_entry(walk, struct pppol2tp_session, hlist);
339 if (session->tunnel_addr.s_session == session_id) {
340 return session;
344 return NULL;
347 /*****************************************************************************
348 * Receive data handling
349 *****************************************************************************/
351 /* Queue a skb in order. If the skb has no sequence number, queue it
352 * at the tail.
354 static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
356 struct sk_buff *next;
357 struct sk_buff *prev;
358 u16 ns = PPPOL2TP_SKB_CB(skb)->ns;
360 ENTER_FUNCTION;
362 spin_lock(&session->reorder_q.lock);
364 prev = (struct sk_buff *) &session->reorder_q;
365 next = prev->next;
366 while (next != prev) {
367 if (PPPOL2TP_SKB_CB(next)->ns > ns) {
368 __skb_insert(skb, next->prev, next, &session->reorder_q);
369 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
370 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
371 session->name, ns, PPPOL2TP_SKB_CB(next)->ns,
372 skb_queue_len(&session->reorder_q));
373 session->stats.rx_oos_packets++;
374 goto out;
376 next = next->next;
379 __skb_queue_tail(&session->reorder_q, skb);
381 out:
382 spin_unlock(&session->reorder_q.lock);
383 EXIT_FUNCTION;
386 /* Dequeue a single skb, passing it either to ppp or to userspace.
388 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
390 struct pppol2tp_tunnel *tunnel = session->tunnel;
391 int length = PPPOL2TP_SKB_CB(skb)->length;
392 struct sock *session_sock = NULL;
394 ENTER_FUNCTION;
396 /* We're about to requeue the skb, so unlink it and return resources
397 * to its current owner (a socket receive buffer).
399 skb_unlink(skb, &session->reorder_q);
400 skb_orphan(skb);
402 tunnel->stats.rx_packets++;
403 tunnel->stats.rx_bytes += length;
404 session->stats.rx_packets++;
405 session->stats.rx_bytes += length;
407 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
408 /* Bump our Nr */
409 session->nr++;
410 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
411 "%s: updated nr to %hu\n", session->name, session->nr);
414 /* If the socket is bound, send it in to PPP's input queue. Otherwise
415 * queue it on the session socket.
417 session_sock = session->sock;
418 if (session_sock->sk_state & PPPOX_BOUND) {
419 struct pppox_sock *po;
420 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
421 "%s: recv %d byte data frame, passing to ppp\n",
422 session->name, length);
424 /* We need to forget all info related to the L2TP packet
425 * gathered in the skb as we are going to reuse the same
426 * skb for the inner packet.
427 * Namely we need to:
428 * - reset xfrm (IPSec) information as it applies to
429 * the outer L2TP packet and not to the inner one
430 * - release the dst to force a route lookup on the inner
431 * IP packet since skb->dst currently points to the dst
432 * of the UDP tunnel
433 * - reset netfilter information as it doesn't apply
434 * to the inner packet either
436 secpath_reset(skb);
437 dst_release(skb->dst);
438 skb->dst = NULL;
439 nf_reset(skb);
441 po = pppox_sk(session_sock);
442 ppp_input(&po->chan, skb);
443 } else {
444 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
445 "%s: socket not bound\n", session->name);
446 /* Not bound. Queue it now */
447 if (sock_queue_rcv_skb(session_sock, skb) < 0) {
448 session->stats.rx_errors++;
449 kfree_skb(skb);
450 if (!sock_flag(session_sock, SOCK_DEAD))
451 session_sock->sk_data_ready(session_sock, 0);
455 DPRINTK(session->debug, "calling sock_put; refcnt=%d\n",
456 session->sock->sk_refcnt.counter);
457 sock_put(session->sock);
458 EXIT_FUNCTION;
461 /* Dequeue skbs from the session's reorder_q, subject to packet order.
462 * Skbs that have been in the queue for too long are simply discarded.
464 static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
466 struct sk_buff *next;
467 struct sk_buff *prev;
469 ENTER_FUNCTION;
471 prev = (struct sk_buff *) &session->reorder_q;
472 spin_lock(&session->reorder_q.lock);
473 next = prev->next;
475 /* If the pkt at the head of the queue has the nr that we
476 * expect to send up next, dequeue it and any other
477 * in-sequence packets behind it.
479 while (next != prev) {
480 struct sk_buff *skb = next;
481 next = next->next;
482 spin_unlock(&session->reorder_q.lock);
484 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
485 session->stats.rx_seq_discards++;
486 session->stats.rx_errors++;
487 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
488 "%s: oos pkt %hu len %d discarded (too old), waiting for %hu, reorder_q_len=%d\n",
489 session->name, PPPOL2TP_SKB_CB(skb)->ns,
490 PPPOL2TP_SKB_CB(skb)->length, session->nr,
491 skb_queue_len(&session->reorder_q));
492 skb_unlink(skb, &session->reorder_q);
493 kfree_skb(skb);
494 sock_put(session->sock);
495 goto again;
498 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
499 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
500 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
501 "%s: holding oos pkt %hu len %d, waiting for %hu, reorder_q_len=%d\n",
502 session->name, PPPOL2TP_SKB_CB(skb)->ns,
503 PPPOL2TP_SKB_CB(skb)->length, session->nr,
504 skb_queue_len(&session->reorder_q));
505 goto out;
508 pppol2tp_recv_dequeue_skb(session, skb);
509 again:
510 spin_lock(&session->reorder_q.lock);
513 spin_unlock(&session->reorder_q.lock);
514 out:
515 EXIT_FUNCTION;
518 /* Internal receive frame. Do the real work of receiving an L2TP data frame
519 * here. The skb is not on a list when we get here.
520 * Returns 0 if the packet was a data packet and was successfully passed on.
521 * Returns 1 if the packet was not a good data packet and could not be
522 * forwarded. All such packets are passed up to userspace to deal with.
524 static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
526 struct pppol2tp_session *session = NULL;
527 int error = 0;
528 struct pppol2tp_tunnel *tunnel;
529 unsigned char *ptr;
530 u16 hdrflags;
531 u16 tunnel_id, session_id;
532 int length, i;
533 struct udphdr *uh;
535 ENTER_FUNCTION;
537 SOCK_2_TUNNEL(sock, tunnel, error, 1, end, 0);
539 /* Short packet? */
540 if (skb->len < sizeof(struct udphdr)) {
541 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
542 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
543 goto end;
546 /* Get length of L2TP packet */
547 uh = (struct udphdr *) skb_transport_header(skb);
548 length = ntohs(uh->len) - sizeof(struct udphdr);
550 /* Point to L2TP header */
551 ptr = skb->data + sizeof(struct udphdr);
553 /* Trace packet contents, if enabled */
554 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
555 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
557 for (i = 0; i < length && i < 16; i++)
558 printk(" %02X", ptr[i]);
559 printk("\n");
562 /* Too short? */
563 if (length < 12) {
564 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
565 "%s: recv short L2TP packet (len=%d)\n", tunnel->name, length);
566 goto end;
569 /* Get L2TP header flags */
570 hdrflags = ntohs(*(u16*)ptr);
572 /* If type is control packet, it is handled by userspace. */
573 if (hdrflags & L2TP_HDRFLAG_T) {
574 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
575 "%s: recv control packet, len=%d\n", tunnel->name, length);
576 goto end;
579 /* Skip flags */
580 ptr += 2;
582 /* If length is present, skip it */
583 if (hdrflags & L2TP_HDRFLAG_L)
584 ptr += 2;
586 /* Extract tunnel and session ID */
587 tunnel_id = ntohs(*(u16 *) ptr);
588 ptr += 2;
589 session_id = ntohs(*(u16 *) ptr);
590 ptr += 2;
592 /* Find the session context */
593 session = pppol2tp_session_find(tunnel, session_id);
594 if (!session) {
595 /* Not found? Pass to userspace to deal with */
596 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
597 "%s: no socket found (%hu/%hu). Passing up.\n",
598 tunnel->name, tunnel_id, session_id);
599 goto end;
601 sock_hold(session->sock);
603 DPRINTK(session->debug, "%s: socket rcvbuf alloc=%d\n",
604 session->name, atomic_read(&sock->sk_rmem_alloc));
606 /* The ref count on the socket was increased by the above call since
607 * we now hold a pointer to the session. Take care to do sock_put()
608 * when exiting this function from now on...
611 /* Handle the optional sequence numbers. If we are the LAC,
612 * enable/disable sequence numbers under the control of the LNS. If
613 * no sequence numbers present but we were expecting them, discard
614 * frame.
616 if (hdrflags & L2TP_HDRFLAG_S) {
617 u16 ns, nr;
618 ns = ntohs(*(u16 *) ptr);
619 ptr += 2;
620 nr = ntohs(*(u16 *) ptr);
621 ptr += 2;
623 /* Received a packet with sequence numbers. If we're the LNS,
624 * check if we sre sending sequence numbers and if not,
625 * configure it so.
627 if ((!session->lns_mode) && (!session->send_seq)) {
628 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
629 "%s: requested to enable seq numbers by LNS\n",
630 session->name);
631 session->send_seq = -1;
634 /* Store L2TP info in the skb */
635 PPPOL2TP_SKB_CB(skb)->ns = ns;
636 PPPOL2TP_SKB_CB(skb)->nr = nr;
637 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
639 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
640 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
641 session->name, ns, nr, session->nr);
642 } else {
643 /* No sequence numbers.
644 * If user has configured mandatory sequence numbers, discard.
646 if (session->recv_seq) {
647 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
648 "%s: recv data has no seq numbers when required. "
649 "Discarding\n", session->name);
650 session->stats.rx_seq_discards++;
651 session->stats.rx_errors++;
652 goto discard;
655 /* If we're the LAC and we're sending sequence numbers, the
656 * LNS has requested that we no longer send sequence numbers.
657 * If we're the LNS and we're sending sequence numbers, the
658 * LAC is broken. Discard the frame.
660 if ((!session->lns_mode) && (session->send_seq)) {
661 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
662 "%s: requested to disable seq numbers by LNS\n",
663 session->name);
664 session->send_seq = 0;
665 } else if (session->send_seq) {
666 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
667 "%s: recv data has no seq numbers when required. "
668 "Discarding\n", session->name);
669 session->stats.rx_seq_discards++;
670 session->stats.rx_errors++;
671 goto discard;
674 /* Store L2TP info in the skb */
675 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
678 /* If offset bit set, skip it. */
679 if (hdrflags & L2TP_HDRFLAG_O)
680 ptr += 2 + ntohs(*(u16 *) ptr);
682 skb_pull(skb, ptr - skb->data);
684 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
685 * don't send the PPP header (PPP header compression enabled), but
686 * other clients can include the header. So we cope with both cases
687 * here. The PPP header is always FF03 when using L2TP.
689 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
690 * the field may be unaligned.
692 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
693 skb_pull(skb, 2);
695 /* Prepare skb for adding to the session's reorder_q. Hold
696 * packets for max reorder_timeout or 1 second if not
697 * reordering.
699 PPPOL2TP_SKB_CB(skb)->length = length;
700 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
701 (session->reorder_timeout ? session->reorder_timeout : HZ);
703 /* Add packet to the session's receive queue. Reordering is done here, if
704 * enabled. Saved L2TP protocol info is stored in skb->sb[].
706 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
707 if (session->reorder_timeout != 0) {
708 /* Packet reordering enabled. Add skb to session's
709 * reorder queue, in order of ns.
711 pppol2tp_recv_queue_skb(session, skb);
712 } else {
713 /* Packet reordering disabled. Discard out-of-sequence
714 * packets
716 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
717 session->stats.rx_seq_discards++;
718 session->stats.rx_errors++;
719 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
720 "%s: oos pkt %hu len %d discarded, waiting for %hu, reorder_q_len=%d\n",
721 session->name, PPPOL2TP_SKB_CB(skb)->ns,
722 PPPOL2TP_SKB_CB(skb)->length, session->nr,
723 skb_queue_len(&session->reorder_q));
724 goto discard;
726 skb_queue_tail(&session->reorder_q, skb);
728 } else {
729 /* No sequence numbers. Add the skb to the tail of the
730 * reorder queue. This ensures that it will be
731 * delivered after all previous sequenced skbs.
733 skb_queue_tail(&session->reorder_q, skb);
736 /* Try to dequeue as many skbs from reorder_q as we can. */
737 pppol2tp_recv_dequeue(session);
739 EXIT_FUNCTION;
740 return 0;
742 discard:
743 DPRINTK(session->debug, "discarding skb, len=%d\n", skb->len);
744 kfree_skb(skb);
746 DPRINTK(session->debug, "calling sock_put; refcnt=%d\n",
747 session->sock->sk_refcnt.counter);
748 sock_put(session->sock);
749 EXIT_FUNCTION;
750 return 0;
752 end:
753 EXIT_FUNCTION;
754 return 1;
757 #ifndef UDP_ENCAP_L2TPINUDP
758 /* The data_ready hook on the UDP socket. Scan the incoming packet list for
759 * packets to process. Only control or bad data packets are delivered to
760 * userspace.
762 static void pppol2tp_data_ready(struct sock *sk, int len)
764 int err;
765 struct pppol2tp_tunnel *tunnel;
766 struct sk_buff *skb;
768 ENTER_FUNCTION;
769 SOCK_2_TUNNEL(sk, tunnel, err, -EBADF, end, 0);
771 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
772 "%s: received %d bytes\n", tunnel->name, len);
774 skb = skb_dequeue(&sk->sk_receive_queue);
775 if (skb != NULL) {
776 if (pppol2tp_recv_core(sk, skb)) {
777 DPRINTK(tunnel->debug, "%s: packet passing to userspace\n",
778 tunnel->name);
779 skb_queue_head(&sk->sk_receive_queue, skb);
780 tunnel->old_data_ready(sk, len);
781 } else {
782 DPRINTK(tunnel->debug, "%s: data packet received\n",
783 tunnel->name);
786 end:
787 EXIT_FUNCTION;
788 return;
790 #else
791 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
792 * Return codes:
793 * 0 : success.
794 * <0: error
795 * >0: skb should be passed up to userspace as UDP.
797 static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
799 int err;
800 struct pppol2tp_tunnel *tunnel;
802 ENTER_FUNCTION;
803 SOCK_2_TUNNEL(sk, tunnel, err, -EBADF, pass_up, 0);
805 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
806 "%s: received %d bytes\n", tunnel->name, skb->len);
808 if (pppol2tp_recv_core(sk, skb))
809 goto pass_up;
811 EXIT_FUNCTION;
812 return 0;
814 pass_up:
815 EXIT_FUNCTION;
816 return 1;
818 #endif
820 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
822 static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
823 struct msghdr *msg, size_t len,
824 int flags)
826 int err = 0;
827 struct sk_buff *skb = NULL;
828 struct sock *sk = sock->sk;
830 ENTER_FUNCTION;
832 err = -EIO;
833 if (sock->state & PPPOX_BOUND)
834 goto error;
836 msg->msg_namelen = 0;
838 skb=skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
839 flags & MSG_DONTWAIT, &err);
840 if (!skb)
841 goto error;
843 if (len > skb->len)
844 len = skb->len;
845 else if (len < skb->len)
846 msg->msg_flags |= MSG_TRUNC;
848 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
849 if (likely(err == 0))
850 err = len;
852 if (skb)
853 kfree_skb(skb);
854 error:
855 EXIT_FUNCTION;
856 return err;
859 /************************************************************************
860 * Transmit handling
861 ***********************************************************************/
863 /* Internal UDP socket transmission
865 static int pppol2tp_udp_sock_send(struct kiocb *iocb,
866 struct pppol2tp_session *session,
867 struct pppol2tp_tunnel *tunnel,
868 struct msghdr *msg, int total_len)
870 mm_segment_t fs;
871 int error;
873 ENTER_FUNCTION;
875 DPRINTK(session->debug, "%s: udp_sendmsg call...\n", session->name);
876 #ifdef DEBUG
877 /* Catch bad socket parameter errors */
878 if (msg->msg_name) {
879 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
880 if (msg->msg_namelen < sizeof(*usin)) {
881 printk(KERN_ERR "msg->msg_namelen wrong, %d\n", msg->msg_namelen);
882 return -EINVAL;
884 if (usin->sin_family != AF_INET) {
885 if (usin->sin_family != AF_UNSPEC) {
886 printk(KERN_ERR "addr family wrong: %d\n", usin->sin_family);
887 return -EINVAL;
890 if ((usin->sin_addr.s_addr == 0) || (usin->sin_port == 0)) {
891 printk(KERN_ERR "udp addr=%x/%hu\n", usin->sin_addr.s_addr, usin->sin_port);
892 return -EINVAL;
895 #endif /* DEBUG */
897 /* Set to userspace data segment while we do a sendmsg() call. We're
898 * actually calling a userspace API from the kernel here...
900 fs = get_fs();
901 set_fs(get_ds());
903 /* The actual sendmsg() call... */
904 error = tunnel->old_proto->sendmsg(iocb, session->tunnel_sock, msg, total_len);
905 if (error == -EIOCBQUEUED)
906 error = wait_on_sync_kiocb(iocb);
908 /* Back to kernel space */
909 set_fs(fs);
911 if (error >= 0) {
912 tunnel->stats.tx_packets++;
913 tunnel->stats.tx_bytes += error;
914 session->stats.tx_packets++;
915 session->stats.tx_bytes += error;
916 } else {
917 tunnel->stats.tx_errors++;
918 session->stats.tx_errors++;
921 DPRINTK(session->debug, "%s: %s: returning result %d\n", __FUNCTION__,
922 session->name, error);
923 kfree(msg->msg_iov);
924 kfree(msg);
926 EXIT_FUNCTION;
927 return error;
930 /* Build an L2TP header for the session into the buffer provided.
932 static int pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
933 void *buf)
935 u16 *bufp = buf;
936 u16 flags = L2TP_HDR_VER;
938 if (session->send_seq) {
939 flags |= L2TP_HDRFLAG_S;
942 /* Setup L2TP header.
943 * FIXME: Can this ever be unaligned? Is direct dereferencing of
944 * 16-bit header fields safe here for all architectures?
946 *bufp++ = htons(flags);
947 *bufp++ = htons(session->tunnel_addr.d_tunnel);
948 *bufp++ = htons(session->tunnel_addr.d_session);
949 if (session->send_seq) {
950 *bufp++ = htons(session->ns);
951 *bufp++ = 0;
952 session->ns++;
953 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
954 "%s: updated ns to %hu\n", session->name, session->ns);
956 /* This is the PPP header really */
957 *bufp = htons(0xff03);
959 return ((void *) bufp) - buf;
962 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
963 * when a user application does a sendmsg() on the session socket. L2TP and
964 * PPP headers must be inserted into the user's data.
966 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
967 size_t total_len)
969 static unsigned char ppph[2] = { 0xff, 0x03 };
970 struct sock *sk = sock->sk;
971 int error = 0;
972 u8 hdr[PPPOL2TP_L2TP_HDR_SIZE_SEQ];
973 int hdr_len;
974 struct msghdr *msg;
975 struct pppol2tp_session *session;
976 struct pppol2tp_tunnel *tunnel;
978 ENTER_FUNCTION;
980 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
981 error = -ENOTCONN;
982 goto end;
985 /* Get session and tunnel contexts */
986 SOCK_2_SESSION(sk, session, error, -EBADF, end, 0);
987 SOCK_2_TUNNEL(session->tunnel_sock, tunnel, error, -EBADF, end, 0);
989 /* Setup L2TP header */
990 hdr_len = pppol2tp_build_l2tp_header(session, &hdr);
992 if (session->send_seq)
993 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
994 "%s: send %d bytes, ns=%hu\n", session->name,
995 total_len, session->ns - 1);
996 else
997 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
998 "%s: send %d bytes\n", session->name, total_len);
1000 if (session->debug & PPPOL2TP_MSG_DATA) {
1001 int i, j, count;
1003 printk(KERN_DEBUG "%s: xmit:", session->name);
1004 count = 0;
1005 for (i = 0; i < m->msg_iovlen; i++) {
1006 for (j = 0; j < m->msg_iov[i].iov_len; j++) {
1007 printk(" %02X", ((unsigned char *) m->msg_iov[i].iov_base)[j]);
1008 count++;
1009 if (count == 15) {
1010 printk(" ...");
1011 break;
1015 printk("\n");
1018 /* Unfortunately, there is no direct way for us to pass an skb to the
1019 * UDP layer, we have to pretend to be sending ordinary data and use
1020 * sendmsg.
1022 * We add the L2TP and PPP headers here. To do so, we create a new
1023 * struct msghdr and insert the headers as the first iovecs.
1025 msg = kmalloc(sizeof(struct msghdr), GFP_ATOMIC);
1026 if (msg == NULL) {
1027 error = -ENOBUFS;
1028 tunnel->stats.tx_errors++;
1029 session->stats.tx_errors++;
1030 goto end;
1033 msg->msg_iov = kmalloc((m->msg_iovlen + 2) * sizeof(struct iovec),
1034 GFP_ATOMIC);
1035 if (msg->msg_iov == NULL) {
1036 error = -ENOBUFS;
1037 tunnel->stats.tx_errors++;
1038 session->stats.tx_errors++;
1039 kfree(msg);
1040 goto end;
1043 msg->msg_iov[0].iov_base = &hdr;
1044 msg->msg_iov[0].iov_len = hdr_len;
1045 msg->msg_iov[1].iov_base = &ppph;
1046 msg->msg_iov[1].iov_len = sizeof(ppph);
1047 memcpy(&msg->msg_iov[2], &m->msg_iov[0],
1048 m->msg_iovlen * sizeof(struct iovec));
1049 msg->msg_iovlen = m->msg_iovlen + 2;
1051 /* If the user calls sendto() that's just too bad */
1052 msg->msg_name = &session->tunnel_addr.addr;
1053 msg->msg_namelen = sizeof(session->tunnel_addr.addr);
1055 msg->msg_control = m->msg_control;
1056 msg->msg_controllen = m->msg_controllen;
1057 msg->msg_flags = m->msg_flags;
1059 /* Do the real work. This always frees msg, regardless of whether
1060 * there was an error
1062 error = pppol2tp_udp_sock_send(iocb, session, tunnel, msg,
1063 total_len + hdr_len + sizeof(ppph));
1065 end:
1066 EXIT_FUNCTION;
1067 return error;
1070 /* Work queue handler for pppol2tp_xmit().
1072 static void pppol2tp_wq_send(struct work_struct *work)
1074 struct pppol2tp_send *send = container_of(work, struct pppol2tp_send, send_task);
1075 int error;
1076 mm_segment_t oldfs;
1078 oldfs = get_fs();
1079 set_fs(KERNEL_DS);
1081 error = send->tunnel->old_proto->sendmsg(&send->iocb,
1082 send->session->tunnel_sock,
1083 send->msg, send->total_len);
1084 if (error == -EIOCBQUEUED)
1085 error = wait_on_sync_kiocb(&send->iocb);
1087 set_fs(oldfs);
1089 if (error >= 0) {
1090 send->tunnel->stats.tx_packets++;
1091 send->tunnel->stats.tx_bytes += error;
1092 send->session->stats.tx_packets++;
1093 send->session->stats.tx_bytes += error;
1094 } else {
1095 send->tunnel->stats.tx_errors++;
1096 send->session->stats.tx_errors++;
1099 kfree(send->iov);
1100 kfree(send->msg);
1101 kfree_skb(send->skb);
1102 kfree(send);
1105 /* Transmit function called by generic PPP driver. Sends PPP frame over
1106 * PPPoL2TP socket.
1108 * This is almost the same as pppol2tp_sendmsg(), but rather than being called
1109 * with a msghdr from userspace, it is called with a skb from the kernel.
1110 * Since this function cannot block, we must queue up the actual socket send
1111 * on a work queue.
1113 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
1115 static unsigned char ppph[2] = { 0xff, 0x03 };
1116 struct sock *sk = (struct sock *) chan->private;
1117 int error = 0;
1118 int hdr_len;
1119 struct msghdr *msg = NULL;
1120 struct pppol2tp_session *session;
1121 struct pppol2tp_tunnel *tunnel;
1122 struct pppol2tp_send *send = NULL;
1124 ENTER_FUNCTION;
1126 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
1127 DPRINTK(-1, "dead=%d state=%x\n", sock_flag(sk, SOCK_DEAD), sk->sk_state);
1128 error = -ENOTCONN;
1129 goto end;
1132 /* Get session and tunnel contexts from the socket */
1133 SOCK_2_SESSION(sk, session, error, -EBADF, end, 0);
1134 SOCK_2_TUNNEL(session->tunnel_sock, tunnel, error, -EBADF, end, 0);
1136 send = kmalloc(sizeof(struct pppol2tp_send), GFP_ATOMIC);
1137 if (send == NULL) {
1138 error = -ENOBUFS;
1139 tunnel->stats.tx_errors++;
1140 session->stats.tx_errors++;
1141 goto end;
1144 /* Setup L2TP header */
1145 hdr_len = pppol2tp_build_l2tp_header(session, &send->hdr);
1147 if (session->send_seq)
1148 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1149 "%s: send %d bytes, ns=%hu\n",
1150 session->name, skb->len, session->ns - 1);
1151 else
1152 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1153 "%s: send %d bytes\n", session->name, skb->len);
1155 if (session->debug & PPPOL2TP_MSG_DATA) {
1156 int i;
1158 printk(KERN_DEBUG "%s: xmit:", session->name);
1159 for (i = 0; i < skb->len; i++) {
1160 printk(" %02X", skb->data[i]);
1161 if (i == 15) {
1162 printk(" ...");
1163 break;
1166 printk("\n");
1169 /* Unfortunately there doesn't appear to be a way for us to pass an skb
1170 * to the UDP layer, we have to pretend to be sending ordinary data
1171 * and use sendmsg
1173 msg = kmalloc(sizeof(struct msghdr), GFP_ATOMIC);
1174 if (msg == NULL) {
1175 error = -ENOBUFS;
1176 tunnel->stats.tx_errors++;
1177 session->stats.tx_errors++;
1178 goto end;
1181 msg->msg_iov = kmalloc(3 * sizeof(struct iovec), GFP_ATOMIC);
1182 if (msg->msg_iov == NULL) {
1183 error = -ENOBUFS;
1184 tunnel->stats.tx_errors++;
1185 session->stats.tx_errors++;
1186 goto end;
1188 msg->msg_iov[0].iov_base = &send->hdr;
1189 msg->msg_iov[0].iov_len = hdr_len;
1190 /* FIXME: do we need to handle skb fragments here? */
1191 msg->msg_iov[1].iov_base = &ppph;
1192 msg->msg_iov[1].iov_len = sizeof(ppph);
1193 msg->msg_iov[2].iov_base = skb->data;
1194 msg->msg_iov[2].iov_len = skb->len;
1195 msg->msg_iovlen = 3;
1197 /* If the user calls sendto() that's just too bad */
1198 msg->msg_name = &session->tunnel_addr.addr;
1199 msg->msg_namelen = sizeof(session->tunnel_addr.addr);
1201 msg->msg_control = NULL;
1202 msg->msg_controllen = 0;
1203 msg->msg_flags = MSG_DONTWAIT; /* Need this to prevent blocking */
1205 send->session = session;
1206 send->tunnel = tunnel;
1207 send->msg = msg;
1208 send->skb = skb;
1209 send->tunnel_sock = session->tunnel_sock;
1210 send->iov = msg->msg_iov;
1211 send->total_len = skb->len + hdr_len + sizeof(ppph);
1212 init_sync_kiocb(&send->iocb, NULL);
1213 send->iocb.private = &send->siocb;
1215 INIT_WORK(&send->send_task, pppol2tp_wq_send);
1216 queue_work(tunnel->wq, &send->send_task);
1217 return 1;
1219 end:
1220 if (msg != NULL)
1221 kfree(msg);
1222 if (send != NULL)
1223 kfree(send);
1225 EXIT_FUNCTION;
1226 return error;
1229 /*****************************************************************************
1230 * Session (and tunnel control) socket create/destroy.
1231 *****************************************************************************/
1233 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1234 * too. This handles that.
1236 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1238 int hash;
1239 struct hlist_node *walk;
1240 struct hlist_node *tmp;
1241 struct pppol2tp_session *session;
1242 struct sock *sk;
1244 ENTER_FUNCTION;
1246 if (tunnel == NULL)
1247 BUG();
1249 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1250 "%s: closing all sessions...\n", tunnel->name);
1252 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1253 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1254 struct sk_buff *skb;
1256 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1258 sk = session->sock;
1260 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1261 "%s: closing session\n", session->name);
1263 write_lock_bh(&tunnel->hlist_lock);
1264 hlist_del_init(&session->hlist);
1265 write_unlock_bh(&tunnel->hlist_lock);
1267 sock_hold(sk);
1269 lock_sock(sk);
1271 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1272 pppox_unbind_sock(sk);
1273 sk->sk_state = PPPOX_DEAD;
1274 sk->sk_state_change(sk);
1277 /* Purge any queued data */
1278 skb_queue_purge(&sk->sk_receive_queue);
1279 skb_queue_purge(&sk->sk_write_queue);
1280 while ((skb = skb_dequeue(&session->reorder_q))) {
1281 kfree_skb(skb);
1282 sock_put(sk);
1285 release_sock(sk);
1287 DPRINTK(session->debug, "calling sock_put; refcnt=%d\n",
1288 sk->sk_refcnt.counter);
1289 sock_put(sk);
1293 EXIT_FUNCTION;
1296 /* Really kill the tunnel.
1297 * Come here only when all sessions have been cleared from the tunnel.
1299 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1301 struct sock *sk = tunnel->sock;
1303 ENTER_FUNCTION;
1305 /* Remove from socket list */
1306 list_del_init(&tunnel->list);
1308 sk->sk_prot = tunnel->old_proto;
1309 #ifndef UDP_ENCAP_L2TPINUDP
1310 sk->sk_data_ready = tunnel->old_data_ready;
1311 #else
1312 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1313 (udp_sk(sk))->encap_type = 0;
1314 (udp_sk(sk))->encap_rcv = NULL;
1315 #endif
1316 sk->sk_destruct = tunnel->old_sk_destruct;
1317 sk->sk_user_data = NULL;
1319 flush_workqueue(tunnel->wq);
1320 destroy_workqueue(tunnel->wq);
1322 DPRINTK(tunnel->debug, "%s: MOD_DEC_USE_COUNT\n", tunnel->name);
1323 kfree(tunnel);
1325 EXIT_FUNCTION;
1328 /* Tunnel UDP socket destruct hook.
1329 * The tunnel context is deleted only when all session sockets have been
1330 * closed.
1332 static void pppol2tp_tunnel_destruct(struct sock *sk)
1334 struct pppol2tp_tunnel *tunnel;
1335 int error = 0;
1336 ENTER_FUNCTION;
1338 SOCK_2_TUNNEL(sk, tunnel, error, -EBADF, end, 0);
1340 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1341 "%s: closing...\n", tunnel->name);
1343 pppol2tp_tunnel_closeall(tunnel);
1345 end:
1346 EXIT_FUNCTION;
1347 return;
1350 /* Really kill the socket. (Called from sock_put if refcnt == 0.)
1352 static void pppol2tp_session_destruct(struct sock *sk)
1354 struct pppol2tp_session *session = NULL;
1355 int error = 0;
1357 ENTER_FUNCTION;
1359 if (sk->sk_user_data != NULL) {
1360 struct pppol2tp_tunnel *tunnel;
1362 SOCK_2_SESSION(sk, session, error, -EBADF, out, 0);
1363 skb_queue_purge(&session->reorder_q);
1365 /* Don't use SOCK_2_TUNNEL() here to get the tunnel context
1366 * because the tunnel socket might have already been closed
1367 * (its sk->sk_user_data will be NULL) so use the session's
1368 * private tunnel ptr instead.
1370 tunnel = session->tunnel;
1371 if (tunnel != NULL) {
1372 if (tunnel->magic != L2TP_TUNNEL_MAGIC) {
1373 printk(KERN_ERR "%s: %s:%d: BAD TUNNEL MAGIC "
1374 "( tunnel=%p magic=%x )\n",
1375 __FUNCTION__, __FILE__, __LINE__,
1376 tunnel, tunnel->magic);
1377 goto out;
1381 /* Delete tunnel context if this was the last session on the
1382 * tunnel. This was allocated when the first session was
1383 * created on the tunnel. See
1384 * pppol2tp_prepare_tunnel_socket().
1386 DPRINTK(tunnel->debug, "%s: session_count=%d\n",
1387 tunnel->name, atomic_read(&tunnel->session_count));
1388 if (atomic_dec_and_test(&tunnel->session_count)) {
1389 pppol2tp_tunnel_free(tunnel);
1393 if (session != NULL)
1394 kfree(session);
1396 out:
1397 EXIT_FUNCTION;
1400 /* Called when the PPPoX socket (session) is closed.
1402 static int pppol2tp_release(struct socket *sock)
1404 struct sock *sk = sock->sk;
1405 struct pppol2tp_session *session = NULL;
1406 struct pppol2tp_tunnel *tunnel;
1407 int error = 0;
1408 ENTER_FUNCTION;
1410 if (!sk)
1411 return 0;
1413 if (sock_flag(sk, SOCK_DEAD) != 0)
1414 return -EBADF;
1416 if (sk->sk_user_data) { /* Was this socket actually connected? */
1417 SOCK_2_SESSION(sk, session, error, -EBADF, end, 0);
1419 /* Don't use SOCK_2_TUNNEL() here to get the tunnel context
1420 * because the tunnel socket might have already been closed
1421 * (its sk->sk_user_data will be NULL) so use the session's
1422 * private tunnel ptr instead.
1424 tunnel = session->tunnel;
1425 if (tunnel != NULL) {
1426 if (tunnel->magic == L2TP_TUNNEL_MAGIC) {
1427 /* Delete the session socket from the hash */
1428 write_lock_bh(&tunnel->hlist_lock);
1429 hlist_del_init(&session->hlist);
1430 write_unlock_bh(&tunnel->hlist_lock);
1431 } else {
1432 printk(KERN_ERR "%s: %s:%d: BAD TUNNEL MAGIC "
1433 "( tunnel=%p magic=%x )\n",
1434 __FUNCTION__, __FILE__, __LINE__,
1435 tunnel, tunnel->magic);
1436 goto end;
1441 lock_sock(sk);
1443 pppox_unbind_sock(sk);
1445 /* Signal the death of the socket. */
1446 sk->sk_state = PPPOX_DEAD;
1447 sock_orphan(sk);
1448 sock->sk = NULL;
1450 /* Purge any queued data */
1451 skb_queue_purge(&sk->sk_receive_queue);
1452 skb_queue_purge(&sk->sk_write_queue);
1453 if (session != NULL) {
1454 struct sk_buff *skb;
1455 while ((skb = skb_dequeue(&session->reorder_q))) {
1456 kfree_skb(skb);
1457 sock_put(sk);
1459 sock_put(sk);
1462 release_sock(sk);
1464 if (session != NULL)
1465 DPRINTK(session->debug, "calling sock_put; refcnt=%d\n",
1466 session->sock->sk_refcnt.counter);
1467 sock_put(sk);
1469 end:
1470 EXIT_FUNCTION;
1471 return error;
1474 /* Copied from fget() in fs/file_table.c.
1475 * Allows caller to specify the pid that owns the fd.
1477 static struct file *pppol2tp_fget(pid_t pid, unsigned int fd)
1479 struct file *file;
1480 struct files_struct *files = current->files;
1482 if (pid != 0) {
1483 struct task_struct *tsk = find_task_by_pid(pid);
1484 if (tsk == NULL)
1485 return NULL;
1486 files = tsk->files;
1489 spin_lock(&files->file_lock);
1490 file = fcheck_files(files, fd);
1491 if (file)
1492 get_file(file);
1493 spin_unlock(&files->file_lock);
1494 return file;
1497 /* Copied from sockfd_lookup() in net/socket.c.
1498 * Allows caller to specify the pid that owns the fd.
1500 static struct socket *pppol2tp_sockfd_lookup(pid_t pid, int fd, int *err)
1502 struct file *file;
1503 struct inode *inode;
1504 struct socket *sock;
1506 if (!(file = pppol2tp_fget(pid, fd))) {
1507 *err = -EBADF;
1508 return NULL;
1511 inode = file->f_dentry->d_inode;
1512 if (!(sock = SOCKET_I(inode))) {
1513 *err = -ENOTSOCK;
1514 fput(file);
1515 return NULL;
1518 if (sock->file != file) {
1519 printk(KERN_ERR "socki_lookup: socket file changed!\n");
1520 sock->file = file;
1522 return sock;
1525 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX sockets
1526 * attached to it
1528 static struct sock *pppol2tp_prepare_tunnel_socket(pid_t pid, int fd,
1529 u16 tunnel_id, int *error)
1531 int err;
1532 struct socket *sock = NULL;
1533 struct sock *sk;
1534 struct pppol2tp_tunnel *tunnel;
1535 struct sock *ret = NULL;
1537 ENTER_FUNCTION;
1539 /* Get the socket from the fd */
1540 err = -EBADF;
1541 sock = pppol2tp_sockfd_lookup(pid, fd, &err);
1542 if (!sock) {
1543 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1544 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1545 tunnel_id, fd, err);
1546 goto err;
1549 /* Quick sanity checks */
1550 err = -ESOCKTNOSUPPORT;
1551 if (sock->type != SOCK_DGRAM) {
1552 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1553 "tunl %hu: fd %d wrong type, got %d, expected %d\n",
1554 tunnel_id, fd, sock->type, SOCK_DGRAM);
1555 goto err;
1557 err = -EAFNOSUPPORT;
1558 if (sock->ops->family!=AF_INET) {
1559 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1560 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1561 tunnel_id, fd, sock->ops->family, AF_INET);
1562 goto err;
1565 err = -ENOTCONN;
1566 sk = sock->sk;
1568 /* Check if this socket has already been prepped */
1569 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1570 if (tunnel != NULL) {
1571 /* User-data field already set */
1572 err = -EBUSY;
1573 if (tunnel->magic != L2TP_TUNNEL_MAGIC) {
1574 printk(KERN_ERR "%s: %s:%d: BAD TUNNEL MAGIC "
1575 "( tunnel=%p magic=%x )\n",
1576 __FUNCTION__, __FILE__, __LINE__,
1577 tunnel, tunnel->magic);
1578 goto err;
1581 /* This socket has already been prepped */
1582 ret = tunnel->sock;
1583 goto out;
1586 /* This socket is available and needs prepping. Create a new tunnel
1587 * context and init it.
1589 sk->sk_user_data = tunnel = kmalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1590 if (sk->sk_user_data == NULL) {
1591 err = -ENOMEM;
1592 goto err;
1595 memset(tunnel, 0, sizeof(struct pppol2tp_tunnel));
1597 tunnel->magic = L2TP_TUNNEL_MAGIC;
1598 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1600 tunnel->stats.tunnel_id = tunnel_id;
1602 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1604 DPRINTK(tunnel->debug, "tunl %hu: allocated tunnel=%p, sk=%p, sock=%p\n",
1605 tunnel_id, tunnel, sk, sock);
1607 tunnel->wq = create_workqueue("kl2tpd");
1608 if (!tunnel->wq) {
1609 err = -ENOMEM;
1610 goto err_free_tunnel;
1613 /* Setup the new protocol stuff */
1614 tunnel->old_proto = sk->sk_prot;
1615 tunnel->l2tp_proto = *sk->sk_prot;
1617 sk->sk_prot = &tunnel->l2tp_proto;
1619 #ifndef UDP_ENCAP_L2TPINUDP
1620 tunnel->old_data_ready = sk->sk_data_ready;
1621 sk->sk_data_ready = &pppol2tp_data_ready;
1622 #else
1623 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1624 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1625 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1626 #endif
1628 tunnel->old_sk_destruct = sk->sk_destruct;
1629 sk->sk_destruct = &pppol2tp_tunnel_destruct;
1631 tunnel->sock = sk;
1632 sk->sk_allocation = GFP_ATOMIC;
1634 rwlock_init(&tunnel->hlist_lock);
1636 /* Add tunnel to our list */
1637 INIT_LIST_HEAD(&tunnel->list);
1638 list_add(&tunnel->list, &pppol2tp_tunnel_list);
1640 ret = tunnel->sock;
1642 *error = 0;
1643 out:
1644 if (sock)
1645 sockfd_put(sock);
1646 EXIT_FUNCTION;
1648 return ret;
1650 err_free_tunnel:
1651 kfree(tunnel);
1652 err:
1653 *error = err;
1654 goto out;
1657 static struct proto pppol2tp_sk_proto = {
1658 .name = "PPPOL2TP",
1659 .owner = THIS_MODULE,
1660 .obj_size = sizeof(struct pppox_sock),
1663 /* socket() handler. Initialize a new struct sock.
1665 static int pppol2tp_create(struct socket *sock)
1667 int error = -ENOMEM;
1668 struct sock *sk;
1670 ENTER_FUNCTION;
1671 DPRINTK(-1, "sock=%p\n", sock);
1673 try_module_get(THIS_MODULE);
1675 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, 1);
1676 if (!sk)
1677 goto out;
1679 sock_init_data(sock, sk);
1681 sock->state = SS_UNCONNECTED;
1682 sock->ops = &pppol2tp_ops;
1684 sk->sk_backlog_rcv = pppol2tp_recv_core;
1685 sk->sk_protocol = PX_PROTO_OL2TP;
1686 sk->sk_family = PF_PPPOX;
1687 sk->sk_state = PPPOX_NONE;
1688 sk->sk_type = SOCK_STREAM;
1689 sk->sk_destruct = pppol2tp_session_destruct;
1691 error = 0;
1693 out:
1694 module_put(THIS_MODULE);
1696 EXIT_FUNCTION;
1697 return error;
1700 /* connect() handler.. Attach a PPPoX socket to a tunnel UDP socket
1702 int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1703 int sockaddr_len, int flags)
1705 struct sock *sk = sock->sk;
1706 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1707 struct pppox_sock *po = pppox_sk(sk);
1708 struct sock *tunnel_sock = NULL;
1709 struct pppol2tp_session *session = NULL;
1710 struct pppol2tp_tunnel *tunnel;
1711 struct dst_entry *dst;
1712 int error = 0;
1714 ENTER_FUNCTION;
1716 DPRINTK(-1, "sock=%p, uservaddr=%p, sockaddr_len=%d, flags=%d, addr=%x/%hu\n",
1717 sock, uservaddr, sockaddr_len, flags,
1718 ntohl(sp->pppol2tp.addr.sin_addr.s_addr), ntohs(sp->pppol2tp.addr.sin_port));
1719 lock_sock(sk);
1721 error = -EINVAL;
1722 if (sp->sa_protocol != PX_PROTO_OL2TP)
1723 goto end;
1725 /* Check for already bound sockets */
1726 error = -EBUSY;
1727 if (sk->sk_state & PPPOX_CONNECTED)
1728 goto end;
1730 /* We don't supporting rebinding anyway */
1731 if (sk->sk_user_data)
1732 goto end; /* socket is already attached */
1734 /* Don't bind if s_tunnel is 0 */
1735 error = -EINVAL;
1736 if (sp->pppol2tp.s_tunnel == 0)
1737 goto end;
1739 /* Look up the tunnel socket and configure it if necessary */
1740 tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.pid,
1741 sp->pppol2tp.fd,
1742 sp->pppol2tp.s_tunnel,
1743 &error);
1744 if (tunnel_sock == NULL)
1745 goto end;
1746 tunnel = tunnel_sock->sk_user_data;
1748 /* Allocate and initialize a new session context.
1750 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1751 if (session == NULL) {
1752 error = -ENOMEM;
1753 goto end;
1756 skb_queue_head_init(&session->reorder_q);
1758 session->magic = L2TP_SESSION_MAGIC;
1759 session->owner = current->pid;
1760 session->sock = sk;
1761 session->tunnel = tunnel;
1762 session->tunnel_sock = tunnel_sock;
1763 session->tunnel_addr = sp->pppol2tp;
1764 sprintf(&session->name[0], "sess %hu/%hu",
1765 session->tunnel_addr.s_tunnel,
1766 session->tunnel_addr.s_session);
1768 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1769 session->stats.session_id = session->tunnel_addr.s_session;
1771 INIT_HLIST_NODE(&session->hlist);
1773 session->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1775 /* Default MTU must allow space for UDP/L2TP/PPP
1776 * headers. Leave some slack.
1778 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1780 /* If PMTU discovery was enabled, use the MTU that was discovered */
1781 dst = sk_dst_get(sk);
1782 if (dst != NULL) {
1783 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1784 if (pmtu != 0) {
1785 session->mtu = session->mru = pmtu -
1786 PPPOL2TP_HEADER_OVERHEAD;
1787 DPRINTK(session->debug,
1788 "%s: MTU set by Path MTU discovery: mtu=%d\n",
1789 session->name, session->mtu);
1791 dst_release(dst);
1794 /* Special case: if source & dest session_id == 0x0000, this socket is
1795 * being created to manage the tunnel. Don't add the session to the
1796 * session hash list, just set up the internal context for use by
1797 * ioctl() and sockopt() handlers.
1799 if ((session->tunnel_addr.s_session == 0) &&
1800 (session->tunnel_addr.d_session == 0)) {
1801 error = 0;
1802 DPRINTK(session->debug,
1803 "tunl %hu: socket created for tunnel mgmt ops\n",
1804 session->tunnel_addr.s_tunnel);
1805 sk->sk_user_data = session;
1806 goto out_no_ppp;
1809 DPRINTK(session->debug, "%s: allocated session=%p, sock=%p, owner=%d\n",
1810 session->name, session, sk, session->owner);
1812 /* Add session to the tunnel's hash list */
1813 SOCK_2_TUNNEL(tunnel_sock, tunnel, error, -EBADF, end, 0);
1814 write_lock_bh(&tunnel->hlist_lock);
1815 hlist_add_head(&session->hlist,
1816 pppol2tp_session_id_hash(tunnel,
1817 session->tunnel_addr.s_session));
1818 write_unlock_bh(&tunnel->hlist_lock);
1820 /* This is how we get the session context from the socket. */
1821 sk->sk_user_data = session;
1823 /* Right now, because we don't have a way to push the incoming skb's
1824 * straight through the UDP layer, the only header we need to worry
1825 * about is the L2TP header. This size is different depending on
1826 * whether sequence numbers are enabled for the data channel.
1828 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1830 po->chan.private = sk;
1831 po->chan.ops = &pppol2tp_chan_ops;
1832 po->chan.mtu = session->mtu;
1834 error = ppp_register_channel(&po->chan);
1835 if (error)
1836 goto end;
1838 out_no_ppp:
1839 atomic_inc(&tunnel->session_count);
1840 sk->sk_state = PPPOX_CONNECTED;
1841 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1842 "%s: created\n", session->name);
1844 end:
1845 release_sock(sk);
1847 if (error != 0)
1848 PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1849 "%s: connect failed: %d\n", session->name, error);
1851 EXIT_FUNCTION;
1853 return error;
1856 /* getname() support.
1858 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1859 int *usockaddr_len, int peer)
1861 int len = sizeof(struct sockaddr_pppol2tp);
1862 struct sockaddr_pppol2tp sp;
1863 int error = 0;
1864 struct pppol2tp_session *session;
1866 ENTER_FUNCTION;
1868 error = -ENOTCONN;
1869 if (sock->sk->sk_state != PPPOX_CONNECTED)
1870 goto end;
1872 SOCK_2_SESSION(sock->sk, session, error, -EBADF, end, 0);
1874 sp.sa_family = AF_PPPOX;
1875 sp.sa_protocol = PX_PROTO_OL2TP;
1876 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1877 sizeof(struct pppol2tp_addr));
1879 memcpy(uaddr, &sp, len);
1881 *usockaddr_len = len;
1883 error = 0;
1884 end:
1885 EXIT_FUNCTION;
1886 return error;
1889 /****************************************************************************
1890 * ioctl() handlers.
1892 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1893 * sockets. However, in order to control kernel tunnel features, we allow
1894 * userspace to create a special "tunnel" PPPoX socket which is used for
1895 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1896 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1897 * calls.
1898 ****************************************************************************/
1900 /* Session ioctl helper.
1902 static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1903 unsigned int cmd, unsigned long arg)
1905 struct ifreq ifr;
1906 int err = 0;
1907 struct sock *sk = session->sock;
1908 int val = (int) arg;
1910 sock_hold(sk);
1912 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1913 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1914 session->name, cmd, arg);
1916 switch (cmd) {
1917 case SIOCGIFMTU:
1918 err = -ENXIO;
1919 if (!(sk->sk_state & PPPOX_CONNECTED))
1920 break;
1922 err = -EFAULT;
1923 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1924 break;
1925 ifr.ifr_mtu = session->mtu;
1926 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1927 break;
1929 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1930 "%s: get mtu=%d\n", session->name, session->mtu);
1931 err = 0;
1932 break;
1934 case SIOCSIFMTU:
1935 err = -ENXIO;
1936 if (!(sk->sk_state & PPPOX_CONNECTED))
1937 break;
1939 err = -EFAULT;
1940 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1941 break;
1943 session->mtu = ifr.ifr_mtu;
1945 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1946 "%s: set mtu=%d\n", session->name, session->mtu);
1947 err = 0;
1948 break;
1950 case PPPIOCGMRU:
1951 err = -ENXIO;
1952 if (!(sk->sk_state & PPPOX_CONNECTED))
1953 break;
1955 err = -EFAULT;
1956 if (put_user(session->mru, (int __user *) arg))
1957 break;
1959 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1960 "%s: get mru=%d\n", session->name, session->mru);
1961 err = 0;
1962 break;
1964 case PPPIOCSMRU:
1965 err = -ENXIO;
1966 if (!(sk->sk_state & PPPOX_CONNECTED))
1967 break;
1969 err = -EFAULT;
1970 if (get_user(val,(int __user *) arg))
1971 break;
1973 session->mru = val;
1974 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1975 "%s: set mru=%d\n", session->name, session->mru);
1976 err = 0;
1977 break;
1979 case PPPIOCGFLAGS:
1980 err = -EFAULT;
1981 if (put_user(session->flags, (int __user *) arg))
1982 break;
1984 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1985 "%s: get flags=%d\n", session->name, session->flags);
1986 err = 0;
1987 break;
1989 case PPPIOCSFLAGS:
1990 err = -EFAULT;
1991 if (get_user(val, (int __user *) arg))
1992 break;
1993 session->flags = val;
1994 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1995 "%s: set flags=%d\n", session->name, session->flags);
1996 err = 0;
1997 break;
1999 case PPPIOCGL2TPSTATS:
2000 err = -ENXIO;
2002 if (!(sk->sk_state & PPPOX_CONNECTED))
2003 break;
2005 if (copy_to_user((void __user *) arg, &session->stats,
2006 sizeof(session->stats)))
2007 break;
2008 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2009 "%s: get L2TP stats\n", session->name);
2010 err = 0;
2011 break;
2013 default:
2014 err = -ENOSYS;
2015 break;
2018 sock_put(sk);
2020 return err;
2023 /* Tunnel ioctl helper.
2025 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
2026 * specifies a session_id, the session ioctl handler is called. This allows an
2027 * application to retrieve session stats via a tunnel socket.
2029 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
2030 unsigned int cmd, unsigned long arg)
2032 int err = 0;
2033 struct sock *sk = tunnel->sock;
2034 struct pppol2tp_ioc_stats stats_req;
2036 sock_hold(sk);
2038 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
2039 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
2040 cmd, arg);
2042 switch (cmd) {
2043 case PPPIOCGL2TPSTATS:
2044 err = -ENXIO;
2046 if (!(sk->sk_state & PPPOX_CONNECTED))
2047 break;
2049 if (copy_from_user(&stats_req, (void __user *) arg,
2050 sizeof(stats_req))) {
2051 err = -EFAULT;
2052 break;
2054 if (stats_req.session_id != 0) {
2055 /* resend to session ioctl handler */
2056 struct pppol2tp_session *session =
2057 pppol2tp_session_find(tunnel, stats_req.session_id);
2058 if (session != NULL)
2059 err = pppol2tp_session_ioctl(session, cmd, arg);
2060 else
2061 err = -EBADR;
2062 break;
2064 #ifdef CONFIG_XFRM
2065 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
2066 #endif
2067 if (copy_to_user((void __user *) arg, &tunnel->stats,
2068 sizeof(tunnel->stats))) {
2069 err = -EFAULT;
2070 break;
2072 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2073 "%s: get L2TP stats\n", tunnel->name);
2074 err = 0;
2075 break;
2077 default:
2078 err = -ENOSYS;
2079 break;
2082 sock_put(sk);
2084 return err;
2087 /* Main ioctl() handler.
2088 * Dispatch to tunnel or session helpers depending on the socket.
2090 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
2091 unsigned long arg)
2093 struct sock *sk = sock->sk;
2094 struct pppol2tp_session *session;
2095 struct pppol2tp_tunnel *tunnel;
2096 int err = 0;
2098 ENTER_FUNCTION;
2100 if (!sk)
2101 return 0;
2103 if (sock_flag(sk, SOCK_DEAD) != 0)
2104 return -EBADF;
2106 if ((sk->sk_user_data == NULL) ||
2107 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)))) {
2108 err = -ENOTCONN;
2109 DPRINTK(-1, "ioctl: socket %p not connected.\n", sk);
2110 goto end;
2113 SOCK_2_SESSION(sk, session, err, -EBADF, end, 0);
2114 SOCK_2_TUNNEL(session->tunnel_sock, tunnel, err, -EBADF, end, 1);
2116 /* Special case: if session's session_id is zero, treat ioctl as a
2117 * tunnel ioctl
2119 if ((session->tunnel_addr.s_session == 0) &&
2120 (session->tunnel_addr.d_session == 0)) {
2121 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
2122 goto end;
2125 err = pppol2tp_session_ioctl(session, cmd, arg);
2127 end:
2128 EXIT_FUNCTION;
2129 return err;
2132 /*****************************************************************************
2133 * setsockopt() / getsockopt() support.
2135 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2136 * sockets. In order to control kernel tunnel features, we allow userspace to
2137 * create a special "tunnel" PPPoX socket which is used for control only.
2138 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2139 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2140 *****************************************************************************/
2142 /* Tunnel setsockopt() helper.
2144 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
2145 struct pppol2tp_tunnel *tunnel,
2146 int optname, int val)
2148 int err = 0;
2150 switch (optname) {
2151 case PPPOL2TP_SO_DEBUG:
2152 tunnel->debug = val;
2153 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2154 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
2155 break;
2157 default:
2158 err = -ENOPROTOOPT;
2159 break;
2162 return err;
2165 /* Session setsockopt helper.
2167 static int pppol2tp_session_setsockopt(struct sock *sk,
2168 struct pppol2tp_session *session,
2169 int optname, int val)
2171 int err = 0;
2173 switch (optname) {
2174 case PPPOL2TP_SO_RECVSEQ:
2175 if ((val != 0) && (val != 1)) {
2176 err = -EINVAL;
2177 break;
2179 session->recv_seq = val ? -1 : 0;
2180 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2181 "%s: set recv_seq=%d\n", session->name,
2182 session->recv_seq);
2183 break;
2185 case PPPOL2TP_SO_SENDSEQ:
2186 if ((val != 0) && (val != 1)) {
2187 err = -EINVAL;
2188 break;
2190 session->send_seq = val ? -1 : 0;
2192 /* FIXME: is it safe to change the ppp channel's
2193 * hdrlen on the fly?
2195 struct sock *sk = session->sock;
2196 struct pppox_sock *po = pppox_sk(sk);
2197 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
2198 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
2200 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2201 "%s: set send_seq=%d\n", session->name, session->send_seq);
2202 break;
2204 case PPPOL2TP_SO_LNSMODE:
2205 if ((val != 0) && (val != 1)) {
2206 err = -EINVAL;
2207 break;
2209 session->lns_mode = val ? -1 : 0;
2210 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2211 "%s: set lns_mode=%d\n", session->name,
2212 session->lns_mode);
2213 break;
2215 case PPPOL2TP_SO_DEBUG:
2216 session->debug = val;
2217 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2218 "%s: set debug=%x\n", session->name, session->debug);
2219 break;
2221 case PPPOL2TP_SO_REORDERTO:
2222 session->reorder_timeout = MS_TO_JIFFIES(val);
2223 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2224 "%s: set reorder_timeout=%d\n", session->name,
2225 session->reorder_timeout);
2226 break;
2228 default:
2229 err = -ENOPROTOOPT;
2230 break;
2233 return err;
2236 /* Main setsockopt() entry point.
2237 * Does API checks, then calls either the tunnel or session setsockopt
2238 * handler, according to whether the PPPoL2TP socket is a for a regular
2239 * session or the special tunnel type.
2241 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
2242 char *optval, int optlen)
2244 struct sock *sk = sock->sk;
2245 struct pppol2tp_session *session = sk->sk_user_data;
2246 struct pppol2tp_tunnel *tunnel;
2247 int val;
2248 int err = 0;
2250 if (level != SOL_PPPOL2TP)
2251 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2253 if (optlen<sizeof(int))
2254 return -EINVAL;
2256 if (get_user(val, (int __user *)optval))
2257 return -EFAULT;
2259 if (sk->sk_user_data == NULL) {
2260 err = -ENOTCONN;
2261 DPRINTK(-1, "setsockopt: socket %p not connected.\n", sk);
2262 goto end;
2265 SOCK_2_SESSION(sk, session, err, -EBADF, end, 0);
2266 SOCK_2_TUNNEL(session->tunnel_sock, tunnel, err, -EBADF, end, 1);
2268 lock_sock(sk);
2270 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2272 if ((session->tunnel_addr.s_session == 0) &&
2273 (session->tunnel_addr.d_session == 0))
2274 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2275 else
2276 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2278 release_sock(sk);
2279 end:
2280 return err;
2283 /* Tunnel getsockopt helper.
2285 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2286 struct pppol2tp_tunnel *tunnel,
2287 int optname, int *val)
2289 int err = 0;
2291 switch (optname) {
2292 case PPPOL2TP_SO_DEBUG:
2293 *val = tunnel->debug;
2294 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2295 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2296 break;
2298 default:
2299 err = -ENOPROTOOPT;
2300 break;
2303 return err;
2306 /* Session getsockopt helper.
2308 static int pppol2tp_session_getsockopt(struct sock *sk,
2309 struct pppol2tp_session *session,
2310 int optname, int *val)
2312 int err = 0;
2314 switch (optname) {
2315 case PPPOL2TP_SO_RECVSEQ:
2316 *val = session->recv_seq;
2317 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2318 "%s: get recv_seq=%d\n", session->name, *val);
2319 break;
2321 case PPPOL2TP_SO_SENDSEQ:
2322 *val = session->send_seq;
2323 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2324 "%s: get send_seq=%d\n", session->name, *val);
2325 break;
2327 case PPPOL2TP_SO_LNSMODE:
2328 *val = session->lns_mode;
2329 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2330 "%s: get lns_mode=%d\n", session->name, *val);
2331 break;
2333 case PPPOL2TP_SO_DEBUG:
2334 *val = session->debug;
2335 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2336 "%s: get debug=%d\n", session->name, *val);
2337 break;
2339 case PPPOL2TP_SO_REORDERTO:
2340 *val = JIFFIES_TO_MS(session->reorder_timeout);
2341 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2342 "%s: get reorder_timeout=%d\n", session->name, *val);
2343 break;
2345 default:
2346 err = -ENOPROTOOPT;
2349 return err;
2352 /* Main getsockopt() entry point.
2353 * Does API checks, then calls either the tunnel or session getsockopt
2354 * handler, according to whether the PPPoX socket is a for a regular session
2355 * or the special tunnel type.
2357 static int pppol2tp_getsockopt(struct socket *sock, int level,
2358 int optname, char *optval, int *optlen)
2360 struct sock *sk = sock->sk;
2361 struct pppol2tp_session *session = sk->sk_user_data;
2362 struct pppol2tp_tunnel *tunnel;
2363 int val, len;
2364 int err = 0;
2366 if (level != SOL_PPPOL2TP)
2367 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2369 if (get_user(len, (int __user *) optlen))
2370 return -EFAULT;
2372 len = min_t(unsigned int, len, sizeof(int));
2374 if (len < 0)
2375 return -EINVAL;
2377 if (sk->sk_user_data == NULL) {
2378 err = -ENOTCONN;
2379 DPRINTK(-1, "getsockopt: socket %p not connected.\n", sk);
2380 goto end;
2383 /* Get the session and tunnel contexts */
2384 SOCK_2_SESSION(sk, session, err, -EBADF, end, 0);
2385 SOCK_2_TUNNEL(session->tunnel_sock, tunnel, err, -EBADF, end, 1);
2387 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2388 if ((session->tunnel_addr.s_session == 0) &&
2389 (session->tunnel_addr.d_session == 0))
2390 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2391 else
2392 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2395 if (put_user(len, (int __user *) optlen))
2396 return -EFAULT;
2398 if (copy_to_user((void __user *) optval, &val, len))
2399 return -EFAULT;
2401 end:
2402 return err;
2405 /*****************************************************************************
2406 * /proc filesystem for debug
2407 *****************************************************************************/
2409 #ifdef CONFIG_PROC_FS
2411 #include <linux/seq_file.h>
2413 static int pppol2tp_proc_open(struct inode *inode, struct file *file);
2414 static void *pppol2tp_proc_start(struct seq_file *m, loff_t *_pos);
2415 static void *pppol2tp_proc_next(struct seq_file *p, void *v, loff_t *pos);
2416 static void pppol2tp_proc_stop(struct seq_file *p, void *v);
2417 static int pppol2tp_proc_show(struct seq_file *m, void *v);
2419 static struct proc_dir_entry *pppol2tp_proc;
2421 static struct seq_operations pppol2tp_proc_ops = {
2422 .start = pppol2tp_proc_start,
2423 .next = pppol2tp_proc_next,
2424 .stop = pppol2tp_proc_stop,
2425 .show = pppol2tp_proc_show,
2428 static struct file_operations pppol2tp_proc_fops = {
2429 .owner = THIS_MODULE,
2430 .open = pppol2tp_proc_open,
2431 .read = seq_read,
2432 .llseek = seq_lseek,
2433 .release = seq_release,
2436 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2438 struct seq_file *m;
2439 int ret = 0;
2441 ENTER_FUNCTION;
2442 ret = seq_open(file, &pppol2tp_proc_ops);
2443 if (ret < 0)
2444 goto out;
2446 m = file->private_data;
2447 m->private = PDE(inode)->data;
2449 out:
2450 EXIT_FUNCTION;
2451 return ret;
2454 static void *pppol2tp_proc_start(struct seq_file *m, loff_t *_pos)
2456 struct pppol2tp_tunnel *tunnel = NULL;
2457 loff_t pos = *_pos;
2458 struct list_head *walk;
2459 struct list_head *tmp;
2461 ENTER_FUNCTION;
2463 /* allow for the header line */
2464 if (!pos) {
2465 tunnel = (void *)1;
2466 goto out;
2468 pos--;
2470 /* find the n'th element in the list */
2471 list_for_each_safe(walk, tmp, &pppol2tp_tunnel_list) {
2472 tunnel = list_entry(walk, struct pppol2tp_tunnel, list);
2473 if (!pos--) {
2474 sock_hold(tunnel->sock);
2475 goto out;
2478 tunnel = NULL;
2480 out:
2481 EXIT_FUNCTION;
2483 return tunnel;
2486 static void *pppol2tp_proc_next(struct seq_file *p, void *v, loff_t *pos)
2488 struct pppol2tp_tunnel *tunnel = v;
2489 struct list_head *tmp;
2490 struct list_head *list;
2492 ENTER_FUNCTION;
2494 (*pos)++;
2496 if (v == (void *)1)
2497 list = &pppol2tp_tunnel_list;
2498 else
2499 list = &tunnel->list;
2501 tmp = list->next;
2502 if (tmp == &pppol2tp_tunnel_list)
2503 tunnel = NULL;
2504 else
2505 tunnel = list_entry(tmp, struct pppol2tp_tunnel, list);
2507 EXIT_FUNCTION;
2509 return tunnel;
2512 static void pppol2tp_proc_stop(struct seq_file *p, void *v)
2514 struct pppol2tp_tunnel *tunnel = v;
2516 ENTER_FUNCTION;
2518 if (tunnel != NULL)
2519 sock_put(tunnel->sock);
2521 EXIT_FUNCTION;
2524 static int pppol2tp_proc_show(struct seq_file *m, void *v)
2526 struct pppol2tp_tunnel *tunnel = v;
2527 struct pppol2tp_session *session;
2528 struct hlist_node *walk;
2529 struct hlist_node *tmp;
2530 int i;
2532 ENTER_FUNCTION;
2534 /* display header on line 1 */
2535 if (v == (void *)1) {
2536 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2537 seq_puts(m, "TUNNEL name, user-data-ok "
2538 "session-count magic-ok\n");
2539 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2540 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2541 "dest-tid/sid state user-data-ok magic-ok\n");
2542 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2543 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2544 goto out;
2547 seq_printf(m, "TUNNEL '%s', %c %d MAGIC %s\n",
2548 tunnel->name,
2549 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2550 atomic_read(&tunnel->session_count),
2551 (tunnel->magic == L2TP_TUNNEL_MAGIC) ? "OK" : "BAD");
2552 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2553 tunnel->debug,
2554 tunnel->stats.tx_packets, tunnel->stats.tx_bytes,
2555 tunnel->stats.tx_errors,
2556 tunnel->stats.rx_packets, tunnel->stats.rx_bytes,
2557 tunnel->stats.rx_errors);
2559 if (tunnel->magic != L2TP_TUNNEL_MAGIC) {
2560 seq_puts(m, "*** Aborting ***\n");
2561 goto out;
2564 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2565 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[i]) {
2566 session = hlist_entry(walk, struct pppol2tp_session, hlist);
2567 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2568 "%04X/%04X %d %c MAGIC %s\n",
2569 session->name,
2570 htonl(session->tunnel_addr.addr.sin_addr.s_addr),
2571 htons(session->tunnel_addr.addr.sin_port),
2572 session->tunnel_addr.s_tunnel,
2573 session->tunnel_addr.s_session,
2574 session->tunnel_addr.d_tunnel,
2575 session->tunnel_addr.d_session,
2576 session->sock->sk_state,
2577 (session == session->sock->sk_user_data) ?
2578 'Y' : 'N',
2579 (session->magic == L2TP_SESSION_MAGIC) ?
2580 "OK" : "BAD");
2582 seq_printf(m, " %d/%d/%c/%c/%s %08x %d\n",
2583 session->mtu, session->mru,
2584 session->recv_seq ? 'R' : '-',
2585 session->send_seq ? 'S' : '-',
2586 session->lns_mode ? "LNS" : "LAC",
2587 session->debug,
2588 JIFFIES_TO_MS(session->reorder_timeout));
2589 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2590 session->nr, session->ns,
2591 session->stats.tx_packets,
2592 session->stats.tx_bytes,
2593 session->stats.tx_errors,
2594 session->stats.rx_packets,
2595 session->stats.rx_bytes,
2596 session->stats.rx_errors);
2598 if (session->magic != L2TP_SESSION_MAGIC) {
2599 seq_puts(m, "*** Aborting ***\n");
2600 goto out;
2604 out:
2605 seq_puts(m, "\n");
2607 EXIT_FUNCTION;
2609 return 0;
2612 #endif /* CONFIG_PROC_FS */
2614 /*****************************************************************************
2615 * Init and cleanup
2616 *****************************************************************************/
2618 static struct proto_ops pppol2tp_ops = {
2619 .family = AF_PPPOX,
2620 .owner = THIS_MODULE,
2621 .release = pppol2tp_release,
2622 .bind = sock_no_bind,
2623 .connect = pppol2tp_connect,
2624 .socketpair = sock_no_socketpair,
2625 .accept = sock_no_accept,
2626 .getname = pppol2tp_getname,
2627 .poll = datagram_poll,
2628 .listen = sock_no_listen,
2629 .shutdown = sock_no_shutdown,
2630 .setsockopt = pppol2tp_setsockopt,
2631 .getsockopt = pppol2tp_getsockopt,
2632 .sendmsg = pppol2tp_sendmsg,
2633 .recvmsg = pppol2tp_recvmsg,
2634 .mmap = sock_no_mmap,
2635 .ioctl = pppox_ioctl,
2638 struct pppox_proto pppol2tp_proto = {
2639 .create = pppol2tp_create,
2640 .ioctl = pppol2tp_ioctl
2643 int __init pppol2tp_init(void)
2645 int err;
2647 err = proto_register(&pppol2tp_sk_proto, 0);
2648 if (err)
2649 goto out;
2650 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2651 if (err)
2652 goto out_unregister_pppol2tp_proto;
2654 #ifdef CONFIG_PROC_FS
2655 pppol2tp_proc = create_proc_entry("pppol2tp", 0, proc_net);
2656 if (!pppol2tp_proc) {
2657 err = -ENOMEM;
2658 goto out_unregister_pppox_proto;
2660 pppol2tp_proc->proc_fops = &pppol2tp_proc_fops;
2661 #endif /* CONFIG_PROC_FS */
2662 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2663 PPPOL2TP_DRV_VERSION);
2665 out:
2666 return err;
2668 out_unregister_pppox_proto:
2669 unregister_pppox_proto(PX_PROTO_OL2TP);
2670 out_unregister_pppol2tp_proto:
2671 proto_unregister(&pppol2tp_sk_proto);
2672 goto out;
2675 void __exit pppol2tp_exit(void)
2677 unregister_pppox_proto(PX_PROTO_OL2TP);
2679 #ifdef CONFIG_PROC_FS
2680 remove_proc_entry("pppol2tp", proc_net);
2681 #endif
2682 proto_unregister(&pppol2tp_sk_proto);
2685 module_init(pppol2tp_init);
2686 module_exit(pppol2tp_exit);
2688 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>");
2689 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2690 MODULE_LICENSE("GPL");
2691 MODULE_VERSION(PPPOL2TP_DRV_VERSION);