Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / pppol2tp.c
blob9f4627baa0261bb26a8023b2278434aa9b099cb8
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 <<<<<<< HEAD:drivers/net/pppol2tp.c
306 read_lock(&tunnel->hlist_lock);
307 =======
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);
314 =======
315 read_unlock_bh(&tunnel->hlist_lock);
316 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
317 return session;
320 <<<<<<< HEAD:drivers/net/pppol2tp.c
321 read_unlock(&tunnel->hlist_lock);
322 =======
323 read_unlock_bh(&tunnel->hlist_lock);
324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
326 return NULL;
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);
337 =======
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);
344 =======
345 read_unlock_bh(&pppol2tp_tunnel_list_lock);
346 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
347 return tunnel;
350 <<<<<<< HEAD:drivers/net/pppol2tp.c
351 read_unlock(&pppol2tp_tunnel_list_lock);
352 =======
353 read_unlock_bh(&pppol2tp_tunnel_list_lock);
354 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
356 return NULL;
359 /*****************************************************************************
360 * Receive data handling
361 *****************************************************************************/
363 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
364 * number.
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
370 =======
371 struct sk_buff *tmp;
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) {
378 =======
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++;
389 goto out;
393 __skb_queue_tail(&session->reorder_q, skb);
395 out:
396 <<<<<<< HEAD:drivers/net/pppol2tp.c
397 spin_unlock(&session->reorder_q.lock);
398 =======
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
413 =======
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);
420 =======
421 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
422 skb_orphan(skb);
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) {
430 /* Bump our Nr */
431 session->nr++;
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.
449 * Namely we need to:
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
454 * of the UDP tunnel
455 * - reset netfilter information as it doesn't apply
456 * to the inner packet either
458 secpath_reset(skb);
459 dst_release(skb->dst);
460 skb->dst = NULL;
461 nf_reset(skb);
463 po = pppox_sk(session_sock);
464 ppp_input(&po->chan, skb);
465 } else {
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++;
471 kfree_skb(skb);
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)
482 struct sk_buff *skb;
483 struct sk_buff *tmp;
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);
491 =======
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);
505 kfree_skb(skb);
506 <<<<<<< HEAD:drivers/net/pppol2tp.c
507 =======
508 sock_put(session->sock);
509 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
510 continue;
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));
521 goto out;
524 <<<<<<< HEAD:drivers/net/pppol2tp.c
525 spin_unlock(&session->reorder_q.lock);
526 =======
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);
537 =======
538 spin_lock_bh(&session->reorder_q.lock);
539 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
542 out:
543 <<<<<<< HEAD:drivers/net/pppol2tp.c
544 spin_unlock(&session->reorder_q.lock);
545 =======
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;
561 u16 hdrflags;
562 u16 tunnel_id, session_id;
563 int length;
564 int offset;
566 tunnel = pppol2tp_sock_to_tunnel(sock);
567 if (tunnel == NULL)
568 goto no_tunnel;
570 /* UDP always verifies the packet length. */
571 __skb_pull(skb, sizeof(struct udphdr));
573 /* Short packet? */
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);
577 goto error;
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))
590 goto error;
592 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
594 offset = 0;
595 do {
596 printk(" %02X", ptr[offset]);
597 } while (++offset < length);
599 printk("\n");
602 /* Get length of L2TP packet */
603 length = skb->len;
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);
609 goto error;
612 /* Skip flags */
613 ptr += 2;
615 /* If length is present, skip it */
616 if (hdrflags & L2TP_HDRFLAG_L)
617 ptr += 2;
619 /* Extract tunnel and session ID */
620 tunnel_id = ntohs(*(__be16 *) ptr);
621 ptr += 2;
622 session_id = ntohs(*(__be16 *) ptr);
623 ptr += 2;
625 /* Find the session context */
626 session = pppol2tp_session_find(tunnel, session_id);
627 if (!session) {
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);
632 goto error;
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
644 * frame.
646 if (hdrflags & L2TP_HDRFLAG_S) {
647 u16 ns, nr;
648 ns = ntohs(*(__be16 *) ptr);
649 ptr += 2;
650 nr = ntohs(*(__be16 *) ptr);
651 ptr += 2;
653 /* Received a packet with sequence numbers. If we're the LNS,
654 * check if we sre sending sequence numbers and if not,
655 * configure it so.
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",
660 session->name);
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);
672 } else {
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++;
681 goto discard;
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",
692 session->name);
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++;
699 goto discard;
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);
709 ptr += 2 + offset;
712 offset = ptr - optr;
713 if (!pskb_may_pull(skb, offset))
714 goto discard;
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))
727 goto discard;
729 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
730 skb_pull(skb, 2);
732 /* Prepare skb for adding to the session's reorder_q. Hold
733 * packets for max reorder_timeout or 1 second if not
734 * reordering.
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);
749 } else {
750 /* Packet reordering disabled. Discard out-of-sequence
751 * packets
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));
761 goto discard;
763 skb_queue_tail(&session->reorder_q, skb);
765 } else {
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);
776 return 0;
778 discard:
779 session->stats.rx_errors++;
780 kfree_skb(skb);
781 sock_put(session->sock);
783 return 0;
785 error:
786 /* Put UDP header back */
787 __skb_push(skb, sizeof(struct udphdr));
789 no_tunnel:
790 return 1;
793 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
794 * Return codes:
795 * 0 : success.
796 * <0: error
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);
804 if (tunnel == NULL)
805 goto pass_up;
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))
811 goto pass_up;
813 return 0;
815 pass_up:
816 return 1;
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,
823 int flags)
825 int err;
826 struct sk_buff *skb;
827 struct sock *sk = sock->sk;
829 err = -EIO;
830 if (sk->sk_state & PPPOX_BOUND)
831 goto end;
833 msg->msg_namelen = 0;
835 err = 0;
836 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
837 flags & MSG_DONTWAIT, &err);
838 if (skb) {
839 err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data,
840 skb->len);
841 if (err < 0)
842 goto do_skb_free;
843 err = skb->len;
845 do_skb_free:
846 kfree_skb(skb);
847 end:
848 return err;
851 /************************************************************************
852 * Transmit handling
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,
869 void *buf)
871 __be16 *bufp = buf;
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);
886 *bufp++ = 0;
887 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,
898 size_t total_len)
900 static const unsigned char ppph[2] = { 0xff, 0x03 };
901 struct sock *sk = sock->sk;
902 struct inet_sock *inet;
903 __wsum csum = 0;
904 struct sk_buff *skb;
905 int error;
906 int hdr_len;
907 struct pppol2tp_session *session;
908 struct pppol2tp_tunnel *tunnel;
909 struct udphdr *uh;
910 unsigned int len;
912 error = -ENOTCONN;
913 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
914 goto error;
916 /* Get session and tunnel contexts */
917 error = -EBADF;
918 session = pppol2tp_sock_to_session(sk);
919 if (session == NULL)
920 goto error;
922 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
923 if (tunnel == NULL)
924 goto error;
926 /* What header length is configured for this session? */
927 hdr_len = pppol2tp_l2tp_header_len(session);
929 /* Allocate a socket buffer */
930 error = -ENOMEM;
931 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
932 sizeof(struct udphdr) + hdr_len +
933 sizeof(ppph) + total_len,
934 0, GFP_KERNEL);
935 if (!skb)
936 goto error;
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);
950 uh->check = 0;
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);
957 /* Add PPP header */
958 skb->data[0] = ppph[0];
959 skb->data[1] = ppph[1];
960 skb_put(skb, 2);
962 /* Copy user data into skb */
963 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
964 if (error < 0) {
965 kfree_skb(skb);
966 goto error;
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);
974 /* Debug */
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);
979 else
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) {
984 int i;
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++);
990 if (i == 15) {
991 printk(" ...");
992 break;
995 printk("\n");
998 /* Queue the packet to IP for output */
999 len = skb->len;
1000 error = ip_queue_xmit(skb, 1);
1002 /* Update stats */
1003 if (error >= 0) {
1004 tunnel->stats.tx_packets++;
1005 tunnel->stats.tx_bytes += len;
1006 session->stats.tx_packets++;
1007 session->stats.tx_bytes += len;
1008 } else {
1009 tunnel->stats.tx_errors++;
1010 session->stats.tx_errors++;
1013 error:
1014 return error;
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
1022 * from the kernel.
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;
1036 int hdr_len;
1037 struct pppol2tp_session *session;
1038 struct pppol2tp_tunnel *tunnel;
1039 int rc;
1040 int headroom;
1041 int data_len = skb->len;
1042 struct inet_sock *inet;
1043 __wsum csum = 0;
1044 struct udphdr *uh;
1045 unsigned int len;
1047 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
1048 goto abort;
1050 /* Get session and tunnel contexts from the socket */
1051 session = pppol2tp_sock_to_session(sk);
1052 if (session == NULL)
1053 goto abort;
1055 sk_tun = session->tunnel_sock;
1056 if (sk_tun == NULL)
1057 goto abort;
1058 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
1059 if (tunnel == NULL)
1060 goto abort;
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))
1075 goto abort;
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);
1089 uh = udp_hdr(skb);
1090 uh->source = inet->sport;
1091 uh->dest = inet->dport;
1092 uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len);
1093 uh->check = 0;
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);
1099 /* Debug */
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);
1104 else
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) {
1109 int i;
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++);
1115 if (i == 31) {
1116 printk(" ...");
1117 break;
1120 printk("\n");
1123 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1124 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1125 IPSKB_REROUTED);
1126 nf_reset(skb);
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);
1132 =======
1133 skb->dst = dst_clone(__sk_dst_get(sk_tun));
1134 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1135 skb_orphan(skb);
1136 skb->sk = sk_tun;
1138 /* Queue the packet to IP for output */
1139 len = skb->len;
1140 rc = ip_queue_xmit(skb, 1);
1142 /* Update stats */
1143 if (rc >= 0) {
1144 tunnel->stats.tx_packets++;
1145 tunnel->stats.tx_bytes += len;
1146 session->stats.tx_packets++;
1147 session->stats.tx_bytes += len;
1148 } else {
1149 tunnel->stats.tx_errors++;
1150 session->stats.tx_errors++;
1153 return 1;
1155 abort:
1156 /* Free the original skb */
1157 kfree_skb(skb);
1158 return 1;
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
1166 * too.
1168 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1170 int hash;
1171 struct hlist_node *walk;
1172 struct hlist_node *tmp;
1173 struct pppol2tp_session *session;
1174 struct sock *sk;
1176 if (tunnel == NULL)
1177 BUG();
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);
1184 =======
1185 write_lock_bh(&tunnel->hlist_lock);
1186 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1187 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1188 again:
1189 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1190 <<<<<<< HEAD:drivers/net/pppol2tp.c
1191 =======
1192 struct sk_buff *skb;
1194 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1195 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1197 sk = session->sock;
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.
1210 sock_hold(sk);
1211 <<<<<<< HEAD:drivers/net/pppol2tp.c
1212 write_unlock(&tunnel->hlist_lock);
1213 =======
1214 write_unlock_bh(&tunnel->hlist_lock);
1215 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1216 lock_sock(sk);
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);
1229 =======
1230 while ((skb = skb_dequeue(&session->reorder_q))) {
1231 kfree_skb(skb);
1232 sock_put(sk);
1234 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1236 release_sock(sk);
1237 sock_put(sk);
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
1242 * progress.
1244 <<<<<<< HEAD:drivers/net/pppol2tp.c
1245 write_lock(&tunnel->hlist_lock);
1246 =======
1247 write_lock_bh(&tunnel->hlist_lock);
1248 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1249 goto again;
1252 <<<<<<< HEAD:drivers/net/pppol2tp.c
1253 write_unlock(&tunnel->hlist_lock);
1254 =======
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);
1267 =======
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);
1273 =======
1274 write_unlock_bh(&pppol2tp_tunnel_list_lock);
1275 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1277 atomic_dec(&pppol2tp_tunnel_count);
1278 kfree(tunnel);
1281 /* Tunnel UDP socket destruct hook.
1282 * The tunnel context is deleted only when all session sockets have been
1283 * closed.
1285 static void pppol2tp_tunnel_destruct(struct sock *sk)
1287 struct pppol2tp_tunnel *tunnel;
1289 tunnel = pppol2tp_sock_to_tunnel(sk);
1290 if (tunnel == NULL)
1291 goto end;
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);
1314 end:
1315 return;
1318 /* Really kill the session socket. (Called from sock_put() if
1319 * refcnt == 0.)
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)
1330 goto out;
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
1345 * tunnels.
1347 if (session->tunnel_addr.s_session != 0) {
1348 /* Delete the session socket from the
1349 * hash
1351 <<<<<<< HEAD:drivers/net/pppol2tp.c
1352 write_lock(&tunnel->hlist_lock);
1353 =======
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);
1359 =======
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);
1375 kfree(session);
1376 out:
1377 return;
1380 /* Called when the PPPoX socket (session) is closed.
1382 static int pppol2tp_release(struct socket *sock)
1384 struct sock *sk = sock->sk;
1385 int error;
1387 if (!sk)
1388 return 0;
1390 error = -EBADF;
1391 lock_sock(sk);
1392 if (sock_flag(sk, SOCK_DEAD) != 0)
1393 goto error;
1395 pppox_unbind_sock(sk);
1397 /* Signal the death of the socket. */
1398 sk->sk_state = PPPOX_DEAD;
1399 sock_orphan(sk);
1400 sock->sk = NULL;
1402 /* Purge any queued data */
1403 skb_queue_purge(&sk->sk_receive_queue);
1404 skb_queue_purge(&sk->sk_write_queue);
1406 release_sock(sk);
1408 /* This will delete the session context via
1409 * pppol2tp_session_destruct() if the socket's refcnt drops to
1410 * zero.
1412 sock_put(sk);
1414 return 0;
1416 error:
1417 release_sock(sk);
1418 return error;
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,
1425 int *error)
1427 int err;
1428 struct socket *sock = NULL;
1429 struct sock *sk;
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.
1436 err = -EBADF;
1437 sock = sockfd_lookup(fd, &err);
1438 if (!sock) {
1439 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1440 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1441 tunnel_id, fd, err);
1442 goto err;
1445 sk = sock->sk;
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);
1453 goto err;
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);
1460 goto err;
1463 err = -ENOTCONN;
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 */
1469 err = -EBUSY;
1470 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1472 /* This socket has already been prepped */
1473 ret = tunnel->sock;
1474 goto out;
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) {
1482 err = -ENOMEM;
1483 goto err;
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;
1498 tunnel->sock = sk;
1499 sk->sk_allocation = GFP_ATOMIC;
1501 /* Misc init */
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);
1508 =======
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);
1514 =======
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;
1528 ret = tunnel->sock;
1530 *error = 0;
1531 out:
1532 if (sock)
1533 sockfd_put(sock);
1535 return ret;
1537 err:
1538 *error = err;
1539 goto out;
1542 static struct proto pppol2tp_sk_proto = {
1543 .name = "PPPOL2TP",
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;
1553 struct sock *sk;
1555 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
1556 if (!sk)
1557 goto out;
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;
1571 error = 0;
1573 out:
1574 return error;
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;
1589 int error = 0;
1591 lock_sock(sk);
1593 error = -EINVAL;
1594 if (sp->sa_protocol != PX_PROTO_OL2TP)
1595 goto end;
1597 /* Check for already bound sockets */
1598 error = -EBUSY;
1599 if (sk->sk_state & PPPOX_CONNECTED)
1600 goto end;
1602 /* We don't supporting rebinding anyway */
1603 error = -EALREADY;
1604 if (sk->sk_user_data)
1605 goto end; /* socket is already attached */
1607 /* Don't bind if s_tunnel is 0 */
1608 error = -EINVAL;
1609 if (sp->pppol2tp.s_tunnel == 0)
1610 goto end;
1612 /* Special case: prepare tunnel socket if s_session and
1613 * d_session is 0. Otherwise look up tunnel using supplied
1614 * tunnel id.
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,
1619 &error);
1620 if (tunnel_sock == NULL)
1621 goto end;
1623 tunnel = tunnel_sock->sk_user_data;
1624 } else {
1625 tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel);
1627 /* Error if we can't find the tunnel */
1628 error = -ENOENT;
1629 if (tunnel == NULL)
1630 goto end;
1632 tunnel_sock = tunnel->sock;
1635 /* Check that this session doesn't already exist */
1636 error = -EEXIST;
1637 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1638 if (session != NULL)
1639 goto end;
1641 /* Allocate and initialize a new session context. */
1642 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1643 if (session == NULL) {
1644 error = -ENOMEM;
1645 goto end;
1648 skb_queue_head_init(&session->reorder_q);
1650 session->magic = L2TP_SESSION_MAGIC;
1651 session->owner = current->pid;
1652 session->sock = sk;
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
1669 * headers.
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);
1675 if (dst != NULL) {
1676 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1677 if (pmtu != 0)
1678 session->mtu = session->mru = pmtu -
1679 PPPOL2TP_HEADER_OVERHEAD;
1680 dst_release(dst);
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)) {
1690 error = 0;
1691 sk->sk_user_data = session;
1692 goto out_no_ppp;
1695 /* Get tunnel context from the tunnel socket */
1696 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1697 if (tunnel == NULL) {
1698 error = -EBADF;
1699 goto end;
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);
1714 if (error)
1715 goto end;
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);
1723 =======
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);
1731 =======
1732 write_unlock_bh(&tunnel->hlist_lock);
1733 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
1735 atomic_inc(&pppol2tp_session_count);
1737 out_no_ppp:
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);
1743 end:
1744 release_sock(sk);
1746 if (error != 0)
1747 PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1748 "%s: connect failed: %d\n", session->name, error);
1750 return 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;
1760 int error = 0;
1761 struct pppol2tp_session *session;
1763 error = -ENOTCONN;
1764 if (sock->sk->sk_state != PPPOX_CONNECTED)
1765 goto end;
1767 session = pppol2tp_sock_to_session(sock->sk);
1768 if (session == NULL) {
1769 error = -EBADF;
1770 goto end;
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;
1782 error = 0;
1784 end:
1785 return error;
1788 /****************************************************************************
1789 * ioctl() handlers.
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()
1796 * calls.
1797 ****************************************************************************/
1799 /* Session ioctl helper.
1801 static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1802 unsigned int cmd, unsigned long arg)
1804 struct ifreq ifr;
1805 int err = 0;
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);
1813 sock_hold(sk);
1815 switch (cmd) {
1816 case SIOCGIFMTU:
1817 err = -ENXIO;
1818 if (!(sk->sk_state & PPPOX_CONNECTED))
1819 break;
1821 err = -EFAULT;
1822 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1823 break;
1824 ifr.ifr_mtu = session->mtu;
1825 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1826 break;
1828 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1829 "%s: get mtu=%d\n", session->name, session->mtu);
1830 err = 0;
1831 break;
1833 case SIOCSIFMTU:
1834 err = -ENXIO;
1835 if (!(sk->sk_state & PPPOX_CONNECTED))
1836 break;
1838 err = -EFAULT;
1839 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1840 break;
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);
1846 err = 0;
1847 break;
1849 case PPPIOCGMRU:
1850 err = -ENXIO;
1851 if (!(sk->sk_state & PPPOX_CONNECTED))
1852 break;
1854 err = -EFAULT;
1855 if (put_user(session->mru, (int __user *) arg))
1856 break;
1858 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1859 "%s: get mru=%d\n", session->name, session->mru);
1860 err = 0;
1861 break;
1863 case PPPIOCSMRU:
1864 err = -ENXIO;
1865 if (!(sk->sk_state & PPPOX_CONNECTED))
1866 break;
1868 err = -EFAULT;
1869 if (get_user(val,(int __user *) arg))
1870 break;
1872 session->mru = val;
1873 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1874 "%s: set mru=%d\n", session->name, session->mru);
1875 err = 0;
1876 break;
1878 case PPPIOCGFLAGS:
1879 err = -EFAULT;
1880 if (put_user(session->flags, (int __user *) arg))
1881 break;
1883 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1884 "%s: get flags=%d\n", session->name, session->flags);
1885 err = 0;
1886 break;
1888 case PPPIOCSFLAGS:
1889 err = -EFAULT;
1890 if (get_user(val, (int __user *) arg))
1891 break;
1892 session->flags = val;
1893 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1894 "%s: set flags=%d\n", session->name, session->flags);
1895 err = 0;
1896 break;
1898 case PPPIOCGL2TPSTATS:
1899 err = -ENXIO;
1900 if (!(sk->sk_state & PPPOX_CONNECTED))
1901 break;
1903 if (copy_to_user((void __user *) arg, &session->stats,
1904 sizeof(session->stats)))
1905 break;
1906 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1907 "%s: get L2TP stats\n", session->name);
1908 err = 0;
1909 break;
1911 default:
1912 err = -ENOSYS;
1913 break;
1916 sock_put(sk);
1918 return err;
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)
1930 int err = 0;
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,
1936 cmd, arg);
1938 sock_hold(sk);
1940 switch (cmd) {
1941 case PPPIOCGL2TPSTATS:
1942 err = -ENXIO;
1943 if (!(sk->sk_state & PPPOX_CONNECTED))
1944 break;
1946 if (copy_from_user(&stats_req, (void __user *) arg,
1947 sizeof(stats_req))) {
1948 err = -EFAULT;
1949 break;
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);
1957 else
1958 err = -EBADR;
1959 break;
1961 #ifdef CONFIG_XFRM
1962 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1963 #endif
1964 if (copy_to_user((void __user *) arg, &tunnel->stats,
1965 sizeof(tunnel->stats))) {
1966 err = -EFAULT;
1967 break;
1969 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1970 "%s: get L2TP stats\n", tunnel->name);
1971 err = 0;
1972 break;
1974 default:
1975 err = -ENOSYS;
1976 break;
1979 sock_put(sk);
1981 return err;
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,
1988 unsigned long arg)
1990 struct sock *sk = sock->sk;
1991 struct pppol2tp_session *session;
1992 struct pppol2tp_tunnel *tunnel;
1993 int err;
1995 if (!sk)
1996 return 0;
1998 err = -EBADF;
1999 if (sock_flag(sk, SOCK_DEAD) != 0)
2000 goto end;
2002 err = -ENOTCONN;
2003 if ((sk->sk_user_data == NULL) ||
2004 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
2005 goto end;
2007 /* Get session context from the socket */
2008 err = -EBADF;
2009 session = pppol2tp_sock_to_session(sk);
2010 if (session == NULL)
2011 goto end;
2013 /* Special case: if session's session_id is zero, treat ioctl as a
2014 * tunnel ioctl
2016 if ((session->tunnel_addr.s_session == 0) &&
2017 (session->tunnel_addr.d_session == 0)) {
2018 err = -EBADF;
2019 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2020 if (tunnel == NULL)
2021 goto end;
2023 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
2024 goto end;
2027 err = pppol2tp_session_ioctl(session, cmd, arg);
2029 end:
2030 return err;
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)
2049 int err = 0;
2051 switch (optname) {
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);
2056 break;
2058 default:
2059 err = -ENOPROTOOPT;
2060 break;
2063 return err;
2066 /* Session setsockopt helper.
2068 static int pppol2tp_session_setsockopt(struct sock *sk,
2069 struct pppol2tp_session *session,
2070 int optname, int val)
2072 int err = 0;
2074 switch (optname) {
2075 case PPPOL2TP_SO_RECVSEQ:
2076 if ((val != 0) && (val != 1)) {
2077 err = -EINVAL;
2078 break;
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,
2083 session->recv_seq);
2084 break;
2086 case PPPOL2TP_SO_SENDSEQ:
2087 if ((val != 0) && (val != 1)) {
2088 err = -EINVAL;
2089 break;
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);
2100 break;
2102 case PPPOL2TP_SO_LNSMODE:
2103 if ((val != 0) && (val != 1)) {
2104 err = -EINVAL;
2105 break;
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,
2110 session->lns_mode);
2111 break;
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);
2117 break;
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);
2124 break;
2126 default:
2127 err = -ENOPROTOOPT;
2128 break;
2131 return err;
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;
2145 int val;
2146 int err;
2148 if (level != SOL_PPPOL2TP)
2149 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2151 if (optlen < sizeof(int))
2152 return -EINVAL;
2154 if (get_user(val, (int __user *)optval))
2155 return -EFAULT;
2157 err = -ENOTCONN;
2158 if (sk->sk_user_data == NULL)
2159 goto end;
2161 /* Get session context from the socket */
2162 err = -EBADF;
2163 session = pppol2tp_sock_to_session(sk);
2164 if (session == NULL)
2165 goto end;
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)) {
2171 err = -EBADF;
2172 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2173 if (tunnel == NULL)
2174 goto end;
2176 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2177 } else
2178 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2180 err = 0;
2182 end:
2183 return err;
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)
2192 int err = 0;
2194 switch (optname) {
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);
2199 break;
2201 default:
2202 err = -ENOPROTOOPT;
2203 break;
2206 return err;
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)
2215 int err = 0;
2217 switch (optname) {
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);
2222 break;
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);
2228 break;
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);
2234 break;
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);
2240 break;
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);
2246 break;
2248 default:
2249 err = -ENOPROTOOPT;
2252 return err;
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;
2266 int val, len;
2267 int err;
2269 if (level != SOL_PPPOL2TP)
2270 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2272 if (get_user(len, (int __user *) optlen))
2273 return -EFAULT;
2275 len = min_t(unsigned int, len, sizeof(int));
2277 if (len < 0)
2278 return -EINVAL;
2280 err = -ENOTCONN;
2281 if (sk->sk_user_data == NULL)
2282 goto end;
2284 /* Get the session context */
2285 err = -EBADF;
2286 session = pppol2tp_sock_to_session(sk);
2287 if (session == NULL)
2288 goto end;
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)) {
2293 err = -EBADF;
2294 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2295 if (tunnel == NULL)
2296 goto end;
2298 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2299 } else
2300 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2302 err = -EFAULT;
2303 if (put_user(len, (int __user *) optlen))
2304 goto end;
2306 if (copy_to_user((void __user *) optval, &val, len))
2307 goto end;
2309 err = 0;
2310 end:
2311 return err;
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;
2331 int found = 0;
2332 int next = 0;
2333 int i;
2335 <<<<<<< HEAD:drivers/net/pppol2tp.c
2336 read_lock(&tunnel->hlist_lock);
2337 =======
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) {
2342 if (curr == NULL) {
2343 found = 1;
2344 goto out;
2346 if (session == curr) {
2347 next = 1;
2348 continue;
2350 if (next) {
2351 found = 1;
2352 goto out;
2356 out:
2357 <<<<<<< HEAD:drivers/net/pppol2tp.c
2358 read_unlock(&tunnel->hlist_lock);
2359 =======
2360 read_unlock_bh(&tunnel->hlist_lock);
2361 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
2362 if (!found)
2363 session = NULL;
2365 return session;
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);
2374 =======
2375 read_lock_bh(&pppol2tp_tunnel_list_lock);
2376 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
2377 if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) {
2378 goto out;
2380 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2381 out:
2382 <<<<<<< HEAD:drivers/net/pppol2tp.c
2383 read_unlock(&pppol2tp_tunnel_list_lock);
2384 =======
2385 read_unlock_bh(&pppol2tp_tunnel_list_lock);
2386 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/pppol2tp.c
2388 return tunnel;
2391 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2393 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
2394 loff_t pos = *offs;
2396 if (!pos)
2397 goto out;
2399 BUG_ON(m->private == NULL);
2400 pd = m->private;
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);
2405 } else {
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))
2414 pd = NULL;
2416 out:
2417 return pd;
2420 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2422 (*pos)++;
2423 return NULL;
2426 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2428 /* nothing to do */
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",
2436 tunnel->name,
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",
2440 tunnel->debug,
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",
2455 session->name,
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) ?
2464 'Y' : 'N');
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",
2470 session->debug,
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");
2495 goto out;
2498 /* Show the tunnel or session context.
2500 if (pd->session == NULL)
2501 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2502 else
2503 pppol2tp_seq_session_show(m, pd->session);
2505 out:
2506 return 0;
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)
2522 struct seq_file *m;
2523 struct pppol2tp_seq_data *pd;
2524 int ret = 0;
2526 ret = seq_open(file, &pppol2tp_seq_ops);
2527 if (ret < 0)
2528 goto out;
2530 m = file->private_data;
2532 /* Allocate and fill our proc_data for access later */
2533 ret = -ENOMEM;
2534 m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL);
2535 if (m->private == NULL)
2536 goto out;
2538 pd = m->private;
2539 ret = 0;
2541 out:
2542 return ret;
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;
2551 kfree(m->private);
2552 m->private = NULL;
2554 return seq_release(inode, file);
2557 static struct file_operations pppol2tp_proc_fops = {
2558 .owner = THIS_MODULE,
2559 .open = pppol2tp_proc_open,
2560 .read = seq_read,
2561 .llseek = seq_lseek,
2562 .release = pppol2tp_proc_release,
2565 static struct proc_dir_entry *pppol2tp_proc;
2567 #endif /* CONFIG_PROC_FS */
2569 /*****************************************************************************
2570 * Init and cleanup
2571 *****************************************************************************/
2573 static struct proto_ops pppol2tp_ops = {
2574 .family = AF_PPPOX,
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)
2600 int err;
2602 err = proto_register(&pppol2tp_sk_proto, 0);
2603 if (err)
2604 goto out;
2605 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2606 if (err)
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) {
2612 err = -ENOMEM;
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);
2620 out:
2621 return err;
2622 #ifdef CONFIG_PROC_FS
2623 out_unregister_pppox_proto:
2624 unregister_pppox_proto(PX_PROTO_OL2TP);
2625 #endif
2626 out_unregister_pppol2tp_proto:
2627 proto_unregister(&pppol2tp_sk_proto);
2628 goto out;
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);
2637 #endif
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);