1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
24 /* This driver handles only L2TP data frames; control frames are handled by a
25 * userspace application.
27 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
28 * attaches it to a bound UDP socket with local tunnel_id / session_id and
29 * peer tunnel_id / session_id set. Data can then be sent or received using
30 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
31 * can be read or modified using ioctl() or [gs]etsockopt() calls.
33 * When a PPPoL2TP socket is connected with local and peer session_id values
34 * zero, the socket is treated as a special tunnel management socket.
36 * Here's example userspace code to create a socket for sending/receiving data
37 * over an L2TP session:-
39 * struct sockaddr_pppol2tp sax;
43 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
45 * sax.sa_family = AF_PPPOX;
46 * sax.sa_protocol = PX_PROTO_OL2TP;
47 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
48 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
49 * sax.pppol2tp.addr.sin_port = addr->sin_port;
50 * sax.pppol2tp.addr.sin_family = AF_INET;
51 * sax.pppol2tp.s_tunnel = tunnel_id;
52 * sax.pppol2tp.s_session = session_id;
53 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
54 * sax.pppol2tp.d_session = peer_session_id;
56 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
58 * A pppd plugin that allows PPP traffic to be carried over L2TP using
59 * this driver is available from the OpenL2TP project at
60 * http://openl2tp.sourceforge.net.
63 #include <linux/module.h>
64 #include <linux/version.h>
65 #include <linux/string.h>
66 #include <linux/list.h>
67 #include <asm/uaccess.h>
69 #include <linux/kernel.h>
70 #include <linux/spinlock.h>
71 #include <linux/kthread.h>
72 #include <linux/sched.h>
73 #include <linux/slab.h>
74 #include <linux/errno.h>
75 #include <linux/jiffies.h>
77 #include <linux/netdevice.h>
78 #include <linux/net.h>
79 #include <linux/inetdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/init.h>
83 #include <linux/udp.h>
84 #include <linux/if_pppox.h>
85 #include <linux/if_pppol2tp.h>
87 #include <linux/ppp_channel.h>
88 #include <linux/ppp_defs.h>
89 #include <linux/if_ppp.h>
90 #include <linux/file.h>
91 #include <linux/hash.h>
92 #include <linux/sort.h>
93 #include <linux/proc_fs.h>
94 #include <net/net_namespace.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...) \
131 if ((_mask) & (_type)) \
132 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
135 /* Number of bytes to build transmit L2TP headers.
136 * Unfortunately the size is different depending on whether sequence numbers
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
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
156 struct sock
*tunnel_sock
; /* Pointer to the tunnel UDP
159 struct pppol2tp_addr tunnel_addr
; /* Description of tunnel */
161 struct pppol2tp_tunnel
*tunnel
; /* back pointer to tunnel
164 char name
[20]; /* "sess xxxxx/yyyyy", where
165 * x=tunnel_id, y=session_id */
168 int flags
; /* accessed by PPPIOCGFLAGS.
170 unsigned recv_seq
:1; /* expect receive packets with
171 * sequence numbers? */
172 unsigned send_seq
:1; /* send packets with sequence
174 unsigned lns_mode
:1; /* behave as LNS? LAC enables
175 * sequence numbers under
177 int debug
; /* bitmask of debug message
179 int reorder_timeout
; /* configured reorder timeout
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,
198 int debug
; /* bitmask of debug message
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 */
212 /* Private data stored for received packets in the skb.
214 struct pppol2tp_skb_cb
{
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
;
243 session
= (struct pppol2tp_session
*)(sk
->sk_user_data
);
247 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
252 static inline struct pppol2tp_tunnel
*pppol2tp_sock_to_tunnel(struct sock
*sk
)
254 struct pppol2tp_tunnel
*tunnel
;
259 tunnel
= (struct pppol2tp_tunnel
*)(sk
->sk_user_data
);
263 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
268 /* Tunnel reference counts. Incremented per session that is added to
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
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 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
306 read_lock(&tunnel
->hlist_lock
);
308 read_lock_bh(&tunnel
->hlist_lock
);
309 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
310 hlist_for_each_entry(session
, walk
, session_list
, hlist
) {
311 if (session
->tunnel_addr
.s_session
== session_id
) {
312 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
313 read_unlock(&tunnel
->hlist_lock
);
315 read_unlock_bh(&tunnel
->hlist_lock
);
316 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
320 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
321 read_unlock(&tunnel
->hlist_lock
);
323 read_unlock_bh(&tunnel
->hlist_lock
);
324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
329 /* Lookup a tunnel by id
331 static struct pppol2tp_tunnel
*pppol2tp_tunnel_find(u16 tunnel_id
)
333 struct pppol2tp_tunnel
*tunnel
= NULL
;
335 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
336 read_lock(&pppol2tp_tunnel_list_lock
);
338 read_lock_bh(&pppol2tp_tunnel_list_lock
);
339 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
340 list_for_each_entry(tunnel
, &pppol2tp_tunnel_list
, list
) {
341 if (tunnel
->stats
.tunnel_id
== tunnel_id
) {
342 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
343 read_unlock(&pppol2tp_tunnel_list_lock
);
345 read_unlock_bh(&pppol2tp_tunnel_list_lock
);
346 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
350 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
351 read_unlock(&pppol2tp_tunnel_list_lock
);
353 read_unlock_bh(&pppol2tp_tunnel_list_lock
);
354 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
359 /*****************************************************************************
360 * Receive data handling
361 *****************************************************************************/
363 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
366 static void pppol2tp_recv_queue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
368 struct sk_buff
*skbp
;
369 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
372 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
373 u16 ns
= PPPOL2TP_SKB_CB(skb
)->ns
;
375 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
376 spin_lock(&session
->reorder_q
.lock
);
377 skb_queue_walk(&session
->reorder_q
, skbp
) {
379 spin_lock_bh(&session
->reorder_q
.lock
);
380 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
381 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
382 if (PPPOL2TP_SKB_CB(skbp
)->ns
> ns
) {
383 __skb_insert(skb
, skbp
->prev
, skbp
, &session
->reorder_q
);
384 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
385 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
386 session
->name
, ns
, PPPOL2TP_SKB_CB(skbp
)->ns
,
387 skb_queue_len(&session
->reorder_q
));
388 session
->stats
.rx_oos_packets
++;
393 __skb_queue_tail(&session
->reorder_q
, skb
);
396 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
397 spin_unlock(&session
->reorder_q
.lock
);
399 spin_unlock_bh(&session
->reorder_q
.lock
);
400 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
403 /* Dequeue a single skb.
405 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
407 struct pppol2tp_tunnel
*tunnel
= session
->tunnel
;
408 int length
= PPPOL2TP_SKB_CB(skb
)->length
;
409 struct sock
*session_sock
= NULL
;
411 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
412 /* We're about to requeue the skb, so unlink it and return resources
414 /* We're about to requeue the skb, so return resources
415 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
416 * to its current owner (a socket receive buffer).
418 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
419 skb_unlink(skb
, &session
->reorder_q
);
421 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
424 tunnel
->stats
.rx_packets
++;
425 tunnel
->stats
.rx_bytes
+= length
;
426 session
->stats
.rx_packets
++;
427 session
->stats
.rx_bytes
+= length
;
429 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
432 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
433 "%s: updated nr to %hu\n", session
->name
, session
->nr
);
436 /* If the socket is bound, send it in to PPP's input queue. Otherwise
437 * queue it on the session socket.
439 session_sock
= session
->sock
;
440 if (session_sock
->sk_state
& PPPOX_BOUND
) {
441 struct pppox_sock
*po
;
442 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
443 "%s: recv %d byte data frame, passing to ppp\n",
444 session
->name
, length
);
446 /* We need to forget all info related to the L2TP packet
447 * gathered in the skb as we are going to reuse the same
448 * skb for the inner packet.
450 * - reset xfrm (IPSec) information as it applies to
451 * the outer L2TP packet and not to the inner one
452 * - release the dst to force a route lookup on the inner
453 * IP packet since skb->dst currently points to the dst
455 * - reset netfilter information as it doesn't apply
456 * to the inner packet either
459 dst_release(skb
->dst
);
463 po
= pppox_sk(session_sock
);
464 ppp_input(&po
->chan
, skb
);
466 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
467 "%s: socket not bound\n", session
->name
);
469 /* Not bound. Nothing we can do, so discard. */
470 session
->stats
.rx_errors
++;
474 sock_put(session
->sock
);
477 /* Dequeue skbs from the session's reorder_q, subject to packet order.
478 * Skbs that have been in the queue for too long are simply discarded.
480 static void pppol2tp_recv_dequeue(struct pppol2tp_session
*session
)
485 /* If the pkt at the head of the queue has the nr that we
486 * expect to send up next, dequeue it and any other
487 * in-sequence packets behind it.
489 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
490 spin_lock(&session
->reorder_q
.lock
);
492 spin_lock_bh(&session
->reorder_q
.lock
);
493 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
494 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
495 if (time_after(jiffies
, PPPOL2TP_SKB_CB(skb
)->expires
)) {
496 session
->stats
.rx_seq_discards
++;
497 session
->stats
.rx_errors
++;
498 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
499 "%s: oos pkt %hu len %d discarded (too old), "
500 "waiting for %hu, reorder_q_len=%d\n",
501 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
502 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
503 skb_queue_len(&session
->reorder_q
));
504 __skb_unlink(skb
, &session
->reorder_q
);
506 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
508 sock_put(session
->sock
);
509 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
513 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
514 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
515 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
516 "%s: holding oos pkt %hu len %d, "
517 "waiting for %hu, reorder_q_len=%d\n",
518 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
519 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
520 skb_queue_len(&session
->reorder_q
));
524 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
525 spin_unlock(&session
->reorder_q
.lock
);
527 __skb_unlink(skb
, &session
->reorder_q
);
529 /* Process the skb. We release the queue lock while we
530 * do so to let other contexts process the queue.
532 spin_unlock_bh(&session
->reorder_q
.lock
);
533 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
534 pppol2tp_recv_dequeue_skb(session
, skb
);
535 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
536 spin_lock(&session
->reorder_q
.lock
);
538 spin_lock_bh(&session
->reorder_q
.lock
);
539 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
543 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
544 spin_unlock(&session
->reorder_q
.lock
);
546 spin_unlock_bh(&session
->reorder_q
.lock
);
547 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
550 /* Internal receive frame. Do the real work of receiving an L2TP data frame
551 * here. The skb is not on a list when we get here.
552 * Returns 0 if the packet was a data packet and was successfully passed on.
553 * Returns 1 if the packet was not a good data packet and could not be
554 * forwarded. All such packets are passed up to userspace to deal with.
556 static int pppol2tp_recv_core(struct sock
*sock
, struct sk_buff
*skb
)
558 struct pppol2tp_session
*session
= NULL
;
559 struct pppol2tp_tunnel
*tunnel
;
560 unsigned char *ptr
, *optr
;
562 u16 tunnel_id
, session_id
;
566 tunnel
= pppol2tp_sock_to_tunnel(sock
);
570 /* UDP always verifies the packet length. */
571 __skb_pull(skb
, sizeof(struct udphdr
));
574 if (!pskb_may_pull(skb
, 12)) {
575 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
576 "%s: recv short packet (len=%d)\n", tunnel
->name
, skb
->len
);
580 /* Point to L2TP header */
581 optr
= ptr
= skb
->data
;
583 /* Get L2TP header flags */
584 hdrflags
= ntohs(*(__be16
*)ptr
);
586 /* Trace packet contents, if enabled */
587 if (tunnel
->debug
& PPPOL2TP_MSG_DATA
) {
588 length
= min(16u, skb
->len
);
589 if (!pskb_may_pull(skb
, length
))
592 printk(KERN_DEBUG
"%s: recv: ", tunnel
->name
);
596 printk(" %02X", ptr
[offset
]);
597 } while (++offset
< length
);
602 /* Get length of L2TP packet */
605 /* If type is control packet, it is handled by userspace. */
606 if (hdrflags
& L2TP_HDRFLAG_T
) {
607 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
608 "%s: recv control packet, len=%d\n", tunnel
->name
, length
);
615 /* If length is present, skip it */
616 if (hdrflags
& L2TP_HDRFLAG_L
)
619 /* Extract tunnel and session ID */
620 tunnel_id
= ntohs(*(__be16
*) ptr
);
622 session_id
= ntohs(*(__be16
*) ptr
);
625 /* Find the session context */
626 session
= pppol2tp_session_find(tunnel
, session_id
);
628 /* Not found? Pass to userspace to deal with */
629 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
630 "%s: no socket found (%hu/%hu). Passing up.\n",
631 tunnel
->name
, tunnel_id
, session_id
);
634 sock_hold(session
->sock
);
636 /* The ref count on the socket was increased by the above call since
637 * we now hold a pointer to the session. Take care to do sock_put()
638 * when exiting this function from now on...
641 /* Handle the optional sequence numbers. If we are the LAC,
642 * enable/disable sequence numbers under the control of the LNS. If
643 * no sequence numbers present but we were expecting them, discard
646 if (hdrflags
& L2TP_HDRFLAG_S
) {
648 ns
= ntohs(*(__be16
*) ptr
);
650 nr
= ntohs(*(__be16
*) ptr
);
653 /* Received a packet with sequence numbers. If we're the LNS,
654 * check if we sre sending sequence numbers and if not,
657 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
658 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
659 "%s: requested to enable seq numbers by LNS\n",
661 session
->send_seq
= -1;
664 /* Store L2TP info in the skb */
665 PPPOL2TP_SKB_CB(skb
)->ns
= ns
;
666 PPPOL2TP_SKB_CB(skb
)->nr
= nr
;
667 PPPOL2TP_SKB_CB(skb
)->has_seq
= 1;
669 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
670 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
671 session
->name
, ns
, nr
, session
->nr
);
673 /* No sequence numbers.
674 * If user has configured mandatory sequence numbers, discard.
676 if (session
->recv_seq
) {
677 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
678 "%s: recv data has no seq numbers when required. "
679 "Discarding\n", session
->name
);
680 session
->stats
.rx_seq_discards
++;
684 /* If we're the LAC and we're sending sequence numbers, the
685 * LNS has requested that we no longer send sequence numbers.
686 * If we're the LNS and we're sending sequence numbers, the
687 * LAC is broken. Discard the frame.
689 if ((!session
->lns_mode
) && (session
->send_seq
)) {
690 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
691 "%s: requested to disable seq numbers by LNS\n",
693 session
->send_seq
= 0;
694 } else if (session
->send_seq
) {
695 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
696 "%s: recv data has no seq numbers when required. "
697 "Discarding\n", session
->name
);
698 session
->stats
.rx_seq_discards
++;
702 /* Store L2TP info in the skb */
703 PPPOL2TP_SKB_CB(skb
)->has_seq
= 0;
706 /* If offset bit set, skip it. */
707 if (hdrflags
& L2TP_HDRFLAG_O
) {
708 offset
= ntohs(*(__be16
*)ptr
);
713 if (!pskb_may_pull(skb
, offset
))
716 __skb_pull(skb
, offset
);
718 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
719 * don't send the PPP header (PPP header compression enabled), but
720 * other clients can include the header. So we cope with both cases
721 * here. The PPP header is always FF03 when using L2TP.
723 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
724 * the field may be unaligned.
726 if (!pskb_may_pull(skb
, 2))
729 if ((skb
->data
[0] == 0xff) && (skb
->data
[1] == 0x03))
732 /* Prepare skb for adding to the session's reorder_q. Hold
733 * packets for max reorder_timeout or 1 second if not
736 PPPOL2TP_SKB_CB(skb
)->length
= length
;
737 PPPOL2TP_SKB_CB(skb
)->expires
= jiffies
+
738 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
740 /* Add packet to the session's receive queue. Reordering is done here, if
741 * enabled. Saved L2TP protocol info is stored in skb->sb[].
743 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
744 if (session
->reorder_timeout
!= 0) {
745 /* Packet reordering enabled. Add skb to session's
746 * reorder queue, in order of ns.
748 pppol2tp_recv_queue_skb(session
, skb
);
750 /* Packet reordering disabled. Discard out-of-sequence
753 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
754 session
->stats
.rx_seq_discards
++;
755 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
756 "%s: oos pkt %hu len %d discarded, "
757 "waiting for %hu, reorder_q_len=%d\n",
758 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
759 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
760 skb_queue_len(&session
->reorder_q
));
763 skb_queue_tail(&session
->reorder_q
, skb
);
766 /* No sequence numbers. Add the skb to the tail of the
767 * reorder queue. This ensures that it will be
768 * delivered after all previous sequenced skbs.
770 skb_queue_tail(&session
->reorder_q
, skb
);
773 /* Try to dequeue as many skbs from reorder_q as we can. */
774 pppol2tp_recv_dequeue(session
);
779 session
->stats
.rx_errors
++;
781 sock_put(session
->sock
);
786 /* Put UDP header back */
787 __skb_push(skb
, sizeof(struct udphdr
));
793 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
797 * >0: skb should be passed up to userspace as UDP.
799 static int pppol2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
801 struct pppol2tp_tunnel
*tunnel
;
803 tunnel
= pppol2tp_sock_to_tunnel(sk
);
807 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
808 "%s: received %d bytes\n", tunnel
->name
, skb
->len
);
810 if (pppol2tp_recv_core(sk
, skb
))
819 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
821 static int pppol2tp_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
822 struct msghdr
*msg
, size_t len
,
827 struct sock
*sk
= sock
->sk
;
830 if (sk
->sk_state
& PPPOX_BOUND
)
833 msg
->msg_namelen
= 0;
836 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
837 flags
& MSG_DONTWAIT
, &err
);
839 err
= memcpy_toiovec(msg
->msg_iov
, (unsigned char *) skb
->data
,
851 /************************************************************************
853 ***********************************************************************/
855 /* Tell how big L2TP headers are for a particular session. This
856 * depends on whether sequence numbers are being used.
858 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session
*session
)
860 if (session
->send_seq
)
861 return PPPOL2TP_L2TP_HDR_SIZE_SEQ
;
863 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
866 /* Build an L2TP header for the session into the buffer provided.
868 static void pppol2tp_build_l2tp_header(struct pppol2tp_session
*session
,
872 u16 flags
= L2TP_HDR_VER
;
874 if (session
->send_seq
)
875 flags
|= L2TP_HDRFLAG_S
;
877 /* Setup L2TP header.
878 * FIXME: Can this ever be unaligned? Is direct dereferencing of
879 * 16-bit header fields safe here for all architectures?
881 *bufp
++ = htons(flags
);
882 *bufp
++ = htons(session
->tunnel_addr
.d_tunnel
);
883 *bufp
++ = htons(session
->tunnel_addr
.d_session
);
884 if (session
->send_seq
) {
885 *bufp
++ = htons(session
->ns
);
888 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
889 "%s: updated ns to %hu\n", session
->name
, session
->ns
);
893 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
894 * when a user application does a sendmsg() on the session socket. L2TP and
895 * PPP headers must be inserted into the user's data.
897 static int pppol2tp_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*m
,
900 static const unsigned char ppph
[2] = { 0xff, 0x03 };
901 struct sock
*sk
= sock
->sk
;
902 struct inet_sock
*inet
;
907 struct pppol2tp_session
*session
;
908 struct pppol2tp_tunnel
*tunnel
;
913 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
))
916 /* Get session and tunnel contexts */
918 session
= pppol2tp_sock_to_session(sk
);
922 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
926 /* What header length is configured for this session? */
927 hdr_len
= pppol2tp_l2tp_header_len(session
);
929 /* Allocate a socket buffer */
931 skb
= sock_wmalloc(sk
, NET_SKB_PAD
+ sizeof(struct iphdr
) +
932 sizeof(struct udphdr
) + hdr_len
+
933 sizeof(ppph
) + total_len
,
938 /* Reserve space for headers. */
939 skb_reserve(skb
, NET_SKB_PAD
);
940 skb_reset_network_header(skb
);
941 skb_reserve(skb
, sizeof(struct iphdr
));
942 skb_reset_transport_header(skb
);
944 /* Build UDP header */
945 inet
= inet_sk(session
->tunnel_sock
);
946 uh
= (struct udphdr
*) skb
->data
;
947 uh
->source
= inet
->sport
;
948 uh
->dest
= inet
->dport
;
949 uh
->len
= htons(hdr_len
+ sizeof(ppph
) + total_len
);
951 skb_put(skb
, sizeof(struct udphdr
));
953 /* Build L2TP header */
954 pppol2tp_build_l2tp_header(session
, skb
->data
);
955 skb_put(skb
, hdr_len
);
958 skb
->data
[0] = ppph
[0];
959 skb
->data
[1] = ppph
[1];
962 /* Copy user data into skb */
963 error
= memcpy_fromiovec(skb
->data
, m
->msg_iov
, total_len
);
968 skb_put(skb
, total_len
);
970 /* Calculate UDP checksum if configured to do so */
971 if (session
->tunnel_sock
->sk_no_check
!= UDP_CSUM_NOXMIT
)
972 csum
= udp_csum_outgoing(sk
, skb
);
975 if (session
->send_seq
)
976 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
977 "%s: send %Zd bytes, ns=%hu\n", session
->name
,
978 total_len
, session
->ns
- 1);
980 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
981 "%s: send %Zd bytes\n", session
->name
, total_len
);
983 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
985 unsigned char *datap
= skb
->data
;
987 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
988 for (i
= 0; i
< total_len
; i
++) {
989 printk(" %02X", *datap
++);
998 /* Queue the packet to IP for output */
1000 error
= ip_queue_xmit(skb
, 1);
1004 tunnel
->stats
.tx_packets
++;
1005 tunnel
->stats
.tx_bytes
+= len
;
1006 session
->stats
.tx_packets
++;
1007 session
->stats
.tx_bytes
+= len
;
1009 tunnel
->stats
.tx_errors
++;
1010 session
->stats
.tx_errors
++;
1017 /* Transmit function called by generic PPP driver. Sends PPP frame
1018 * over PPPoL2TP socket.
1020 * This is almost the same as pppol2tp_sendmsg(), but rather than
1021 * being called with a msghdr from userspace, it is called with a skb
1024 * The supplied skb from ppp doesn't have enough headroom for the
1025 * insertion of L2TP, UDP and IP headers so we need to allocate more
1026 * headroom in the skb. This will create a cloned skb. But we must be
1027 * careful in the error case because the caller will expect to free
1028 * the skb it supplied, not our cloned skb. So we take care to always
1029 * leave the original skb unfreed if we return an error.
1031 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1033 static const u8 ppph
[2] = { 0xff, 0x03 };
1034 struct sock
*sk
= (struct sock
*) chan
->private;
1035 struct sock
*sk_tun
;
1037 struct pppol2tp_session
*session
;
1038 struct pppol2tp_tunnel
*tunnel
;
1041 int data_len
= skb
->len
;
1042 struct inet_sock
*inet
;
1047 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
))
1050 /* Get session and tunnel contexts from the socket */
1051 session
= pppol2tp_sock_to_session(sk
);
1052 if (session
== NULL
)
1055 sk_tun
= session
->tunnel_sock
;
1058 tunnel
= pppol2tp_sock_to_tunnel(sk_tun
);
1062 /* What header length is configured for this session? */
1063 hdr_len
= pppol2tp_l2tp_header_len(session
);
1065 /* Check that there's enough headroom in the skb to insert IP,
1066 * UDP and L2TP and PPP headers. If not enough, expand it to
1067 * make room. Note that a new skb (or a clone) is
1068 * allocated. If we return an error from this point on, make
1069 * sure we free the new skb but do not free the original skb
1070 * since that is done by the caller for the error case.
1072 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1073 sizeof(struct udphdr
) + hdr_len
+ sizeof(ppph
);
1074 if (skb_cow_head(skb
, headroom
))
1077 /* Setup PPP header */
1078 __skb_push(skb
, sizeof(ppph
));
1079 skb
->data
[0] = ppph
[0];
1080 skb
->data
[1] = ppph
[1];
1082 /* Setup L2TP header */
1083 pppol2tp_build_l2tp_header(session
, __skb_push(skb
, hdr_len
));
1085 /* Setup UDP header */
1086 inet
= inet_sk(sk_tun
);
1087 __skb_push(skb
, sizeof(*uh
));
1088 skb_reset_transport_header(skb
);
1090 uh
->source
= inet
->sport
;
1091 uh
->dest
= inet
->dport
;
1092 uh
->len
= htons(sizeof(struct udphdr
) + hdr_len
+ sizeof(ppph
) + data_len
);
1095 /* *BROKEN* Calculate UDP checksum if configured to do so */
1096 if (sk_tun
->sk_no_check
!= UDP_CSUM_NOXMIT
)
1097 csum
= udp_csum_outgoing(sk_tun
, skb
);
1100 if (session
->send_seq
)
1101 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1102 "%s: send %d bytes, ns=%hu\n", session
->name
,
1103 data_len
, session
->ns
- 1);
1105 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1106 "%s: send %d bytes\n", session
->name
, data_len
);
1108 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
1110 unsigned char *datap
= skb
->data
;
1112 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
1113 for (i
= 0; i
< data_len
; i
++) {
1114 printk(" %02X", *datap
++);
1123 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1124 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1128 /* Get routing info from the tunnel socket */
1129 dst_release(skb
->dst
);
1130 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1131 skb
->dst
= sk_dst_get(sk_tun
);
1133 skb
->dst
= dst_clone(__sk_dst_get(sk_tun
));
1134 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1138 /* Queue the packet to IP for output */
1140 rc
= ip_queue_xmit(skb
, 1);
1144 tunnel
->stats
.tx_packets
++;
1145 tunnel
->stats
.tx_bytes
+= len
;
1146 session
->stats
.tx_packets
++;
1147 session
->stats
.tx_bytes
+= len
;
1149 tunnel
->stats
.tx_errors
++;
1150 session
->stats
.tx_errors
++;
1156 /* Free the original skb */
1161 /*****************************************************************************
1162 * Session (and tunnel control) socket create/destroy.
1163 *****************************************************************************/
1165 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1168 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel
*tunnel
)
1171 struct hlist_node
*walk
;
1172 struct hlist_node
*tmp
;
1173 struct pppol2tp_session
*session
;
1179 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1180 "%s: closing all sessions...\n", tunnel
->name
);
1182 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1183 write_lock(&tunnel
->hlist_lock
);
1185 write_lock_bh(&tunnel
->hlist_lock
);
1186 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1187 for (hash
= 0; hash
< PPPOL2TP_HASH_SIZE
; hash
++) {
1189 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1190 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1192 struct sk_buff
*skb
;
1194 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1195 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
1199 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1200 "%s: closing session\n", session
->name
);
1202 hlist_del_init(&session
->hlist
);
1204 /* Since we should hold the sock lock while
1205 * doing any unbinding, we need to release the
1206 * lock we're holding before taking that lock.
1207 * Hold a reference to the sock so it doesn't
1208 * disappear as we're jumping between locks.
1211 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1212 write_unlock(&tunnel
->hlist_lock
);
1214 write_unlock_bh(&tunnel
->hlist_lock
);
1215 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1218 if (sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)) {
1219 pppox_unbind_sock(sk
);
1220 sk
->sk_state
= PPPOX_DEAD
;
1221 sk
->sk_state_change(sk
);
1224 /* Purge any queued data */
1225 skb_queue_purge(&sk
->sk_receive_queue
);
1226 skb_queue_purge(&sk
->sk_write_queue
);
1227 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1228 skb_queue_purge(&session
->reorder_q
);
1230 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1234 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1239 /* Now restart from the beginning of this hash
1240 * chain. We always remove a session from the
1241 * list so we are guaranteed to make forward
1244 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1245 write_lock(&tunnel
->hlist_lock
);
1247 write_lock_bh(&tunnel
->hlist_lock
);
1248 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1252 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1253 write_unlock(&tunnel
->hlist_lock
);
1255 write_unlock_bh(&tunnel
->hlist_lock
);
1256 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1259 /* Really kill the tunnel.
1260 * Come here only when all sessions have been cleared from the tunnel.
1262 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel
*tunnel
)
1264 /* Remove from socket list */
1265 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1266 write_lock(&pppol2tp_tunnel_list_lock
);
1268 write_lock_bh(&pppol2tp_tunnel_list_lock
);
1269 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1270 list_del_init(&tunnel
->list
);
1271 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1272 write_unlock(&pppol2tp_tunnel_list_lock
);
1274 write_unlock_bh(&pppol2tp_tunnel_list_lock
);
1275 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1277 atomic_dec(&pppol2tp_tunnel_count
);
1281 /* Tunnel UDP socket destruct hook.
1282 * The tunnel context is deleted only when all session sockets have been
1285 static void pppol2tp_tunnel_destruct(struct sock
*sk
)
1287 struct pppol2tp_tunnel
*tunnel
;
1289 tunnel
= pppol2tp_sock_to_tunnel(sk
);
1293 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1294 "%s: closing...\n", tunnel
->name
);
1296 /* Close all sessions */
1297 pppol2tp_tunnel_closeall(tunnel
);
1299 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1300 (udp_sk(sk
))->encap_type
= 0;
1301 (udp_sk(sk
))->encap_rcv
= NULL
;
1303 /* Remove hooks into tunnel socket */
1304 tunnel
->sock
= NULL
;
1305 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1306 sk
->sk_user_data
= NULL
;
1308 /* Call original (UDP) socket descructor */
1309 if (sk
->sk_destruct
!= NULL
)
1310 (*sk
->sk_destruct
)(sk
);
1312 pppol2tp_tunnel_dec_refcount(tunnel
);
1318 /* Really kill the session socket. (Called from sock_put() if
1321 static void pppol2tp_session_destruct(struct sock
*sk
)
1323 struct pppol2tp_session
*session
= NULL
;
1325 if (sk
->sk_user_data
!= NULL
) {
1326 struct pppol2tp_tunnel
*tunnel
;
1328 session
= pppol2tp_sock_to_session(sk
);
1329 if (session
== NULL
)
1332 /* Don't use pppol2tp_sock_to_tunnel() here to
1333 * get the tunnel context because the tunnel
1334 * socket might have already been closed (its
1335 * sk->sk_user_data will be NULL) so use the
1336 * session's private tunnel ptr instead.
1338 tunnel
= session
->tunnel
;
1339 if (tunnel
!= NULL
) {
1340 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1342 /* If session_id is zero, this is a null
1343 * session context, which was created for a
1344 * socket that is being used only to manage
1347 if (session
->tunnel_addr
.s_session
!= 0) {
1348 /* Delete the session socket from the
1351 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1352 write_lock(&tunnel
->hlist_lock
);
1354 write_lock_bh(&tunnel
->hlist_lock
);
1355 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1356 hlist_del_init(&session
->hlist
);
1357 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1358 write_unlock(&tunnel
->hlist_lock
);
1360 write_unlock_bh(&tunnel
->hlist_lock
);
1361 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1363 atomic_dec(&pppol2tp_session_count
);
1366 /* This will delete the tunnel context if this
1367 * is the last session on the tunnel.
1369 session
->tunnel
= NULL
;
1370 session
->tunnel_sock
= NULL
;
1371 pppol2tp_tunnel_dec_refcount(tunnel
);
1380 /* Called when the PPPoX socket (session) is closed.
1382 static int pppol2tp_release(struct socket
*sock
)
1384 struct sock
*sk
= sock
->sk
;
1392 if (sock_flag(sk
, SOCK_DEAD
) != 0)
1395 pppox_unbind_sock(sk
);
1397 /* Signal the death of the socket. */
1398 sk
->sk_state
= PPPOX_DEAD
;
1402 /* Purge any queued data */
1403 skb_queue_purge(&sk
->sk_receive_queue
);
1404 skb_queue_purge(&sk
->sk_write_queue
);
1408 /* This will delete the session context via
1409 * pppol2tp_session_destruct() if the socket's refcnt drops to
1421 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1422 * sockets attached to it.
1424 static struct sock
*pppol2tp_prepare_tunnel_socket(int fd
, u16 tunnel_id
,
1428 struct socket
*sock
= NULL
;
1430 struct pppol2tp_tunnel
*tunnel
;
1431 struct sock
*ret
= NULL
;
1433 /* Get the tunnel UDP socket from the fd, which was opened by
1434 * the userspace L2TP daemon.
1437 sock
= sockfd_lookup(fd
, &err
);
1439 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1440 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1441 tunnel_id
, fd
, err
);
1447 /* Quick sanity checks */
1448 err
= -EPROTONOSUPPORT
;
1449 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1450 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1451 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1452 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1455 err
= -EAFNOSUPPORT
;
1456 if (sock
->ops
->family
!= AF_INET
) {
1457 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1458 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1459 tunnel_id
, fd
, sock
->ops
->family
, AF_INET
);
1465 /* Check if this socket has already been prepped */
1466 tunnel
= (struct pppol2tp_tunnel
*)sk
->sk_user_data
;
1467 if (tunnel
!= NULL
) {
1468 /* User-data field already set */
1470 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1472 /* This socket has already been prepped */
1477 /* This socket is available and needs prepping. Create a new tunnel
1478 * context and init it.
1480 sk
->sk_user_data
= tunnel
= kzalloc(sizeof(struct pppol2tp_tunnel
), GFP_KERNEL
);
1481 if (sk
->sk_user_data
== NULL
) {
1486 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1487 sprintf(&tunnel
->name
[0], "tunl %hu", tunnel_id
);
1489 tunnel
->stats
.tunnel_id
= tunnel_id
;
1490 tunnel
->debug
= PPPOL2TP_DEFAULT_DEBUG_FLAGS
;
1492 /* Hook on the tunnel socket destructor so that we can cleanup
1493 * if the tunnel socket goes away.
1495 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1496 sk
->sk_destruct
= &pppol2tp_tunnel_destruct
;
1499 sk
->sk_allocation
= GFP_ATOMIC
;
1502 rwlock_init(&tunnel
->hlist_lock
);
1504 /* Add tunnel to our list */
1505 INIT_LIST_HEAD(&tunnel
->list
);
1506 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1507 write_lock(&pppol2tp_tunnel_list_lock
);
1509 write_lock_bh(&pppol2tp_tunnel_list_lock
);
1510 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1511 list_add(&tunnel
->list
, &pppol2tp_tunnel_list
);
1512 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1513 write_unlock(&pppol2tp_tunnel_list_lock
);
1515 write_unlock_bh(&pppol2tp_tunnel_list_lock
);
1516 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1517 atomic_inc(&pppol2tp_tunnel_count
);
1519 /* Bump the reference count. The tunnel context is deleted
1520 * only when this drops to zero.
1522 pppol2tp_tunnel_inc_refcount(tunnel
);
1524 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1525 (udp_sk(sk
))->encap_type
= UDP_ENCAP_L2TPINUDP
;
1526 (udp_sk(sk
))->encap_rcv
= pppol2tp_udp_encap_recv
;
1542 static struct proto pppol2tp_sk_proto
= {
1544 .owner
= THIS_MODULE
,
1545 .obj_size
= sizeof(struct pppox_sock
),
1548 /* socket() handler. Initialize a new struct sock.
1550 static int pppol2tp_create(struct net
*net
, struct socket
*sock
)
1552 int error
= -ENOMEM
;
1555 sk
= sk_alloc(net
, PF_PPPOX
, GFP_KERNEL
, &pppol2tp_sk_proto
);
1559 sock_init_data(sock
, sk
);
1561 sock
->state
= SS_UNCONNECTED
;
1562 sock
->ops
= &pppol2tp_ops
;
1564 sk
->sk_backlog_rcv
= pppol2tp_recv_core
;
1565 sk
->sk_protocol
= PX_PROTO_OL2TP
;
1566 sk
->sk_family
= PF_PPPOX
;
1567 sk
->sk_state
= PPPOX_NONE
;
1568 sk
->sk_type
= SOCK_STREAM
;
1569 sk
->sk_destruct
= pppol2tp_session_destruct
;
1577 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1579 static int pppol2tp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
1580 int sockaddr_len
, int flags
)
1582 struct sock
*sk
= sock
->sk
;
1583 struct sockaddr_pppol2tp
*sp
= (struct sockaddr_pppol2tp
*) uservaddr
;
1584 struct pppox_sock
*po
= pppox_sk(sk
);
1585 struct sock
*tunnel_sock
= NULL
;
1586 struct pppol2tp_session
*session
= NULL
;
1587 struct pppol2tp_tunnel
*tunnel
;
1588 struct dst_entry
*dst
;
1594 if (sp
->sa_protocol
!= PX_PROTO_OL2TP
)
1597 /* Check for already bound sockets */
1599 if (sk
->sk_state
& PPPOX_CONNECTED
)
1602 /* We don't supporting rebinding anyway */
1604 if (sk
->sk_user_data
)
1605 goto end
; /* socket is already attached */
1607 /* Don't bind if s_tunnel is 0 */
1609 if (sp
->pppol2tp
.s_tunnel
== 0)
1612 /* Special case: prepare tunnel socket if s_session and
1613 * d_session is 0. Otherwise look up tunnel using supplied
1616 if ((sp
->pppol2tp
.s_session
== 0) && (sp
->pppol2tp
.d_session
== 0)) {
1617 tunnel_sock
= pppol2tp_prepare_tunnel_socket(sp
->pppol2tp
.fd
,
1618 sp
->pppol2tp
.s_tunnel
,
1620 if (tunnel_sock
== NULL
)
1623 tunnel
= tunnel_sock
->sk_user_data
;
1625 tunnel
= pppol2tp_tunnel_find(sp
->pppol2tp
.s_tunnel
);
1627 /* Error if we can't find the tunnel */
1632 tunnel_sock
= tunnel
->sock
;
1635 /* Check that this session doesn't already exist */
1637 session
= pppol2tp_session_find(tunnel
, sp
->pppol2tp
.s_session
);
1638 if (session
!= NULL
)
1641 /* Allocate and initialize a new session context. */
1642 session
= kzalloc(sizeof(struct pppol2tp_session
), GFP_KERNEL
);
1643 if (session
== NULL
) {
1648 skb_queue_head_init(&session
->reorder_q
);
1650 session
->magic
= L2TP_SESSION_MAGIC
;
1651 session
->owner
= current
->pid
;
1653 session
->tunnel
= tunnel
;
1654 session
->tunnel_sock
= tunnel_sock
;
1655 session
->tunnel_addr
= sp
->pppol2tp
;
1656 sprintf(&session
->name
[0], "sess %hu/%hu",
1657 session
->tunnel_addr
.s_tunnel
,
1658 session
->tunnel_addr
.s_session
);
1660 session
->stats
.tunnel_id
= session
->tunnel_addr
.s_tunnel
;
1661 session
->stats
.session_id
= session
->tunnel_addr
.s_session
;
1663 INIT_HLIST_NODE(&session
->hlist
);
1665 /* Inherit debug options from tunnel */
1666 session
->debug
= tunnel
->debug
;
1668 /* Default MTU must allow space for UDP/L2TP/PPP
1671 session
->mtu
= session
->mru
= 1500 - PPPOL2TP_HEADER_OVERHEAD
;
1673 /* If PMTU discovery was enabled, use the MTU that was discovered */
1674 dst
= sk_dst_get(sk
);
1676 u32 pmtu
= dst_mtu(__sk_dst_get(sk
));
1678 session
->mtu
= session
->mru
= pmtu
-
1679 PPPOL2TP_HEADER_OVERHEAD
;
1683 /* Special case: if source & dest session_id == 0x0000, this socket is
1684 * being created to manage the tunnel. Don't add the session to the
1685 * session hash list, just set up the internal context for use by
1686 * ioctl() and sockopt() handlers.
1688 if ((session
->tunnel_addr
.s_session
== 0) &&
1689 (session
->tunnel_addr
.d_session
== 0)) {
1691 sk
->sk_user_data
= session
;
1695 /* Get tunnel context from the tunnel socket */
1696 tunnel
= pppol2tp_sock_to_tunnel(tunnel_sock
);
1697 if (tunnel
== NULL
) {
1702 /* Right now, because we don't have a way to push the incoming skb's
1703 * straight through the UDP layer, the only header we need to worry
1704 * about is the L2TP header. This size is different depending on
1705 * whether sequence numbers are enabled for the data channel.
1707 po
->chan
.hdrlen
= PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
1709 po
->chan
.private = sk
;
1710 po
->chan
.ops
= &pppol2tp_chan_ops
;
1711 po
->chan
.mtu
= session
->mtu
;
1713 error
= ppp_register_channel(&po
->chan
);
1717 /* This is how we get the session context from the socket. */
1718 sk
->sk_user_data
= session
;
1720 /* Add session to the tunnel's hash list */
1721 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1722 write_lock(&tunnel
->hlist_lock
);
1724 write_lock_bh(&tunnel
->hlist_lock
);
1725 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1726 hlist_add_head(&session
->hlist
,
1727 pppol2tp_session_id_hash(tunnel
,
1728 session
->tunnel_addr
.s_session
));
1729 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
1730 write_unlock(&tunnel
->hlist_lock
);
1732 write_unlock_bh(&tunnel
->hlist_lock
);
1733 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
1735 atomic_inc(&pppol2tp_session_count
);
1738 pppol2tp_tunnel_inc_refcount(tunnel
);
1739 sk
->sk_state
= PPPOX_CONNECTED
;
1740 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1741 "%s: created\n", session
->name
);
1747 PRINTK(session
? session
->debug
: -1, PPPOL2TP_MSG_CONTROL
, KERN_WARNING
,
1748 "%s: connect failed: %d\n", session
->name
, error
);
1753 /* getname() support.
1755 static int pppol2tp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1756 int *usockaddr_len
, int peer
)
1758 int len
= sizeof(struct sockaddr_pppol2tp
);
1759 struct sockaddr_pppol2tp sp
;
1761 struct pppol2tp_session
*session
;
1764 if (sock
->sk
->sk_state
!= PPPOX_CONNECTED
)
1767 session
= pppol2tp_sock_to_session(sock
->sk
);
1768 if (session
== NULL
) {
1773 sp
.sa_family
= AF_PPPOX
;
1774 sp
.sa_protocol
= PX_PROTO_OL2TP
;
1775 memcpy(&sp
.pppol2tp
, &session
->tunnel_addr
,
1776 sizeof(struct pppol2tp_addr
));
1778 memcpy(uaddr
, &sp
, len
);
1780 *usockaddr_len
= len
;
1788 /****************************************************************************
1791 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1792 * sockets. However, in order to control kernel tunnel features, we allow
1793 * userspace to create a special "tunnel" PPPoX socket which is used for
1794 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1795 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1797 ****************************************************************************/
1799 /* Session ioctl helper.
1801 static int pppol2tp_session_ioctl(struct pppol2tp_session
*session
,
1802 unsigned int cmd
, unsigned long arg
)
1806 struct sock
*sk
= session
->sock
;
1807 int val
= (int) arg
;
1809 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
1810 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1811 session
->name
, cmd
, arg
);
1818 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1822 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1824 ifr
.ifr_mtu
= session
->mtu
;
1825 if (copy_to_user((void __user
*) arg
, &ifr
, sizeof(struct ifreq
)))
1828 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1829 "%s: get mtu=%d\n", session
->name
, session
->mtu
);
1835 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1839 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1842 session
->mtu
= ifr
.ifr_mtu
;
1844 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1845 "%s: set mtu=%d\n", session
->name
, session
->mtu
);
1851 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1855 if (put_user(session
->mru
, (int __user
*) arg
))
1858 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1859 "%s: get mru=%d\n", session
->name
, session
->mru
);
1865 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1869 if (get_user(val
,(int __user
*) arg
))
1873 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1874 "%s: set mru=%d\n", session
->name
, session
->mru
);
1880 if (put_user(session
->flags
, (int __user
*) arg
))
1883 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1884 "%s: get flags=%d\n", session
->name
, session
->flags
);
1890 if (get_user(val
, (int __user
*) arg
))
1892 session
->flags
= val
;
1893 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1894 "%s: set flags=%d\n", session
->name
, session
->flags
);
1898 case PPPIOCGL2TPSTATS
:
1900 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1903 if (copy_to_user((void __user
*) arg
, &session
->stats
,
1904 sizeof(session
->stats
)))
1906 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1907 "%s: get L2TP stats\n", session
->name
);
1921 /* Tunnel ioctl helper.
1923 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1924 * specifies a session_id, the session ioctl handler is called. This allows an
1925 * application to retrieve session stats via a tunnel socket.
1927 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel
*tunnel
,
1928 unsigned int cmd
, unsigned long arg
)
1931 struct sock
*sk
= tunnel
->sock
;
1932 struct pppol2tp_ioc_stats stats_req
;
1934 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
1935 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel
->name
,
1941 case PPPIOCGL2TPSTATS
:
1943 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1946 if (copy_from_user(&stats_req
, (void __user
*) arg
,
1947 sizeof(stats_req
))) {
1951 if (stats_req
.session_id
!= 0) {
1952 /* resend to session ioctl handler */
1953 struct pppol2tp_session
*session
=
1954 pppol2tp_session_find(tunnel
, stats_req
.session_id
);
1955 if (session
!= NULL
)
1956 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
1962 tunnel
->stats
.using_ipsec
= (sk
->sk_policy
[0] || sk
->sk_policy
[1]) ? 1 : 0;
1964 if (copy_to_user((void __user
*) arg
, &tunnel
->stats
,
1965 sizeof(tunnel
->stats
))) {
1969 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1970 "%s: get L2TP stats\n", tunnel
->name
);
1984 /* Main ioctl() handler.
1985 * Dispatch to tunnel or session helpers depending on the socket.
1987 static int pppol2tp_ioctl(struct socket
*sock
, unsigned int cmd
,
1990 struct sock
*sk
= sock
->sk
;
1991 struct pppol2tp_session
*session
;
1992 struct pppol2tp_tunnel
*tunnel
;
1999 if (sock_flag(sk
, SOCK_DEAD
) != 0)
2003 if ((sk
->sk_user_data
== NULL
) ||
2004 (!(sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
))))
2007 /* Get session context from the socket */
2009 session
= pppol2tp_sock_to_session(sk
);
2010 if (session
== NULL
)
2013 /* Special case: if session's session_id is zero, treat ioctl as a
2016 if ((session
->tunnel_addr
.s_session
== 0) &&
2017 (session
->tunnel_addr
.d_session
== 0)) {
2019 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
2023 err
= pppol2tp_tunnel_ioctl(tunnel
, cmd
, arg
);
2027 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
2033 /*****************************************************************************
2034 * setsockopt() / getsockopt() support.
2036 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2037 * sockets. In order to control kernel tunnel features, we allow userspace to
2038 * create a special "tunnel" PPPoX socket which is used for control only.
2039 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2040 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2041 *****************************************************************************/
2043 /* Tunnel setsockopt() helper.
2045 static int pppol2tp_tunnel_setsockopt(struct sock
*sk
,
2046 struct pppol2tp_tunnel
*tunnel
,
2047 int optname
, int val
)
2052 case PPPOL2TP_SO_DEBUG
:
2053 tunnel
->debug
= val
;
2054 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2055 "%s: set debug=%x\n", tunnel
->name
, tunnel
->debug
);
2066 /* Session setsockopt helper.
2068 static int pppol2tp_session_setsockopt(struct sock
*sk
,
2069 struct pppol2tp_session
*session
,
2070 int optname
, int val
)
2075 case PPPOL2TP_SO_RECVSEQ
:
2076 if ((val
!= 0) && (val
!= 1)) {
2080 session
->recv_seq
= val
? -1 : 0;
2081 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2082 "%s: set recv_seq=%d\n", session
->name
,
2086 case PPPOL2TP_SO_SENDSEQ
:
2087 if ((val
!= 0) && (val
!= 1)) {
2091 session
->send_seq
= val
? -1 : 0;
2093 struct sock
*ssk
= session
->sock
;
2094 struct pppox_sock
*po
= pppox_sk(ssk
);
2095 po
->chan
.hdrlen
= val
? PPPOL2TP_L2TP_HDR_SIZE_SEQ
:
2096 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
2098 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2099 "%s: set send_seq=%d\n", session
->name
, session
->send_seq
);
2102 case PPPOL2TP_SO_LNSMODE
:
2103 if ((val
!= 0) && (val
!= 1)) {
2107 session
->lns_mode
= val
? -1 : 0;
2108 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2109 "%s: set lns_mode=%d\n", session
->name
,
2113 case PPPOL2TP_SO_DEBUG
:
2114 session
->debug
= val
;
2115 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2116 "%s: set debug=%x\n", session
->name
, session
->debug
);
2119 case PPPOL2TP_SO_REORDERTO
:
2120 session
->reorder_timeout
= msecs_to_jiffies(val
);
2121 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2122 "%s: set reorder_timeout=%d\n", session
->name
,
2123 session
->reorder_timeout
);
2134 /* Main setsockopt() entry point.
2135 * Does API checks, then calls either the tunnel or session setsockopt
2136 * handler, according to whether the PPPoL2TP socket is a for a regular
2137 * session or the special tunnel type.
2139 static int pppol2tp_setsockopt(struct socket
*sock
, int level
, int optname
,
2140 char __user
*optval
, int optlen
)
2142 struct sock
*sk
= sock
->sk
;
2143 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2144 struct pppol2tp_tunnel
*tunnel
;
2148 if (level
!= SOL_PPPOL2TP
)
2149 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
2151 if (optlen
< sizeof(int))
2154 if (get_user(val
, (int __user
*)optval
))
2158 if (sk
->sk_user_data
== NULL
)
2161 /* Get session context from the socket */
2163 session
= pppol2tp_sock_to_session(sk
);
2164 if (session
== NULL
)
2167 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2169 if ((session
->tunnel_addr
.s_session
== 0) &&
2170 (session
->tunnel_addr
.d_session
== 0)) {
2172 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
2176 err
= pppol2tp_tunnel_setsockopt(sk
, tunnel
, optname
, val
);
2178 err
= pppol2tp_session_setsockopt(sk
, session
, optname
, val
);
2186 /* Tunnel getsockopt helper. Called with sock locked.
2188 static int pppol2tp_tunnel_getsockopt(struct sock
*sk
,
2189 struct pppol2tp_tunnel
*tunnel
,
2190 int optname
, int *val
)
2195 case PPPOL2TP_SO_DEBUG
:
2196 *val
= tunnel
->debug
;
2197 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2198 "%s: get debug=%x\n", tunnel
->name
, tunnel
->debug
);
2209 /* Session getsockopt helper. Called with sock locked.
2211 static int pppol2tp_session_getsockopt(struct sock
*sk
,
2212 struct pppol2tp_session
*session
,
2213 int optname
, int *val
)
2218 case PPPOL2TP_SO_RECVSEQ
:
2219 *val
= session
->recv_seq
;
2220 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2221 "%s: get recv_seq=%d\n", session
->name
, *val
);
2224 case PPPOL2TP_SO_SENDSEQ
:
2225 *val
= session
->send_seq
;
2226 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2227 "%s: get send_seq=%d\n", session
->name
, *val
);
2230 case PPPOL2TP_SO_LNSMODE
:
2231 *val
= session
->lns_mode
;
2232 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2233 "%s: get lns_mode=%d\n", session
->name
, *val
);
2236 case PPPOL2TP_SO_DEBUG
:
2237 *val
= session
->debug
;
2238 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2239 "%s: get debug=%d\n", session
->name
, *val
);
2242 case PPPOL2TP_SO_REORDERTO
:
2243 *val
= (int) jiffies_to_msecs(session
->reorder_timeout
);
2244 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2245 "%s: get reorder_timeout=%d\n", session
->name
, *val
);
2255 /* Main getsockopt() entry point.
2256 * Does API checks, then calls either the tunnel or session getsockopt
2257 * handler, according to whether the PPPoX socket is a for a regular session
2258 * or the special tunnel type.
2260 static int pppol2tp_getsockopt(struct socket
*sock
, int level
,
2261 int optname
, char __user
*optval
, int __user
*optlen
)
2263 struct sock
*sk
= sock
->sk
;
2264 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2265 struct pppol2tp_tunnel
*tunnel
;
2269 if (level
!= SOL_PPPOL2TP
)
2270 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
2272 if (get_user(len
, (int __user
*) optlen
))
2275 len
= min_t(unsigned int, len
, sizeof(int));
2281 if (sk
->sk_user_data
== NULL
)
2284 /* Get the session context */
2286 session
= pppol2tp_sock_to_session(sk
);
2287 if (session
== NULL
)
2290 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2291 if ((session
->tunnel_addr
.s_session
== 0) &&
2292 (session
->tunnel_addr
.d_session
== 0)) {
2294 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
2298 err
= pppol2tp_tunnel_getsockopt(sk
, tunnel
, optname
, &val
);
2300 err
= pppol2tp_session_getsockopt(sk
, session
, optname
, &val
);
2303 if (put_user(len
, (int __user
*) optlen
))
2306 if (copy_to_user((void __user
*) optval
, &val
, len
))
2314 /*****************************************************************************
2315 * /proc filesystem for debug
2316 *****************************************************************************/
2318 #ifdef CONFIG_PROC_FS
2320 #include <linux/seq_file.h>
2322 struct pppol2tp_seq_data
{
2323 struct pppol2tp_tunnel
*tunnel
; /* current tunnel */
2324 struct pppol2tp_session
*session
; /* NULL means get first session in tunnel */
2327 static struct pppol2tp_session
*next_session(struct pppol2tp_tunnel
*tunnel
, struct pppol2tp_session
*curr
)
2329 struct pppol2tp_session
*session
= NULL
;
2330 struct hlist_node
*walk
;
2335 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
2336 read_lock(&tunnel
->hlist_lock
);
2338 read_lock_bh(&tunnel
->hlist_lock
);
2339 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
2340 for (i
= 0; i
< PPPOL2TP_HASH_SIZE
; i
++) {
2341 hlist_for_each_entry(session
, walk
, &tunnel
->session_hlist
[i
], hlist
) {
2346 if (session
== curr
) {
2357 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
2358 read_unlock(&tunnel
->hlist_lock
);
2360 read_unlock_bh(&tunnel
->hlist_lock
);
2361 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
2368 static struct pppol2tp_tunnel
*next_tunnel(struct pppol2tp_tunnel
*curr
)
2370 struct pppol2tp_tunnel
*tunnel
= NULL
;
2372 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
2373 read_lock(&pppol2tp_tunnel_list_lock
);
2375 read_lock_bh(&pppol2tp_tunnel_list_lock
);
2376 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
2377 if (list_is_last(&curr
->list
, &pppol2tp_tunnel_list
)) {
2380 tunnel
= list_entry(curr
->list
.next
, struct pppol2tp_tunnel
, list
);
2382 <<<<<<< HEAD
:drivers
/net
/pppol2tp
.c
2383 read_unlock(&pppol2tp_tunnel_list_lock
);
2385 read_unlock_bh(&pppol2tp_tunnel_list_lock
);
2386 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/net
/pppol2tp
.c
2391 static void *pppol2tp_seq_start(struct seq_file
*m
, loff_t
*offs
)
2393 struct pppol2tp_seq_data
*pd
= SEQ_START_TOKEN
;
2399 BUG_ON(m
->private == NULL
);
2402 if (pd
->tunnel
== NULL
) {
2403 if (!list_empty(&pppol2tp_tunnel_list
))
2404 pd
->tunnel
= list_entry(pppol2tp_tunnel_list
.next
, struct pppol2tp_tunnel
, list
);
2406 pd
->session
= next_session(pd
->tunnel
, pd
->session
);
2407 if (pd
->session
== NULL
) {
2408 pd
->tunnel
= next_tunnel(pd
->tunnel
);
2412 /* NULL tunnel and session indicates end of list */
2413 if ((pd
->tunnel
== NULL
) && (pd
->session
== NULL
))
2420 static void *pppol2tp_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2426 static void pppol2tp_seq_stop(struct seq_file
*p
, void *v
)
2431 static void pppol2tp_seq_tunnel_show(struct seq_file
*m
, void *v
)
2433 struct pppol2tp_tunnel
*tunnel
= v
;
2435 seq_printf(m
, "\nTUNNEL '%s', %c %d\n",
2437 (tunnel
== tunnel
->sock
->sk_user_data
) ? 'Y':'N',
2438 atomic_read(&tunnel
->ref_count
) - 1);
2439 seq_printf(m
, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2441 (unsigned long long)tunnel
->stats
.tx_packets
,
2442 (unsigned long long)tunnel
->stats
.tx_bytes
,
2443 (unsigned long long)tunnel
->stats
.tx_errors
,
2444 (unsigned long long)tunnel
->stats
.rx_packets
,
2445 (unsigned long long)tunnel
->stats
.rx_bytes
,
2446 (unsigned long long)tunnel
->stats
.rx_errors
);
2449 static void pppol2tp_seq_session_show(struct seq_file
*m
, void *v
)
2451 struct pppol2tp_session
*session
= v
;
2453 seq_printf(m
, " SESSION '%s' %08X/%d %04X/%04X -> "
2454 "%04X/%04X %d %c\n",
2456 ntohl(session
->tunnel_addr
.addr
.sin_addr
.s_addr
),
2457 ntohs(session
->tunnel_addr
.addr
.sin_port
),
2458 session
->tunnel_addr
.s_tunnel
,
2459 session
->tunnel_addr
.s_session
,
2460 session
->tunnel_addr
.d_tunnel
,
2461 session
->tunnel_addr
.d_session
,
2462 session
->sock
->sk_state
,
2463 (session
== session
->sock
->sk_user_data
) ?
2465 seq_printf(m
, " %d/%d/%c/%c/%s %08x %u\n",
2466 session
->mtu
, session
->mru
,
2467 session
->recv_seq
? 'R' : '-',
2468 session
->send_seq
? 'S' : '-',
2469 session
->lns_mode
? "LNS" : "LAC",
2471 jiffies_to_msecs(session
->reorder_timeout
));
2472 seq_printf(m
, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2473 session
->nr
, session
->ns
,
2474 (unsigned long long)session
->stats
.tx_packets
,
2475 (unsigned long long)session
->stats
.tx_bytes
,
2476 (unsigned long long)session
->stats
.tx_errors
,
2477 (unsigned long long)session
->stats
.rx_packets
,
2478 (unsigned long long)session
->stats
.rx_bytes
,
2479 (unsigned long long)session
->stats
.rx_errors
);
2482 static int pppol2tp_seq_show(struct seq_file
*m
, void *v
)
2484 struct pppol2tp_seq_data
*pd
= v
;
2486 /* display header on line 1 */
2487 if (v
== SEQ_START_TOKEN
) {
2488 seq_puts(m
, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION
"\n");
2489 seq_puts(m
, "TUNNEL name, user-data-ok session-count\n");
2490 seq_puts(m
, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2491 seq_puts(m
, " SESSION name, addr/port src-tid/sid "
2492 "dest-tid/sid state user-data-ok\n");
2493 seq_puts(m
, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2494 seq_puts(m
, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2498 /* Show the tunnel or session context.
2500 if (pd
->session
== NULL
)
2501 pppol2tp_seq_tunnel_show(m
, pd
->tunnel
);
2503 pppol2tp_seq_session_show(m
, pd
->session
);
2509 static struct seq_operations pppol2tp_seq_ops
= {
2510 .start
= pppol2tp_seq_start
,
2511 .next
= pppol2tp_seq_next
,
2512 .stop
= pppol2tp_seq_stop
,
2513 .show
= pppol2tp_seq_show
,
2516 /* Called when our /proc file is opened. We allocate data for use when
2517 * iterating our tunnel / session contexts and store it in the private
2518 * data of the seq_file.
2520 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
)
2523 struct pppol2tp_seq_data
*pd
;
2526 ret
= seq_open(file
, &pppol2tp_seq_ops
);
2530 m
= file
->private_data
;
2532 /* Allocate and fill our proc_data for access later */
2534 m
->private = kzalloc(sizeof(struct pppol2tp_seq_data
), GFP_KERNEL
);
2535 if (m
->private == NULL
)
2545 /* Called when /proc file access completes.
2547 static int pppol2tp_proc_release(struct inode
*inode
, struct file
*file
)
2549 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2554 return seq_release(inode
, file
);
2557 static struct file_operations pppol2tp_proc_fops
= {
2558 .owner
= THIS_MODULE
,
2559 .open
= pppol2tp_proc_open
,
2561 .llseek
= seq_lseek
,
2562 .release
= pppol2tp_proc_release
,
2565 static struct proc_dir_entry
*pppol2tp_proc
;
2567 #endif /* CONFIG_PROC_FS */
2569 /*****************************************************************************
2571 *****************************************************************************/
2573 static struct proto_ops pppol2tp_ops
= {
2575 .owner
= THIS_MODULE
,
2576 .release
= pppol2tp_release
,
2577 .bind
= sock_no_bind
,
2578 .connect
= pppol2tp_connect
,
2579 .socketpair
= sock_no_socketpair
,
2580 .accept
= sock_no_accept
,
2581 .getname
= pppol2tp_getname
,
2582 .poll
= datagram_poll
,
2583 .listen
= sock_no_listen
,
2584 .shutdown
= sock_no_shutdown
,
2585 .setsockopt
= pppol2tp_setsockopt
,
2586 .getsockopt
= pppol2tp_getsockopt
,
2587 .sendmsg
= pppol2tp_sendmsg
,
2588 .recvmsg
= pppol2tp_recvmsg
,
2589 .mmap
= sock_no_mmap
,
2590 .ioctl
= pppox_ioctl
,
2593 static struct pppox_proto pppol2tp_proto
= {
2594 .create
= pppol2tp_create
,
2595 .ioctl
= pppol2tp_ioctl
2598 static int __init
pppol2tp_init(void)
2602 err
= proto_register(&pppol2tp_sk_proto
, 0);
2605 err
= register_pppox_proto(PX_PROTO_OL2TP
, &pppol2tp_proto
);
2607 goto out_unregister_pppol2tp_proto
;
2609 #ifdef CONFIG_PROC_FS
2610 pppol2tp_proc
= create_proc_entry("pppol2tp", 0, init_net
.proc_net
);
2611 if (!pppol2tp_proc
) {
2613 goto out_unregister_pppox_proto
;
2615 pppol2tp_proc
->proc_fops
= &pppol2tp_proc_fops
;
2616 #endif /* CONFIG_PROC_FS */
2617 printk(KERN_INFO
"PPPoL2TP kernel driver, %s\n",
2618 PPPOL2TP_DRV_VERSION
);
2622 #ifdef CONFIG_PROC_FS
2623 out_unregister_pppox_proto
:
2624 unregister_pppox_proto(PX_PROTO_OL2TP
);
2626 out_unregister_pppol2tp_proto
:
2627 proto_unregister(&pppol2tp_sk_proto
);
2631 static void __exit
pppol2tp_exit(void)
2633 unregister_pppox_proto(PX_PROTO_OL2TP
);
2635 #ifdef CONFIG_PROC_FS
2636 remove_proc_entry("pppol2tp", init_net
.proc_net
);
2638 proto_unregister(&pppol2tp_sk_proto
);
2641 module_init(pppol2tp_init
);
2642 module_exit(pppol2tp_exit
);
2644 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
2645 "James Chapman <jchapman@katalix.com>");
2646 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2647 MODULE_LICENSE("GPL");
2648 MODULE_VERSION(PPPOL2TP_DRV_VERSION
);