lt2p: Fix possible WARN_ON from socket code when UDP socket is closed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / pppol2tp.c
blob04c7e5b407fd8bead24b52d0e851593d00cf2728
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/net_namespace.h>
95 #include <net/dst.h>
96 #include <net/ip.h>
97 #include <net/udp.h>
98 #include <net/xfrm.h>
100 #include <asm/byteorder.h>
101 #include <asm/atomic.h>
104 #define PPPOL2TP_DRV_VERSION "V1.0"
106 /* L2TP header constants */
107 #define L2TP_HDRFLAG_T 0x8000
108 #define L2TP_HDRFLAG_L 0x4000
109 #define L2TP_HDRFLAG_S 0x0800
110 #define L2TP_HDRFLAG_O 0x0200
111 #define L2TP_HDRFLAG_P 0x0100
113 #define L2TP_HDR_VER_MASK 0x000F
114 #define L2TP_HDR_VER 0x0002
116 /* Space for UDP, L2TP and PPP headers */
117 #define PPPOL2TP_HEADER_OVERHEAD 40
119 /* Just some random numbers */
120 #define L2TP_TUNNEL_MAGIC 0x42114DDA
121 #define L2TP_SESSION_MAGIC 0x0C04EB7D
123 #define PPPOL2TP_HASH_BITS 4
124 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
126 /* Default trace flags */
127 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
129 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
130 do { \
131 if ((_mask) & (_type)) \
132 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
133 } while(0)
135 /* Number of bytes to build transmit L2TP headers.
136 * Unfortunately the size is different depending on whether sequence numbers
137 * are enabled.
139 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
140 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
142 struct pppol2tp_tunnel;
144 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
145 * socket. Contains information to determine incoming packets and transmit
146 * outgoing ones.
148 struct pppol2tp_session
150 int magic; /* should be
151 * L2TP_SESSION_MAGIC */
152 int owner; /* pid that opened the socket */
154 struct sock *sock; /* Pointer to the session
155 * PPPoX socket */
156 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
157 * socket */
159 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
161 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
162 * context */
164 char name[20]; /* "sess xxxxx/yyyyy", where
165 * x=tunnel_id, y=session_id */
166 int mtu;
167 int mru;
168 int flags; /* accessed by PPPIOCGFLAGS.
169 * Unused. */
170 unsigned recv_seq:1; /* expect receive packets with
171 * sequence numbers? */
172 unsigned send_seq:1; /* send packets with sequence
173 * numbers? */
174 unsigned lns_mode:1; /* behave as LNS? LAC enables
175 * sequence numbers under
176 * control of LNS. */
177 int debug; /* bitmask of debug message
178 * categories */
179 int reorder_timeout; /* configured reorder timeout
180 * (in jiffies) */
181 u16 nr; /* session NR state (receive) */
182 u16 ns; /* session NR state (send) */
183 struct sk_buff_head reorder_q; /* receive reorder queue */
184 struct pppol2tp_ioc_stats stats;
185 struct hlist_node hlist; /* Hash list node */
188 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
189 * all the associated sessions so incoming packets can be sorted out
191 struct pppol2tp_tunnel
193 int magic; /* Should be L2TP_TUNNEL_MAGIC */
194 rwlock_t hlist_lock; /* protect session_hlist */
195 struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE];
196 /* hashed list of sessions,
197 * hashed by id */
198 int debug; /* bitmask of debug message
199 * categories */
200 char name[12]; /* "tunl xxxxx" */
201 struct pppol2tp_ioc_stats stats;
203 void (*old_sk_destruct)(struct sock *);
205 struct sock *sock; /* Parent socket */
206 struct list_head list; /* Keep a list of all open
207 * prepared sockets */
209 atomic_t ref_count;
212 /* Private data stored for received packets in the skb.
214 struct pppol2tp_skb_cb {
215 u16 ns;
216 u16 nr;
217 u16 has_seq;
218 u16 length;
219 unsigned long expires;
222 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
224 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
225 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel);
227 static atomic_t pppol2tp_tunnel_count;
228 static atomic_t pppol2tp_session_count;
229 static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
230 static struct proto_ops pppol2tp_ops;
231 static LIST_HEAD(pppol2tp_tunnel_list);
232 static DEFINE_RWLOCK(pppol2tp_tunnel_list_lock);
234 /* Helpers to obtain tunnel/session contexts from sockets.
236 static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk)
238 struct pppol2tp_session *session;
240 if (sk == NULL)
241 return NULL;
243 session = (struct pppol2tp_session *)(sk->sk_user_data);
244 if (session == NULL)
245 return NULL;
247 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
249 return session;
252 static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk)
254 struct pppol2tp_tunnel *tunnel;
256 if (sk == NULL)
257 return NULL;
259 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data);
260 if (tunnel == NULL)
261 return NULL;
263 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
265 return tunnel;
268 /* Tunnel reference counts. Incremented per session that is added to
269 * the tunnel.
271 static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel)
273 atomic_inc(&tunnel->ref_count);
276 static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel)
278 if (atomic_dec_and_test(&tunnel->ref_count))
279 pppol2tp_tunnel_free(tunnel);
282 /* Session hash list.
283 * The session_id SHOULD be random according to RFC2661, but several
284 * L2TP implementations (Cisco and Microsoft) use incrementing
285 * session_ids. So we do a real hash on the session_id, rather than a
286 * simple bitmask.
288 static inline struct hlist_head *
289 pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id)
291 unsigned long hash_val = (unsigned long) session_id;
292 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)];
295 /* Lookup a session by id
297 static struct pppol2tp_session *
298 pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id)
300 struct hlist_head *session_list =
301 pppol2tp_session_id_hash(tunnel, session_id);
302 struct pppol2tp_session *session;
303 struct hlist_node *walk;
305 read_lock_bh(&tunnel->hlist_lock);
306 hlist_for_each_entry(session, walk, session_list, hlist) {
307 if (session->tunnel_addr.s_session == session_id) {
308 read_unlock_bh(&tunnel->hlist_lock);
309 return session;
312 read_unlock_bh(&tunnel->hlist_lock);
314 return NULL;
317 /* Lookup a tunnel by id
319 static struct pppol2tp_tunnel *pppol2tp_tunnel_find(u16 tunnel_id)
321 struct pppol2tp_tunnel *tunnel = NULL;
323 read_lock_bh(&pppol2tp_tunnel_list_lock);
324 list_for_each_entry(tunnel, &pppol2tp_tunnel_list, list) {
325 if (tunnel->stats.tunnel_id == tunnel_id) {
326 read_unlock_bh(&pppol2tp_tunnel_list_lock);
327 return tunnel;
330 read_unlock_bh(&pppol2tp_tunnel_list_lock);
332 return NULL;
335 /*****************************************************************************
336 * Receive data handling
337 *****************************************************************************/
339 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
340 * number.
342 static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
344 struct sk_buff *skbp;
345 struct sk_buff *tmp;
346 u16 ns = PPPOL2TP_SKB_CB(skb)->ns;
348 spin_lock_bh(&session->reorder_q.lock);
349 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
350 if (PPPOL2TP_SKB_CB(skbp)->ns > ns) {
351 __skb_insert(skb, skbp->prev, skbp, &session->reorder_q);
352 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
353 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
354 session->name, ns, PPPOL2TP_SKB_CB(skbp)->ns,
355 skb_queue_len(&session->reorder_q));
356 session->stats.rx_oos_packets++;
357 goto out;
361 __skb_queue_tail(&session->reorder_q, skb);
363 out:
364 spin_unlock_bh(&session->reorder_q.lock);
367 /* Dequeue a single skb.
369 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
371 struct pppol2tp_tunnel *tunnel = session->tunnel;
372 int length = PPPOL2TP_SKB_CB(skb)->length;
373 struct sock *session_sock = NULL;
375 /* We're about to requeue the skb, so return resources
376 * to its current owner (a socket receive buffer).
378 skb_orphan(skb);
380 tunnel->stats.rx_packets++;
381 tunnel->stats.rx_bytes += length;
382 session->stats.rx_packets++;
383 session->stats.rx_bytes += length;
385 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
386 /* Bump our Nr */
387 session->nr++;
388 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
389 "%s: updated nr to %hu\n", session->name, session->nr);
392 /* If the socket is bound, send it in to PPP's input queue. Otherwise
393 * queue it on the session socket.
395 session_sock = session->sock;
396 if (session_sock->sk_state & PPPOX_BOUND) {
397 struct pppox_sock *po;
398 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
399 "%s: recv %d byte data frame, passing to ppp\n",
400 session->name, length);
402 /* We need to forget all info related to the L2TP packet
403 * gathered in the skb as we are going to reuse the same
404 * skb for the inner packet.
405 * Namely we need to:
406 * - reset xfrm (IPSec) information as it applies to
407 * the outer L2TP packet and not to the inner one
408 * - release the dst to force a route lookup on the inner
409 * IP packet since skb->dst currently points to the dst
410 * of the UDP tunnel
411 * - reset netfilter information as it doesn't apply
412 * to the inner packet either
414 secpath_reset(skb);
415 dst_release(skb->dst);
416 skb->dst = NULL;
417 nf_reset(skb);
419 po = pppox_sk(session_sock);
420 ppp_input(&po->chan, skb);
421 } else {
422 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
423 "%s: socket not bound\n", session->name);
425 /* Not bound. Nothing we can do, so discard. */
426 session->stats.rx_errors++;
427 kfree_skb(skb);
430 sock_put(session->sock);
433 /* Dequeue skbs from the session's reorder_q, subject to packet order.
434 * Skbs that have been in the queue for too long are simply discarded.
436 static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
438 struct sk_buff *skb;
439 struct sk_buff *tmp;
441 /* If the pkt at the head of the queue has the nr that we
442 * expect to send up next, dequeue it and any other
443 * in-sequence packets behind it.
445 spin_lock_bh(&session->reorder_q.lock);
446 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
447 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
448 session->stats.rx_seq_discards++;
449 session->stats.rx_errors++;
450 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
451 "%s: oos pkt %hu len %d discarded (too old), "
452 "waiting for %hu, reorder_q_len=%d\n",
453 session->name, PPPOL2TP_SKB_CB(skb)->ns,
454 PPPOL2TP_SKB_CB(skb)->length, session->nr,
455 skb_queue_len(&session->reorder_q));
456 __skb_unlink(skb, &session->reorder_q);
457 kfree_skb(skb);
458 sock_put(session->sock);
459 continue;
462 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
463 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
464 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
465 "%s: holding oos pkt %hu len %d, "
466 "waiting for %hu, reorder_q_len=%d\n",
467 session->name, PPPOL2TP_SKB_CB(skb)->ns,
468 PPPOL2TP_SKB_CB(skb)->length, session->nr,
469 skb_queue_len(&session->reorder_q));
470 goto out;
473 __skb_unlink(skb, &session->reorder_q);
475 /* Process the skb. We release the queue lock while we
476 * do so to let other contexts process the queue.
478 spin_unlock_bh(&session->reorder_q.lock);
479 pppol2tp_recv_dequeue_skb(session, skb);
480 spin_lock_bh(&session->reorder_q.lock);
483 out:
484 spin_unlock_bh(&session->reorder_q.lock);
487 /* Internal receive frame. Do the real work of receiving an L2TP data frame
488 * here. The skb is not on a list when we get here.
489 * Returns 0 if the packet was a data packet and was successfully passed on.
490 * Returns 1 if the packet was not a good data packet and could not be
491 * forwarded. All such packets are passed up to userspace to deal with.
493 static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
495 struct pppol2tp_session *session = NULL;
496 struct pppol2tp_tunnel *tunnel;
497 unsigned char *ptr, *optr;
498 u16 hdrflags;
499 u16 tunnel_id, session_id;
500 int length;
501 int offset;
503 tunnel = pppol2tp_sock_to_tunnel(sock);
504 if (tunnel == NULL)
505 goto no_tunnel;
507 /* UDP always verifies the packet length. */
508 __skb_pull(skb, sizeof(struct udphdr));
510 /* Short packet? */
511 if (!pskb_may_pull(skb, 12)) {
512 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
513 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
514 goto error;
517 /* Point to L2TP header */
518 optr = ptr = skb->data;
520 /* Get L2TP header flags */
521 hdrflags = ntohs(*(__be16*)ptr);
523 /* Trace packet contents, if enabled */
524 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
525 length = min(16u, skb->len);
526 if (!pskb_may_pull(skb, length))
527 goto error;
529 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
531 offset = 0;
532 do {
533 printk(" %02X", ptr[offset]);
534 } while (++offset < length);
536 printk("\n");
539 /* Get length of L2TP packet */
540 length = skb->len;
542 /* If type is control packet, it is handled by userspace. */
543 if (hdrflags & L2TP_HDRFLAG_T) {
544 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
545 "%s: recv control packet, len=%d\n", tunnel->name, length);
546 goto error;
549 /* Skip flags */
550 ptr += 2;
552 /* If length is present, skip it */
553 if (hdrflags & L2TP_HDRFLAG_L)
554 ptr += 2;
556 /* Extract tunnel and session ID */
557 tunnel_id = ntohs(*(__be16 *) ptr);
558 ptr += 2;
559 session_id = ntohs(*(__be16 *) ptr);
560 ptr += 2;
562 /* Find the session context */
563 session = pppol2tp_session_find(tunnel, session_id);
564 if (!session) {
565 /* Not found? Pass to userspace to deal with */
566 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
567 "%s: no socket found (%hu/%hu). Passing up.\n",
568 tunnel->name, tunnel_id, session_id);
569 goto error;
571 sock_hold(session->sock);
573 /* The ref count on the socket was increased by the above call since
574 * we now hold a pointer to the session. Take care to do sock_put()
575 * when exiting this function from now on...
578 /* Handle the optional sequence numbers. If we are the LAC,
579 * enable/disable sequence numbers under the control of the LNS. If
580 * no sequence numbers present but we were expecting them, discard
581 * frame.
583 if (hdrflags & L2TP_HDRFLAG_S) {
584 u16 ns, nr;
585 ns = ntohs(*(__be16 *) ptr);
586 ptr += 2;
587 nr = ntohs(*(__be16 *) ptr);
588 ptr += 2;
590 /* Received a packet with sequence numbers. If we're the LNS,
591 * check if we sre sending sequence numbers and if not,
592 * configure it so.
594 if ((!session->lns_mode) && (!session->send_seq)) {
595 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
596 "%s: requested to enable seq numbers by LNS\n",
597 session->name);
598 session->send_seq = -1;
601 /* Store L2TP info in the skb */
602 PPPOL2TP_SKB_CB(skb)->ns = ns;
603 PPPOL2TP_SKB_CB(skb)->nr = nr;
604 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
606 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
607 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
608 session->name, ns, nr, session->nr);
609 } else {
610 /* No sequence numbers.
611 * If user has configured mandatory sequence numbers, discard.
613 if (session->recv_seq) {
614 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
615 "%s: recv data has no seq numbers when required. "
616 "Discarding\n", session->name);
617 session->stats.rx_seq_discards++;
618 goto discard;
621 /* If we're the LAC and we're sending sequence numbers, the
622 * LNS has requested that we no longer send sequence numbers.
623 * If we're the LNS and we're sending sequence numbers, the
624 * LAC is broken. Discard the frame.
626 if ((!session->lns_mode) && (session->send_seq)) {
627 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
628 "%s: requested to disable seq numbers by LNS\n",
629 session->name);
630 session->send_seq = 0;
631 } else if (session->send_seq) {
632 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
633 "%s: recv data has no seq numbers when required. "
634 "Discarding\n", session->name);
635 session->stats.rx_seq_discards++;
636 goto discard;
639 /* Store L2TP info in the skb */
640 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
643 /* If offset bit set, skip it. */
644 if (hdrflags & L2TP_HDRFLAG_O) {
645 offset = ntohs(*(__be16 *)ptr);
646 ptr += 2 + offset;
649 offset = ptr - optr;
650 if (!pskb_may_pull(skb, offset))
651 goto discard;
653 __skb_pull(skb, offset);
655 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
656 * don't send the PPP header (PPP header compression enabled), but
657 * other clients can include the header. So we cope with both cases
658 * here. The PPP header is always FF03 when using L2TP.
660 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
661 * the field may be unaligned.
663 if (!pskb_may_pull(skb, 2))
664 goto discard;
666 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
667 skb_pull(skb, 2);
669 /* Prepare skb for adding to the session's reorder_q. Hold
670 * packets for max reorder_timeout or 1 second if not
671 * reordering.
673 PPPOL2TP_SKB_CB(skb)->length = length;
674 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
675 (session->reorder_timeout ? session->reorder_timeout : HZ);
677 /* Add packet to the session's receive queue. Reordering is done here, if
678 * enabled. Saved L2TP protocol info is stored in skb->sb[].
680 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
681 if (session->reorder_timeout != 0) {
682 /* Packet reordering enabled. Add skb to session's
683 * reorder queue, in order of ns.
685 pppol2tp_recv_queue_skb(session, skb);
686 } else {
687 /* Packet reordering disabled. Discard out-of-sequence
688 * packets
690 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
691 session->stats.rx_seq_discards++;
692 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
693 "%s: oos pkt %hu len %d discarded, "
694 "waiting for %hu, reorder_q_len=%d\n",
695 session->name, PPPOL2TP_SKB_CB(skb)->ns,
696 PPPOL2TP_SKB_CB(skb)->length, session->nr,
697 skb_queue_len(&session->reorder_q));
698 goto discard;
700 skb_queue_tail(&session->reorder_q, skb);
702 } else {
703 /* No sequence numbers. Add the skb to the tail of the
704 * reorder queue. This ensures that it will be
705 * delivered after all previous sequenced skbs.
707 skb_queue_tail(&session->reorder_q, skb);
710 /* Try to dequeue as many skbs from reorder_q as we can. */
711 pppol2tp_recv_dequeue(session);
713 return 0;
715 discard:
716 session->stats.rx_errors++;
717 kfree_skb(skb);
718 sock_put(session->sock);
720 return 0;
722 error:
723 /* Put UDP header back */
724 __skb_push(skb, sizeof(struct udphdr));
726 no_tunnel:
727 return 1;
730 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
731 * Return codes:
732 * 0 : success.
733 * <0: error
734 * >0: skb should be passed up to userspace as UDP.
736 static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
738 struct pppol2tp_tunnel *tunnel;
740 tunnel = pppol2tp_sock_to_tunnel(sk);
741 if (tunnel == NULL)
742 goto pass_up;
744 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
745 "%s: received %d bytes\n", tunnel->name, skb->len);
747 if (pppol2tp_recv_core(sk, skb))
748 goto pass_up;
750 return 0;
752 pass_up:
753 return 1;
756 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
758 static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
759 struct msghdr *msg, size_t len,
760 int flags)
762 int err;
763 struct sk_buff *skb;
764 struct sock *sk = sock->sk;
766 err = -EIO;
767 if (sk->sk_state & PPPOX_BOUND)
768 goto end;
770 msg->msg_namelen = 0;
772 err = 0;
773 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
774 flags & MSG_DONTWAIT, &err);
775 if (skb) {
776 err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data,
777 skb->len);
778 if (err < 0)
779 goto do_skb_free;
780 err = skb->len;
782 do_skb_free:
783 kfree_skb(skb);
784 end:
785 return err;
788 /************************************************************************
789 * Transmit handling
790 ***********************************************************************/
792 /* Tell how big L2TP headers are for a particular session. This
793 * depends on whether sequence numbers are being used.
795 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
797 if (session->send_seq)
798 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
800 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
803 /* Build an L2TP header for the session into the buffer provided.
805 static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
806 void *buf)
808 __be16 *bufp = buf;
809 u16 flags = L2TP_HDR_VER;
811 if (session->send_seq)
812 flags |= L2TP_HDRFLAG_S;
814 /* Setup L2TP header.
815 * FIXME: Can this ever be unaligned? Is direct dereferencing of
816 * 16-bit header fields safe here for all architectures?
818 *bufp++ = htons(flags);
819 *bufp++ = htons(session->tunnel_addr.d_tunnel);
820 *bufp++ = htons(session->tunnel_addr.d_session);
821 if (session->send_seq) {
822 *bufp++ = htons(session->ns);
823 *bufp++ = 0;
824 session->ns++;
825 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
826 "%s: updated ns to %hu\n", session->name, session->ns);
830 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
831 * when a user application does a sendmsg() on the session socket. L2TP and
832 * PPP headers must be inserted into the user's data.
834 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
835 size_t total_len)
837 static const unsigned char ppph[2] = { 0xff, 0x03 };
838 struct sock *sk = sock->sk;
839 struct inet_sock *inet;
840 __wsum csum = 0;
841 struct sk_buff *skb;
842 int error;
843 int hdr_len;
844 struct pppol2tp_session *session;
845 struct pppol2tp_tunnel *tunnel;
846 struct udphdr *uh;
847 unsigned int len;
849 error = -ENOTCONN;
850 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
851 goto error;
853 /* Get session and tunnel contexts */
854 error = -EBADF;
855 session = pppol2tp_sock_to_session(sk);
856 if (session == NULL)
857 goto error;
859 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
860 if (tunnel == NULL)
861 goto error;
863 /* What header length is configured for this session? */
864 hdr_len = pppol2tp_l2tp_header_len(session);
866 /* Allocate a socket buffer */
867 error = -ENOMEM;
868 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
869 sizeof(struct udphdr) + hdr_len +
870 sizeof(ppph) + total_len,
871 0, GFP_KERNEL);
872 if (!skb)
873 goto error;
875 /* Reserve space for headers. */
876 skb_reserve(skb, NET_SKB_PAD);
877 skb_reset_network_header(skb);
878 skb_reserve(skb, sizeof(struct iphdr));
879 skb_reset_transport_header(skb);
881 /* Build UDP header */
882 inet = inet_sk(session->tunnel_sock);
883 uh = (struct udphdr *) skb->data;
884 uh->source = inet->sport;
885 uh->dest = inet->dport;
886 uh->len = htons(hdr_len + sizeof(ppph) + total_len);
887 uh->check = 0;
888 skb_put(skb, sizeof(struct udphdr));
890 /* Build L2TP header */
891 pppol2tp_build_l2tp_header(session, skb->data);
892 skb_put(skb, hdr_len);
894 /* Add PPP header */
895 skb->data[0] = ppph[0];
896 skb->data[1] = ppph[1];
897 skb_put(skb, 2);
899 /* Copy user data into skb */
900 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
901 if (error < 0) {
902 kfree_skb(skb);
903 goto error;
905 skb_put(skb, total_len);
907 /* Calculate UDP checksum if configured to do so */
908 if (session->tunnel_sock->sk_no_check != UDP_CSUM_NOXMIT)
909 csum = udp_csum_outgoing(sk, skb);
911 /* Debug */
912 if (session->send_seq)
913 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
914 "%s: send %Zd bytes, ns=%hu\n", session->name,
915 total_len, session->ns - 1);
916 else
917 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
918 "%s: send %Zd bytes\n", session->name, total_len);
920 if (session->debug & PPPOL2TP_MSG_DATA) {
921 int i;
922 unsigned char *datap = skb->data;
924 printk(KERN_DEBUG "%s: xmit:", session->name);
925 for (i = 0; i < total_len; i++) {
926 printk(" %02X", *datap++);
927 if (i == 15) {
928 printk(" ...");
929 break;
932 printk("\n");
935 /* Queue the packet to IP for output */
936 len = skb->len;
937 error = ip_queue_xmit(skb, 1);
939 /* Update stats */
940 if (error >= 0) {
941 tunnel->stats.tx_packets++;
942 tunnel->stats.tx_bytes += len;
943 session->stats.tx_packets++;
944 session->stats.tx_bytes += len;
945 } else {
946 tunnel->stats.tx_errors++;
947 session->stats.tx_errors++;
950 error:
951 return error;
954 /* Transmit function called by generic PPP driver. Sends PPP frame
955 * over PPPoL2TP socket.
957 * This is almost the same as pppol2tp_sendmsg(), but rather than
958 * being called with a msghdr from userspace, it is called with a skb
959 * from the kernel.
961 * The supplied skb from ppp doesn't have enough headroom for the
962 * insertion of L2TP, UDP and IP headers so we need to allocate more
963 * headroom in the skb. This will create a cloned skb. But we must be
964 * careful in the error case because the caller will expect to free
965 * the skb it supplied, not our cloned skb. So we take care to always
966 * leave the original skb unfreed if we return an error.
968 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
970 static const u8 ppph[2] = { 0xff, 0x03 };
971 struct sock *sk = (struct sock *) chan->private;
972 struct sock *sk_tun;
973 int hdr_len;
974 struct pppol2tp_session *session;
975 struct pppol2tp_tunnel *tunnel;
976 int rc;
977 int headroom;
978 int data_len = skb->len;
979 struct inet_sock *inet;
980 __wsum csum = 0;
981 struct udphdr *uh;
982 unsigned int len;
983 int old_headroom;
984 int new_headroom;
986 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
987 goto abort;
989 /* Get session and tunnel contexts from the socket */
990 session = pppol2tp_sock_to_session(sk);
991 if (session == NULL)
992 goto abort;
994 sk_tun = session->tunnel_sock;
995 if (sk_tun == NULL)
996 goto abort;
997 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
998 if (tunnel == NULL)
999 goto abort;
1001 /* What header length is configured for this session? */
1002 hdr_len = pppol2tp_l2tp_header_len(session);
1004 /* Check that there's enough headroom in the skb to insert IP,
1005 * UDP and L2TP and PPP headers. If not enough, expand it to
1006 * make room. Adjust truesize.
1008 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1009 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
1010 old_headroom = skb_headroom(skb);
1011 if (skb_cow_head(skb, headroom))
1012 goto abort;
1014 new_headroom = skb_headroom(skb);
1015 skb_orphan(skb);
1016 skb->truesize += new_headroom - old_headroom;
1018 /* Setup PPP header */
1019 __skb_push(skb, sizeof(ppph));
1020 skb->data[0] = ppph[0];
1021 skb->data[1] = ppph[1];
1023 /* Setup L2TP header */
1024 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
1026 /* Setup UDP header */
1027 inet = inet_sk(sk_tun);
1028 __skb_push(skb, sizeof(*uh));
1029 skb_reset_transport_header(skb);
1030 uh = udp_hdr(skb);
1031 uh->source = inet->sport;
1032 uh->dest = inet->dport;
1033 uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len);
1034 uh->check = 0;
1036 /* *BROKEN* Calculate UDP checksum if configured to do so */
1037 if (sk_tun->sk_no_check != UDP_CSUM_NOXMIT)
1038 csum = udp_csum_outgoing(sk_tun, skb);
1040 /* Debug */
1041 if (session->send_seq)
1042 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1043 "%s: send %d bytes, ns=%hu\n", session->name,
1044 data_len, session->ns - 1);
1045 else
1046 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1047 "%s: send %d bytes\n", session->name, data_len);
1049 if (session->debug & PPPOL2TP_MSG_DATA) {
1050 int i;
1051 unsigned char *datap = skb->data;
1053 printk(KERN_DEBUG "%s: xmit:", session->name);
1054 for (i = 0; i < data_len; i++) {
1055 printk(" %02X", *datap++);
1056 if (i == 31) {
1057 printk(" ...");
1058 break;
1061 printk("\n");
1064 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1065 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1066 IPSKB_REROUTED);
1067 nf_reset(skb);
1069 /* Get routing info from the tunnel socket */
1070 dst_release(skb->dst);
1071 skb->dst = dst_clone(__sk_dst_get(sk_tun));
1072 skb->sk = sk_tun;
1074 /* Queue the packet to IP for output */
1075 len = skb->len;
1076 rc = ip_queue_xmit(skb, 1);
1078 /* Update stats */
1079 if (rc >= 0) {
1080 tunnel->stats.tx_packets++;
1081 tunnel->stats.tx_bytes += len;
1082 session->stats.tx_packets++;
1083 session->stats.tx_bytes += len;
1084 } else {
1085 tunnel->stats.tx_errors++;
1086 session->stats.tx_errors++;
1089 return 1;
1091 abort:
1092 /* Free the original skb */
1093 kfree_skb(skb);
1094 return 1;
1097 /*****************************************************************************
1098 * Session (and tunnel control) socket create/destroy.
1099 *****************************************************************************/
1101 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1102 * too.
1104 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1106 int hash;
1107 struct hlist_node *walk;
1108 struct hlist_node *tmp;
1109 struct pppol2tp_session *session;
1110 struct sock *sk;
1112 if (tunnel == NULL)
1113 BUG();
1115 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1116 "%s: closing all sessions...\n", tunnel->name);
1118 write_lock_bh(&tunnel->hlist_lock);
1119 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1120 again:
1121 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1122 struct sk_buff *skb;
1124 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1126 sk = session->sock;
1128 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1129 "%s: closing session\n", session->name);
1131 hlist_del_init(&session->hlist);
1133 /* Since we should hold the sock lock while
1134 * doing any unbinding, we need to release the
1135 * lock we're holding before taking that lock.
1136 * Hold a reference to the sock so it doesn't
1137 * disappear as we're jumping between locks.
1139 sock_hold(sk);
1140 write_unlock_bh(&tunnel->hlist_lock);
1141 lock_sock(sk);
1143 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1144 pppox_unbind_sock(sk);
1145 sk->sk_state = PPPOX_DEAD;
1146 sk->sk_state_change(sk);
1149 /* Purge any queued data */
1150 skb_queue_purge(&sk->sk_receive_queue);
1151 skb_queue_purge(&sk->sk_write_queue);
1152 while ((skb = skb_dequeue(&session->reorder_q))) {
1153 kfree_skb(skb);
1154 sock_put(sk);
1157 release_sock(sk);
1158 sock_put(sk);
1160 /* Now restart from the beginning of this hash
1161 * chain. We always remove a session from the
1162 * list so we are guaranteed to make forward
1163 * progress.
1165 write_lock_bh(&tunnel->hlist_lock);
1166 goto again;
1169 write_unlock_bh(&tunnel->hlist_lock);
1172 /* Really kill the tunnel.
1173 * Come here only when all sessions have been cleared from the tunnel.
1175 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1177 /* Remove from socket list */
1178 write_lock_bh(&pppol2tp_tunnel_list_lock);
1179 list_del_init(&tunnel->list);
1180 write_unlock_bh(&pppol2tp_tunnel_list_lock);
1182 atomic_dec(&pppol2tp_tunnel_count);
1183 kfree(tunnel);
1186 /* Tunnel UDP socket destruct hook.
1187 * The tunnel context is deleted only when all session sockets have been
1188 * closed.
1190 static void pppol2tp_tunnel_destruct(struct sock *sk)
1192 struct pppol2tp_tunnel *tunnel;
1194 tunnel = pppol2tp_sock_to_tunnel(sk);
1195 if (tunnel == NULL)
1196 goto end;
1198 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1199 "%s: closing...\n", tunnel->name);
1201 /* Close all sessions */
1202 pppol2tp_tunnel_closeall(tunnel);
1204 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1205 (udp_sk(sk))->encap_type = 0;
1206 (udp_sk(sk))->encap_rcv = NULL;
1208 /* Remove hooks into tunnel socket */
1209 tunnel->sock = NULL;
1210 sk->sk_destruct = tunnel->old_sk_destruct;
1211 sk->sk_user_data = NULL;
1213 /* Call original (UDP) socket descructor */
1214 if (sk->sk_destruct != NULL)
1215 (*sk->sk_destruct)(sk);
1217 pppol2tp_tunnel_dec_refcount(tunnel);
1219 end:
1220 return;
1223 /* Really kill the session socket. (Called from sock_put() if
1224 * refcnt == 0.)
1226 static void pppol2tp_session_destruct(struct sock *sk)
1228 struct pppol2tp_session *session = NULL;
1230 if (sk->sk_user_data != NULL) {
1231 struct pppol2tp_tunnel *tunnel;
1233 session = pppol2tp_sock_to_session(sk);
1234 if (session == NULL)
1235 goto out;
1237 /* Don't use pppol2tp_sock_to_tunnel() here to
1238 * get the tunnel context because the tunnel
1239 * socket might have already been closed (its
1240 * sk->sk_user_data will be NULL) so use the
1241 * session's private tunnel ptr instead.
1243 tunnel = session->tunnel;
1244 if (tunnel != NULL) {
1245 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1247 /* If session_id is zero, this is a null
1248 * session context, which was created for a
1249 * socket that is being used only to manage
1250 * tunnels.
1252 if (session->tunnel_addr.s_session != 0) {
1253 /* Delete the session socket from the
1254 * hash
1256 write_lock_bh(&tunnel->hlist_lock);
1257 hlist_del_init(&session->hlist);
1258 write_unlock_bh(&tunnel->hlist_lock);
1260 atomic_dec(&pppol2tp_session_count);
1263 /* This will delete the tunnel context if this
1264 * is the last session on the tunnel.
1266 session->tunnel = NULL;
1267 session->tunnel_sock = NULL;
1268 pppol2tp_tunnel_dec_refcount(tunnel);
1272 kfree(session);
1273 out:
1274 return;
1277 /* Called when the PPPoX socket (session) is closed.
1279 static int pppol2tp_release(struct socket *sock)
1281 struct sock *sk = sock->sk;
1282 struct pppol2tp_session *session;
1283 int error;
1285 if (!sk)
1286 return 0;
1288 error = -EBADF;
1289 lock_sock(sk);
1290 if (sock_flag(sk, SOCK_DEAD) != 0)
1291 goto error;
1293 pppox_unbind_sock(sk);
1295 /* Signal the death of the socket. */
1296 sk->sk_state = PPPOX_DEAD;
1297 sock_orphan(sk);
1298 sock->sk = NULL;
1300 session = pppol2tp_sock_to_session(sk);
1302 /* Purge any queued data */
1303 skb_queue_purge(&sk->sk_receive_queue);
1304 skb_queue_purge(&sk->sk_write_queue);
1305 if (session != NULL) {
1306 struct sk_buff *skb;
1307 while ((skb = skb_dequeue(&session->reorder_q))) {
1308 kfree_skb(skb);
1309 sock_put(sk);
1313 release_sock(sk);
1315 /* This will delete the session context via
1316 * pppol2tp_session_destruct() if the socket's refcnt drops to
1317 * zero.
1319 sock_put(sk);
1321 return 0;
1323 error:
1324 release_sock(sk);
1325 return error;
1328 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1329 * sockets attached to it.
1331 static struct sock *pppol2tp_prepare_tunnel_socket(int fd, u16 tunnel_id,
1332 int *error)
1334 int err;
1335 struct socket *sock = NULL;
1336 struct sock *sk;
1337 struct pppol2tp_tunnel *tunnel;
1338 struct sock *ret = NULL;
1340 /* Get the tunnel UDP socket from the fd, which was opened by
1341 * the userspace L2TP daemon.
1343 err = -EBADF;
1344 sock = sockfd_lookup(fd, &err);
1345 if (!sock) {
1346 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1347 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1348 tunnel_id, fd, err);
1349 goto err;
1352 sk = sock->sk;
1354 /* Quick sanity checks */
1355 err = -EPROTONOSUPPORT;
1356 if (sk->sk_protocol != IPPROTO_UDP) {
1357 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1358 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1359 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1360 goto err;
1362 err = -EAFNOSUPPORT;
1363 if (sock->ops->family != AF_INET) {
1364 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1365 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1366 tunnel_id, fd, sock->ops->family, AF_INET);
1367 goto err;
1370 err = -ENOTCONN;
1372 /* Check if this socket has already been prepped */
1373 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1374 if (tunnel != NULL) {
1375 /* User-data field already set */
1376 err = -EBUSY;
1377 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1379 /* This socket has already been prepped */
1380 ret = tunnel->sock;
1381 goto out;
1384 /* This socket is available and needs prepping. Create a new tunnel
1385 * context and init it.
1387 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1388 if (sk->sk_user_data == NULL) {
1389 err = -ENOMEM;
1390 goto err;
1393 tunnel->magic = L2TP_TUNNEL_MAGIC;
1394 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1396 tunnel->stats.tunnel_id = tunnel_id;
1397 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1399 /* Hook on the tunnel socket destructor so that we can cleanup
1400 * if the tunnel socket goes away.
1402 tunnel->old_sk_destruct = sk->sk_destruct;
1403 sk->sk_destruct = &pppol2tp_tunnel_destruct;
1405 tunnel->sock = sk;
1406 sk->sk_allocation = GFP_ATOMIC;
1408 /* Misc init */
1409 rwlock_init(&tunnel->hlist_lock);
1411 /* Add tunnel to our list */
1412 INIT_LIST_HEAD(&tunnel->list);
1413 write_lock_bh(&pppol2tp_tunnel_list_lock);
1414 list_add(&tunnel->list, &pppol2tp_tunnel_list);
1415 write_unlock_bh(&pppol2tp_tunnel_list_lock);
1416 atomic_inc(&pppol2tp_tunnel_count);
1418 /* Bump the reference count. The tunnel context is deleted
1419 * only when this drops to zero.
1421 pppol2tp_tunnel_inc_refcount(tunnel);
1423 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1424 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1425 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1427 ret = tunnel->sock;
1429 *error = 0;
1430 out:
1431 if (sock)
1432 sockfd_put(sock);
1434 return ret;
1436 err:
1437 *error = err;
1438 goto out;
1441 static struct proto pppol2tp_sk_proto = {
1442 .name = "PPPOL2TP",
1443 .owner = THIS_MODULE,
1444 .obj_size = sizeof(struct pppox_sock),
1447 /* socket() handler. Initialize a new struct sock.
1449 static int pppol2tp_create(struct net *net, struct socket *sock)
1451 int error = -ENOMEM;
1452 struct sock *sk;
1454 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
1455 if (!sk)
1456 goto out;
1458 sock_init_data(sock, sk);
1460 sock->state = SS_UNCONNECTED;
1461 sock->ops = &pppol2tp_ops;
1463 sk->sk_backlog_rcv = pppol2tp_recv_core;
1464 sk->sk_protocol = PX_PROTO_OL2TP;
1465 sk->sk_family = PF_PPPOX;
1466 sk->sk_state = PPPOX_NONE;
1467 sk->sk_type = SOCK_STREAM;
1468 sk->sk_destruct = pppol2tp_session_destruct;
1470 error = 0;
1472 out:
1473 return error;
1476 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1478 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1479 int sockaddr_len, int flags)
1481 struct sock *sk = sock->sk;
1482 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1483 struct pppox_sock *po = pppox_sk(sk);
1484 struct sock *tunnel_sock = NULL;
1485 struct pppol2tp_session *session = NULL;
1486 struct pppol2tp_tunnel *tunnel;
1487 struct dst_entry *dst;
1488 int error = 0;
1490 lock_sock(sk);
1492 error = -EINVAL;
1493 if (sp->sa_protocol != PX_PROTO_OL2TP)
1494 goto end;
1496 /* Check for already bound sockets */
1497 error = -EBUSY;
1498 if (sk->sk_state & PPPOX_CONNECTED)
1499 goto end;
1501 /* We don't supporting rebinding anyway */
1502 error = -EALREADY;
1503 if (sk->sk_user_data)
1504 goto end; /* socket is already attached */
1506 /* Don't bind if s_tunnel is 0 */
1507 error = -EINVAL;
1508 if (sp->pppol2tp.s_tunnel == 0)
1509 goto end;
1511 /* Special case: prepare tunnel socket if s_session and
1512 * d_session is 0. Otherwise look up tunnel using supplied
1513 * tunnel id.
1515 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
1516 tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.fd,
1517 sp->pppol2tp.s_tunnel,
1518 &error);
1519 if (tunnel_sock == NULL)
1520 goto end;
1522 tunnel = tunnel_sock->sk_user_data;
1523 } else {
1524 tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel);
1526 /* Error if we can't find the tunnel */
1527 error = -ENOENT;
1528 if (tunnel == NULL)
1529 goto end;
1531 tunnel_sock = tunnel->sock;
1534 /* Check that this session doesn't already exist */
1535 error = -EEXIST;
1536 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1537 if (session != NULL)
1538 goto end;
1540 /* Allocate and initialize a new session context. */
1541 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1542 if (session == NULL) {
1543 error = -ENOMEM;
1544 goto end;
1547 skb_queue_head_init(&session->reorder_q);
1549 session->magic = L2TP_SESSION_MAGIC;
1550 session->owner = current->pid;
1551 session->sock = sk;
1552 session->tunnel = tunnel;
1553 session->tunnel_sock = tunnel_sock;
1554 session->tunnel_addr = sp->pppol2tp;
1555 sprintf(&session->name[0], "sess %hu/%hu",
1556 session->tunnel_addr.s_tunnel,
1557 session->tunnel_addr.s_session);
1559 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1560 session->stats.session_id = session->tunnel_addr.s_session;
1562 INIT_HLIST_NODE(&session->hlist);
1564 /* Inherit debug options from tunnel */
1565 session->debug = tunnel->debug;
1567 /* Default MTU must allow space for UDP/L2TP/PPP
1568 * headers.
1570 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1572 /* If PMTU discovery was enabled, use the MTU that was discovered */
1573 dst = sk_dst_get(sk);
1574 if (dst != NULL) {
1575 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1576 if (pmtu != 0)
1577 session->mtu = session->mru = pmtu -
1578 PPPOL2TP_HEADER_OVERHEAD;
1579 dst_release(dst);
1582 /* Special case: if source & dest session_id == 0x0000, this socket is
1583 * being created to manage the tunnel. Don't add the session to the
1584 * session hash list, just set up the internal context for use by
1585 * ioctl() and sockopt() handlers.
1587 if ((session->tunnel_addr.s_session == 0) &&
1588 (session->tunnel_addr.d_session == 0)) {
1589 error = 0;
1590 sk->sk_user_data = session;
1591 goto out_no_ppp;
1594 /* Get tunnel context from the tunnel socket */
1595 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1596 if (tunnel == NULL) {
1597 error = -EBADF;
1598 goto end;
1601 /* Right now, because we don't have a way to push the incoming skb's
1602 * straight through the UDP layer, the only header we need to worry
1603 * about is the L2TP header. This size is different depending on
1604 * whether sequence numbers are enabled for the data channel.
1606 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1608 po->chan.private = sk;
1609 po->chan.ops = &pppol2tp_chan_ops;
1610 po->chan.mtu = session->mtu;
1612 error = ppp_register_channel(&po->chan);
1613 if (error)
1614 goto end;
1616 /* This is how we get the session context from the socket. */
1617 sk->sk_user_data = session;
1619 /* Add session to the tunnel's hash list */
1620 write_lock_bh(&tunnel->hlist_lock);
1621 hlist_add_head(&session->hlist,
1622 pppol2tp_session_id_hash(tunnel,
1623 session->tunnel_addr.s_session));
1624 write_unlock_bh(&tunnel->hlist_lock);
1626 atomic_inc(&pppol2tp_session_count);
1628 out_no_ppp:
1629 pppol2tp_tunnel_inc_refcount(tunnel);
1630 sk->sk_state = PPPOX_CONNECTED;
1631 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1632 "%s: created\n", session->name);
1634 end:
1635 release_sock(sk);
1637 if (error != 0) {
1638 if (session)
1639 PRINTK(session->debug,
1640 PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1641 "%s: connect failed: %d\n",
1642 session->name, error);
1643 else
1644 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1645 "connect failed: %d\n", error);
1648 return error;
1651 /* getname() support.
1653 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1654 int *usockaddr_len, int peer)
1656 int len = sizeof(struct sockaddr_pppol2tp);
1657 struct sockaddr_pppol2tp sp;
1658 int error = 0;
1659 struct pppol2tp_session *session;
1661 error = -ENOTCONN;
1662 if (sock->sk->sk_state != PPPOX_CONNECTED)
1663 goto end;
1665 session = pppol2tp_sock_to_session(sock->sk);
1666 if (session == NULL) {
1667 error = -EBADF;
1668 goto end;
1671 sp.sa_family = AF_PPPOX;
1672 sp.sa_protocol = PX_PROTO_OL2TP;
1673 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1674 sizeof(struct pppol2tp_addr));
1676 memcpy(uaddr, &sp, len);
1678 *usockaddr_len = len;
1680 error = 0;
1682 end:
1683 return error;
1686 /****************************************************************************
1687 * ioctl() handlers.
1689 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1690 * sockets. However, in order to control kernel tunnel features, we allow
1691 * userspace to create a special "tunnel" PPPoX socket which is used for
1692 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1693 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1694 * calls.
1695 ****************************************************************************/
1697 /* Session ioctl helper.
1699 static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1700 unsigned int cmd, unsigned long arg)
1702 struct ifreq ifr;
1703 int err = 0;
1704 struct sock *sk = session->sock;
1705 int val = (int) arg;
1707 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1708 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1709 session->name, cmd, arg);
1711 sock_hold(sk);
1713 switch (cmd) {
1714 case SIOCGIFMTU:
1715 err = -ENXIO;
1716 if (!(sk->sk_state & PPPOX_CONNECTED))
1717 break;
1719 err = -EFAULT;
1720 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1721 break;
1722 ifr.ifr_mtu = session->mtu;
1723 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1724 break;
1726 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1727 "%s: get mtu=%d\n", session->name, session->mtu);
1728 err = 0;
1729 break;
1731 case SIOCSIFMTU:
1732 err = -ENXIO;
1733 if (!(sk->sk_state & PPPOX_CONNECTED))
1734 break;
1736 err = -EFAULT;
1737 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1738 break;
1740 session->mtu = ifr.ifr_mtu;
1742 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1743 "%s: set mtu=%d\n", session->name, session->mtu);
1744 err = 0;
1745 break;
1747 case PPPIOCGMRU:
1748 err = -ENXIO;
1749 if (!(sk->sk_state & PPPOX_CONNECTED))
1750 break;
1752 err = -EFAULT;
1753 if (put_user(session->mru, (int __user *) arg))
1754 break;
1756 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1757 "%s: get mru=%d\n", session->name, session->mru);
1758 err = 0;
1759 break;
1761 case PPPIOCSMRU:
1762 err = -ENXIO;
1763 if (!(sk->sk_state & PPPOX_CONNECTED))
1764 break;
1766 err = -EFAULT;
1767 if (get_user(val,(int __user *) arg))
1768 break;
1770 session->mru = val;
1771 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1772 "%s: set mru=%d\n", session->name, session->mru);
1773 err = 0;
1774 break;
1776 case PPPIOCGFLAGS:
1777 err = -EFAULT;
1778 if (put_user(session->flags, (int __user *) arg))
1779 break;
1781 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1782 "%s: get flags=%d\n", session->name, session->flags);
1783 err = 0;
1784 break;
1786 case PPPIOCSFLAGS:
1787 err = -EFAULT;
1788 if (get_user(val, (int __user *) arg))
1789 break;
1790 session->flags = val;
1791 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1792 "%s: set flags=%d\n", session->name, session->flags);
1793 err = 0;
1794 break;
1796 case PPPIOCGL2TPSTATS:
1797 err = -ENXIO;
1798 if (!(sk->sk_state & PPPOX_CONNECTED))
1799 break;
1801 if (copy_to_user((void __user *) arg, &session->stats,
1802 sizeof(session->stats)))
1803 break;
1804 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1805 "%s: get L2TP stats\n", session->name);
1806 err = 0;
1807 break;
1809 default:
1810 err = -ENOSYS;
1811 break;
1814 sock_put(sk);
1816 return err;
1819 /* Tunnel ioctl helper.
1821 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1822 * specifies a session_id, the session ioctl handler is called. This allows an
1823 * application to retrieve session stats via a tunnel socket.
1825 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1826 unsigned int cmd, unsigned long arg)
1828 int err = 0;
1829 struct sock *sk = tunnel->sock;
1830 struct pppol2tp_ioc_stats stats_req;
1832 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1833 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1834 cmd, arg);
1836 sock_hold(sk);
1838 switch (cmd) {
1839 case PPPIOCGL2TPSTATS:
1840 err = -ENXIO;
1841 if (!(sk->sk_state & PPPOX_CONNECTED))
1842 break;
1844 if (copy_from_user(&stats_req, (void __user *) arg,
1845 sizeof(stats_req))) {
1846 err = -EFAULT;
1847 break;
1849 if (stats_req.session_id != 0) {
1850 /* resend to session ioctl handler */
1851 struct pppol2tp_session *session =
1852 pppol2tp_session_find(tunnel, stats_req.session_id);
1853 if (session != NULL)
1854 err = pppol2tp_session_ioctl(session, cmd, arg);
1855 else
1856 err = -EBADR;
1857 break;
1859 #ifdef CONFIG_XFRM
1860 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1861 #endif
1862 if (copy_to_user((void __user *) arg, &tunnel->stats,
1863 sizeof(tunnel->stats))) {
1864 err = -EFAULT;
1865 break;
1867 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1868 "%s: get L2TP stats\n", tunnel->name);
1869 err = 0;
1870 break;
1872 default:
1873 err = -ENOSYS;
1874 break;
1877 sock_put(sk);
1879 return err;
1882 /* Main ioctl() handler.
1883 * Dispatch to tunnel or session helpers depending on the socket.
1885 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1886 unsigned long arg)
1888 struct sock *sk = sock->sk;
1889 struct pppol2tp_session *session;
1890 struct pppol2tp_tunnel *tunnel;
1891 int err;
1893 if (!sk)
1894 return 0;
1896 err = -EBADF;
1897 if (sock_flag(sk, SOCK_DEAD) != 0)
1898 goto end;
1900 err = -ENOTCONN;
1901 if ((sk->sk_user_data == NULL) ||
1902 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1903 goto end;
1905 /* Get session context from the socket */
1906 err = -EBADF;
1907 session = pppol2tp_sock_to_session(sk);
1908 if (session == NULL)
1909 goto end;
1911 /* Special case: if session's session_id is zero, treat ioctl as a
1912 * tunnel ioctl
1914 if ((session->tunnel_addr.s_session == 0) &&
1915 (session->tunnel_addr.d_session == 0)) {
1916 err = -EBADF;
1917 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
1918 if (tunnel == NULL)
1919 goto end;
1921 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1922 goto end;
1925 err = pppol2tp_session_ioctl(session, cmd, arg);
1927 end:
1928 return err;
1931 /*****************************************************************************
1932 * setsockopt() / getsockopt() support.
1934 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1935 * sockets. In order to control kernel tunnel features, we allow userspace to
1936 * create a special "tunnel" PPPoX socket which is used for control only.
1937 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1938 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1939 *****************************************************************************/
1941 /* Tunnel setsockopt() helper.
1943 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1944 struct pppol2tp_tunnel *tunnel,
1945 int optname, int val)
1947 int err = 0;
1949 switch (optname) {
1950 case PPPOL2TP_SO_DEBUG:
1951 tunnel->debug = val;
1952 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1953 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1954 break;
1956 default:
1957 err = -ENOPROTOOPT;
1958 break;
1961 return err;
1964 /* Session setsockopt helper.
1966 static int pppol2tp_session_setsockopt(struct sock *sk,
1967 struct pppol2tp_session *session,
1968 int optname, int val)
1970 int err = 0;
1972 switch (optname) {
1973 case PPPOL2TP_SO_RECVSEQ:
1974 if ((val != 0) && (val != 1)) {
1975 err = -EINVAL;
1976 break;
1978 session->recv_seq = val ? -1 : 0;
1979 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1980 "%s: set recv_seq=%d\n", session->name,
1981 session->recv_seq);
1982 break;
1984 case PPPOL2TP_SO_SENDSEQ:
1985 if ((val != 0) && (val != 1)) {
1986 err = -EINVAL;
1987 break;
1989 session->send_seq = val ? -1 : 0;
1991 struct sock *ssk = session->sock;
1992 struct pppox_sock *po = pppox_sk(ssk);
1993 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1994 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1996 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1997 "%s: set send_seq=%d\n", session->name, session->send_seq);
1998 break;
2000 case PPPOL2TP_SO_LNSMODE:
2001 if ((val != 0) && (val != 1)) {
2002 err = -EINVAL;
2003 break;
2005 session->lns_mode = val ? -1 : 0;
2006 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2007 "%s: set lns_mode=%d\n", session->name,
2008 session->lns_mode);
2009 break;
2011 case PPPOL2TP_SO_DEBUG:
2012 session->debug = val;
2013 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2014 "%s: set debug=%x\n", session->name, session->debug);
2015 break;
2017 case PPPOL2TP_SO_REORDERTO:
2018 session->reorder_timeout = msecs_to_jiffies(val);
2019 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2020 "%s: set reorder_timeout=%d\n", session->name,
2021 session->reorder_timeout);
2022 break;
2024 default:
2025 err = -ENOPROTOOPT;
2026 break;
2029 return err;
2032 /* Main setsockopt() entry point.
2033 * Does API checks, then calls either the tunnel or session setsockopt
2034 * handler, according to whether the PPPoL2TP socket is a for a regular
2035 * session or the special tunnel type.
2037 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
2038 char __user *optval, int optlen)
2040 struct sock *sk = sock->sk;
2041 struct pppol2tp_session *session = sk->sk_user_data;
2042 struct pppol2tp_tunnel *tunnel;
2043 int val;
2044 int err;
2046 if (level != SOL_PPPOL2TP)
2047 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2049 if (optlen < sizeof(int))
2050 return -EINVAL;
2052 if (get_user(val, (int __user *)optval))
2053 return -EFAULT;
2055 err = -ENOTCONN;
2056 if (sk->sk_user_data == NULL)
2057 goto end;
2059 /* Get session context from the socket */
2060 err = -EBADF;
2061 session = pppol2tp_sock_to_session(sk);
2062 if (session == NULL)
2063 goto end;
2065 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2067 if ((session->tunnel_addr.s_session == 0) &&
2068 (session->tunnel_addr.d_session == 0)) {
2069 err = -EBADF;
2070 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2071 if (tunnel == NULL)
2072 goto end;
2074 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2075 } else
2076 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2078 err = 0;
2080 end:
2081 return err;
2084 /* Tunnel getsockopt helper. Called with sock locked.
2086 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2087 struct pppol2tp_tunnel *tunnel,
2088 int optname, int *val)
2090 int err = 0;
2092 switch (optname) {
2093 case PPPOL2TP_SO_DEBUG:
2094 *val = tunnel->debug;
2095 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2096 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2097 break;
2099 default:
2100 err = -ENOPROTOOPT;
2101 break;
2104 return err;
2107 /* Session getsockopt helper. Called with sock locked.
2109 static int pppol2tp_session_getsockopt(struct sock *sk,
2110 struct pppol2tp_session *session,
2111 int optname, int *val)
2113 int err = 0;
2115 switch (optname) {
2116 case PPPOL2TP_SO_RECVSEQ:
2117 *val = session->recv_seq;
2118 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2119 "%s: get recv_seq=%d\n", session->name, *val);
2120 break;
2122 case PPPOL2TP_SO_SENDSEQ:
2123 *val = session->send_seq;
2124 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2125 "%s: get send_seq=%d\n", session->name, *val);
2126 break;
2128 case PPPOL2TP_SO_LNSMODE:
2129 *val = session->lns_mode;
2130 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2131 "%s: get lns_mode=%d\n", session->name, *val);
2132 break;
2134 case PPPOL2TP_SO_DEBUG:
2135 *val = session->debug;
2136 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2137 "%s: get debug=%d\n", session->name, *val);
2138 break;
2140 case PPPOL2TP_SO_REORDERTO:
2141 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2142 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2143 "%s: get reorder_timeout=%d\n", session->name, *val);
2144 break;
2146 default:
2147 err = -ENOPROTOOPT;
2150 return err;
2153 /* Main getsockopt() entry point.
2154 * Does API checks, then calls either the tunnel or session getsockopt
2155 * handler, according to whether the PPPoX socket is a for a regular session
2156 * or the special tunnel type.
2158 static int pppol2tp_getsockopt(struct socket *sock, int level,
2159 int optname, char __user *optval, int __user *optlen)
2161 struct sock *sk = sock->sk;
2162 struct pppol2tp_session *session = sk->sk_user_data;
2163 struct pppol2tp_tunnel *tunnel;
2164 int val, len;
2165 int err;
2167 if (level != SOL_PPPOL2TP)
2168 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2170 if (get_user(len, (int __user *) optlen))
2171 return -EFAULT;
2173 len = min_t(unsigned int, len, sizeof(int));
2175 if (len < 0)
2176 return -EINVAL;
2178 err = -ENOTCONN;
2179 if (sk->sk_user_data == NULL)
2180 goto end;
2182 /* Get the session context */
2183 err = -EBADF;
2184 session = pppol2tp_sock_to_session(sk);
2185 if (session == NULL)
2186 goto end;
2188 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2189 if ((session->tunnel_addr.s_session == 0) &&
2190 (session->tunnel_addr.d_session == 0)) {
2191 err = -EBADF;
2192 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2193 if (tunnel == NULL)
2194 goto end;
2196 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2197 } else
2198 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2200 err = -EFAULT;
2201 if (put_user(len, (int __user *) optlen))
2202 goto end;
2204 if (copy_to_user((void __user *) optval, &val, len))
2205 goto end;
2207 err = 0;
2208 end:
2209 return err;
2212 /*****************************************************************************
2213 * /proc filesystem for debug
2214 *****************************************************************************/
2216 #ifdef CONFIG_PROC_FS
2218 #include <linux/seq_file.h>
2220 struct pppol2tp_seq_data {
2221 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2222 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
2225 static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2227 struct pppol2tp_session *session = NULL;
2228 struct hlist_node *walk;
2229 int found = 0;
2230 int next = 0;
2231 int i;
2233 read_lock_bh(&tunnel->hlist_lock);
2234 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2235 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2236 if (curr == NULL) {
2237 found = 1;
2238 goto out;
2240 if (session == curr) {
2241 next = 1;
2242 continue;
2244 if (next) {
2245 found = 1;
2246 goto out;
2250 out:
2251 read_unlock_bh(&tunnel->hlist_lock);
2252 if (!found)
2253 session = NULL;
2255 return session;
2258 static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_tunnel *curr)
2260 struct pppol2tp_tunnel *tunnel = NULL;
2262 read_lock_bh(&pppol2tp_tunnel_list_lock);
2263 if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) {
2264 goto out;
2266 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2267 out:
2268 read_unlock_bh(&pppol2tp_tunnel_list_lock);
2270 return tunnel;
2273 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2275 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
2276 loff_t pos = *offs;
2278 if (!pos)
2279 goto out;
2281 BUG_ON(m->private == NULL);
2282 pd = m->private;
2284 if (pd->tunnel == NULL) {
2285 if (!list_empty(&pppol2tp_tunnel_list))
2286 pd->tunnel = list_entry(pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
2287 } else {
2288 pd->session = next_session(pd->tunnel, pd->session);
2289 if (pd->session == NULL) {
2290 pd->tunnel = next_tunnel(pd->tunnel);
2294 /* NULL tunnel and session indicates end of list */
2295 if ((pd->tunnel == NULL) && (pd->session == NULL))
2296 pd = NULL;
2298 out:
2299 return pd;
2302 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2304 (*pos)++;
2305 return NULL;
2308 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2310 /* nothing to do */
2313 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2315 struct pppol2tp_tunnel *tunnel = v;
2317 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2318 tunnel->name,
2319 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2320 atomic_read(&tunnel->ref_count) - 1);
2321 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2322 tunnel->debug,
2323 (unsigned long long)tunnel->stats.tx_packets,
2324 (unsigned long long)tunnel->stats.tx_bytes,
2325 (unsigned long long)tunnel->stats.tx_errors,
2326 (unsigned long long)tunnel->stats.rx_packets,
2327 (unsigned long long)tunnel->stats.rx_bytes,
2328 (unsigned long long)tunnel->stats.rx_errors);
2331 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2333 struct pppol2tp_session *session = v;
2335 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2336 "%04X/%04X %d %c\n",
2337 session->name,
2338 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2339 ntohs(session->tunnel_addr.addr.sin_port),
2340 session->tunnel_addr.s_tunnel,
2341 session->tunnel_addr.s_session,
2342 session->tunnel_addr.d_tunnel,
2343 session->tunnel_addr.d_session,
2344 session->sock->sk_state,
2345 (session == session->sock->sk_user_data) ?
2346 'Y' : 'N');
2347 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2348 session->mtu, session->mru,
2349 session->recv_seq ? 'R' : '-',
2350 session->send_seq ? 'S' : '-',
2351 session->lns_mode ? "LNS" : "LAC",
2352 session->debug,
2353 jiffies_to_msecs(session->reorder_timeout));
2354 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2355 session->nr, session->ns,
2356 (unsigned long long)session->stats.tx_packets,
2357 (unsigned long long)session->stats.tx_bytes,
2358 (unsigned long long)session->stats.tx_errors,
2359 (unsigned long long)session->stats.rx_packets,
2360 (unsigned long long)session->stats.rx_bytes,
2361 (unsigned long long)session->stats.rx_errors);
2364 static int pppol2tp_seq_show(struct seq_file *m, void *v)
2366 struct pppol2tp_seq_data *pd = v;
2368 /* display header on line 1 */
2369 if (v == SEQ_START_TOKEN) {
2370 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2371 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2372 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2373 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2374 "dest-tid/sid state user-data-ok\n");
2375 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2376 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2377 goto out;
2380 /* Show the tunnel or session context.
2382 if (pd->session == NULL)
2383 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2384 else
2385 pppol2tp_seq_session_show(m, pd->session);
2387 out:
2388 return 0;
2391 static struct seq_operations pppol2tp_seq_ops = {
2392 .start = pppol2tp_seq_start,
2393 .next = pppol2tp_seq_next,
2394 .stop = pppol2tp_seq_stop,
2395 .show = pppol2tp_seq_show,
2398 /* Called when our /proc file is opened. We allocate data for use when
2399 * iterating our tunnel / session contexts and store it in the private
2400 * data of the seq_file.
2402 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2404 struct seq_file *m;
2405 struct pppol2tp_seq_data *pd;
2406 int ret = 0;
2408 ret = seq_open(file, &pppol2tp_seq_ops);
2409 if (ret < 0)
2410 goto out;
2412 m = file->private_data;
2414 /* Allocate and fill our proc_data for access later */
2415 ret = -ENOMEM;
2416 m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL);
2417 if (m->private == NULL)
2418 goto out;
2420 pd = m->private;
2421 ret = 0;
2423 out:
2424 return ret;
2427 /* Called when /proc file access completes.
2429 static int pppol2tp_proc_release(struct inode *inode, struct file *file)
2431 struct seq_file *m = (struct seq_file *)file->private_data;
2433 kfree(m->private);
2434 m->private = NULL;
2436 return seq_release(inode, file);
2439 static struct file_operations pppol2tp_proc_fops = {
2440 .owner = THIS_MODULE,
2441 .open = pppol2tp_proc_open,
2442 .read = seq_read,
2443 .llseek = seq_lseek,
2444 .release = pppol2tp_proc_release,
2447 static struct proc_dir_entry *pppol2tp_proc;
2449 #endif /* CONFIG_PROC_FS */
2451 /*****************************************************************************
2452 * Init and cleanup
2453 *****************************************************************************/
2455 static struct proto_ops pppol2tp_ops = {
2456 .family = AF_PPPOX,
2457 .owner = THIS_MODULE,
2458 .release = pppol2tp_release,
2459 .bind = sock_no_bind,
2460 .connect = pppol2tp_connect,
2461 .socketpair = sock_no_socketpair,
2462 .accept = sock_no_accept,
2463 .getname = pppol2tp_getname,
2464 .poll = datagram_poll,
2465 .listen = sock_no_listen,
2466 .shutdown = sock_no_shutdown,
2467 .setsockopt = pppol2tp_setsockopt,
2468 .getsockopt = pppol2tp_getsockopt,
2469 .sendmsg = pppol2tp_sendmsg,
2470 .recvmsg = pppol2tp_recvmsg,
2471 .mmap = sock_no_mmap,
2472 .ioctl = pppox_ioctl,
2475 static struct pppox_proto pppol2tp_proto = {
2476 .create = pppol2tp_create,
2477 .ioctl = pppol2tp_ioctl
2480 static int __init pppol2tp_init(void)
2482 int err;
2484 err = proto_register(&pppol2tp_sk_proto, 0);
2485 if (err)
2486 goto out;
2487 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2488 if (err)
2489 goto out_unregister_pppol2tp_proto;
2491 #ifdef CONFIG_PROC_FS
2492 pppol2tp_proc = proc_net_fops_create(&init_net, "pppol2tp", 0,
2493 &pppol2tp_proc_fops);
2494 if (!pppol2tp_proc) {
2495 err = -ENOMEM;
2496 goto out_unregister_pppox_proto;
2498 #endif /* CONFIG_PROC_FS */
2499 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2500 PPPOL2TP_DRV_VERSION);
2502 out:
2503 return err;
2504 #ifdef CONFIG_PROC_FS
2505 out_unregister_pppox_proto:
2506 unregister_pppox_proto(PX_PROTO_OL2TP);
2507 #endif
2508 out_unregister_pppol2tp_proto:
2509 proto_unregister(&pppol2tp_sk_proto);
2510 goto out;
2513 static void __exit pppol2tp_exit(void)
2515 unregister_pppox_proto(PX_PROTO_OL2TP);
2517 #ifdef CONFIG_PROC_FS
2518 remove_proc_entry("pppol2tp", init_net.proc_net);
2519 #endif
2520 proto_unregister(&pppol2tp_sk_proto);
2523 module_init(pppol2tp_init);
2524 module_exit(pppol2tp_exit);
2526 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
2527 "James Chapman <jchapman@katalix.com>");
2528 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2529 MODULE_LICENSE("GPL");
2530 MODULE_VERSION(PPPOL2TP_DRV_VERSION);