x86, apic: Fix spurious error interrupts triggering on all non-boot APs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / pppol2tp.c
blob8015310b300207f21e35a6b3da2ec5686e1ae473
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/string.h>
65 #include <linux/list.h>
66 #include <asm/uaccess.h>
68 #include <linux/kernel.h>
69 #include <linux/spinlock.h>
70 #include <linux/kthread.h>
71 #include <linux/sched.h>
72 #include <linux/slab.h>
73 #include <linux/errno.h>
74 #include <linux/jiffies.h>
76 #include <linux/netdevice.h>
77 #include <linux/net.h>
78 #include <linux/inetdevice.h>
79 #include <linux/skbuff.h>
80 #include <linux/init.h>
81 #include <linux/ip.h>
82 #include <linux/udp.h>
83 #include <linux/if_pppox.h>
84 #include <linux/if_pppol2tp.h>
85 #include <net/sock.h>
86 #include <linux/ppp_channel.h>
87 #include <linux/ppp_defs.h>
88 #include <linux/if_ppp.h>
89 #include <linux/file.h>
90 #include <linux/hash.h>
91 #include <linux/sort.h>
92 #include <linux/proc_fs.h>
93 #include <linux/nsproxy.h>
94 #include <net/net_namespace.h>
95 #include <net/netns/generic.h>
96 #include <net/dst.h>
97 #include <net/ip.h>
98 #include <net/udp.h>
99 #include <net/xfrm.h>
101 #include <asm/byteorder.h>
102 #include <asm/atomic.h>
105 #define PPPOL2TP_DRV_VERSION "V1.0"
107 /* L2TP header constants */
108 #define L2TP_HDRFLAG_T 0x8000
109 #define L2TP_HDRFLAG_L 0x4000
110 #define L2TP_HDRFLAG_S 0x0800
111 #define L2TP_HDRFLAG_O 0x0200
112 #define L2TP_HDRFLAG_P 0x0100
114 #define L2TP_HDR_VER_MASK 0x000F
115 #define L2TP_HDR_VER 0x0002
117 /* Space for UDP, L2TP and PPP headers */
118 #define PPPOL2TP_HEADER_OVERHEAD 40
120 /* Just some random numbers */
121 #define L2TP_TUNNEL_MAGIC 0x42114DDA
122 #define L2TP_SESSION_MAGIC 0x0C04EB7D
124 #define PPPOL2TP_HASH_BITS 4
125 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
127 /* Default trace flags */
128 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
130 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
131 do { \
132 if ((_mask) & (_type)) \
133 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
134 } while(0)
136 /* Number of bytes to build transmit L2TP headers.
137 * Unfortunately the size is different depending on whether sequence numbers
138 * are enabled.
140 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
141 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
143 struct pppol2tp_tunnel;
145 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
146 * socket. Contains information to determine incoming packets and transmit
147 * outgoing ones.
149 struct pppol2tp_session
151 int magic; /* should be
152 * L2TP_SESSION_MAGIC */
153 int owner; /* pid that opened the socket */
155 struct sock *sock; /* Pointer to the session
156 * PPPoX socket */
157 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
158 * socket */
160 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
162 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
163 * context */
165 char name[20]; /* "sess xxxxx/yyyyy", where
166 * x=tunnel_id, y=session_id */
167 int mtu;
168 int mru;
169 int flags; /* accessed by PPPIOCGFLAGS.
170 * Unused. */
171 unsigned recv_seq:1; /* expect receive packets with
172 * sequence numbers? */
173 unsigned send_seq:1; /* send packets with sequence
174 * numbers? */
175 unsigned lns_mode:1; /* behave as LNS? LAC enables
176 * sequence numbers under
177 * control of LNS. */
178 int debug; /* bitmask of debug message
179 * categories */
180 int reorder_timeout; /* configured reorder timeout
181 * (in jiffies) */
182 u16 nr; /* session NR state (receive) */
183 u16 ns; /* session NR state (send) */
184 struct sk_buff_head reorder_q; /* receive reorder queue */
185 struct pppol2tp_ioc_stats stats;
186 struct hlist_node hlist; /* Hash list node */
189 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
190 * all the associated sessions so incoming packets can be sorted out
192 struct pppol2tp_tunnel
194 int magic; /* Should be L2TP_TUNNEL_MAGIC */
195 rwlock_t hlist_lock; /* protect session_hlist */
196 struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE];
197 /* hashed list of sessions,
198 * hashed by id */
199 int debug; /* bitmask of debug message
200 * categories */
201 char name[12]; /* "tunl xxxxx" */
202 struct pppol2tp_ioc_stats stats;
204 void (*old_sk_destruct)(struct sock *);
206 struct sock *sock; /* Parent socket */
207 struct list_head list; /* Keep a list of all open
208 * prepared sockets */
209 struct net *pppol2tp_net; /* the net we belong to */
211 atomic_t ref_count;
214 /* Private data stored for received packets in the skb.
216 struct pppol2tp_skb_cb {
217 u16 ns;
218 u16 nr;
219 u16 has_seq;
220 u16 length;
221 unsigned long expires;
224 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
226 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
227 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel);
229 static atomic_t pppol2tp_tunnel_count;
230 static atomic_t pppol2tp_session_count;
231 static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
232 static const struct proto_ops pppol2tp_ops;
234 /* per-net private data for this module */
235 static int pppol2tp_net_id __read_mostly;
236 struct pppol2tp_net {
237 struct list_head pppol2tp_tunnel_list;
238 rwlock_t pppol2tp_tunnel_list_lock;
241 static inline struct pppol2tp_net *pppol2tp_pernet(struct net *net)
243 BUG_ON(!net);
245 return net_generic(net, pppol2tp_net_id);
248 /* Helpers to obtain tunnel/session contexts from sockets.
250 static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk)
252 struct pppol2tp_session *session;
254 if (sk == NULL)
255 return NULL;
257 sock_hold(sk);
258 session = (struct pppol2tp_session *)(sk->sk_user_data);
259 if (session == NULL) {
260 sock_put(sk);
261 goto out;
264 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
265 out:
266 return session;
269 static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk)
271 struct pppol2tp_tunnel *tunnel;
273 if (sk == NULL)
274 return NULL;
276 sock_hold(sk);
277 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data);
278 if (tunnel == NULL) {
279 sock_put(sk);
280 goto out;
283 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
284 out:
285 return tunnel;
288 /* Tunnel reference counts. Incremented per session that is added to
289 * the tunnel.
291 static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel)
293 atomic_inc(&tunnel->ref_count);
296 static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel)
298 if (atomic_dec_and_test(&tunnel->ref_count))
299 pppol2tp_tunnel_free(tunnel);
302 /* Session hash list.
303 * The session_id SHOULD be random according to RFC2661, but several
304 * L2TP implementations (Cisco and Microsoft) use incrementing
305 * session_ids. So we do a real hash on the session_id, rather than a
306 * simple bitmask.
308 static inline struct hlist_head *
309 pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id)
311 unsigned long hash_val = (unsigned long) session_id;
312 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)];
315 /* Lookup a session by id
317 static struct pppol2tp_session *
318 pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id)
320 struct hlist_head *session_list =
321 pppol2tp_session_id_hash(tunnel, session_id);
322 struct pppol2tp_session *session;
323 struct hlist_node *walk;
325 read_lock_bh(&tunnel->hlist_lock);
326 hlist_for_each_entry(session, walk, session_list, hlist) {
327 if (session->tunnel_addr.s_session == session_id) {
328 read_unlock_bh(&tunnel->hlist_lock);
329 return session;
332 read_unlock_bh(&tunnel->hlist_lock);
334 return NULL;
337 /* Lookup a tunnel by id
339 static struct pppol2tp_tunnel *pppol2tp_tunnel_find(struct net *net, u16 tunnel_id)
341 struct pppol2tp_tunnel *tunnel;
342 struct pppol2tp_net *pn = pppol2tp_pernet(net);
344 read_lock_bh(&pn->pppol2tp_tunnel_list_lock);
345 list_for_each_entry(tunnel, &pn->pppol2tp_tunnel_list, list) {
346 if (tunnel->stats.tunnel_id == tunnel_id) {
347 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
348 return tunnel;
351 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
353 return NULL;
356 /*****************************************************************************
357 * Receive data handling
358 *****************************************************************************/
360 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
361 * number.
363 static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
365 struct sk_buff *skbp;
366 struct sk_buff *tmp;
367 u16 ns = PPPOL2TP_SKB_CB(skb)->ns;
369 spin_lock_bh(&session->reorder_q.lock);
370 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
371 if (PPPOL2TP_SKB_CB(skbp)->ns > ns) {
372 __skb_queue_before(&session->reorder_q, skbp, skb);
373 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
374 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
375 session->name, ns, PPPOL2TP_SKB_CB(skbp)->ns,
376 skb_queue_len(&session->reorder_q));
377 session->stats.rx_oos_packets++;
378 goto out;
382 __skb_queue_tail(&session->reorder_q, skb);
384 out:
385 spin_unlock_bh(&session->reorder_q.lock);
388 /* Dequeue a single skb.
390 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
392 struct pppol2tp_tunnel *tunnel = session->tunnel;
393 int length = PPPOL2TP_SKB_CB(skb)->length;
394 struct sock *session_sock = NULL;
396 /* We're about to requeue the skb, so return resources
397 * to its current owner (a socket receive buffer).
399 skb_orphan(skb);
401 tunnel->stats.rx_packets++;
402 tunnel->stats.rx_bytes += length;
403 session->stats.rx_packets++;
404 session->stats.rx_bytes += length;
406 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
407 /* Bump our Nr */
408 session->nr++;
409 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
410 "%s: updated nr to %hu\n", session->name, session->nr);
413 /* If the socket is bound, send it in to PPP's input queue. Otherwise
414 * queue it on the session socket.
416 session_sock = session->sock;
417 if (session_sock->sk_state & PPPOX_BOUND) {
418 struct pppox_sock *po;
419 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
420 "%s: recv %d byte data frame, passing to ppp\n",
421 session->name, length);
423 /* We need to forget all info related to the L2TP packet
424 * gathered in the skb as we are going to reuse the same
425 * skb for the inner packet.
426 * Namely we need to:
427 * - reset xfrm (IPSec) information as it applies to
428 * the outer L2TP packet and not to the inner one
429 * - release the dst to force a route lookup on the inner
430 * IP packet since skb->dst currently points to the dst
431 * of the UDP tunnel
432 * - reset netfilter information as it doesn't apply
433 * to the inner packet either
435 secpath_reset(skb);
436 skb_dst_drop(skb);
437 nf_reset(skb);
439 po = pppox_sk(session_sock);
440 ppp_input(&po->chan, skb);
441 } else {
442 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
443 "%s: socket not bound\n", session->name);
445 /* Not bound. Nothing we can do, so discard. */
446 session->stats.rx_errors++;
447 kfree_skb(skb);
450 sock_put(session->sock);
453 /* Dequeue skbs from the session's reorder_q, subject to packet order.
454 * Skbs that have been in the queue for too long are simply discarded.
456 static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
458 struct sk_buff *skb;
459 struct sk_buff *tmp;
461 /* If the pkt at the head of the queue has the nr that we
462 * expect to send up next, dequeue it and any other
463 * in-sequence packets behind it.
465 spin_lock_bh(&session->reorder_q.lock);
466 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
467 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
468 session->stats.rx_seq_discards++;
469 session->stats.rx_errors++;
470 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
471 "%s: oos pkt %hu len %d discarded (too old), "
472 "waiting for %hu, reorder_q_len=%d\n",
473 session->name, PPPOL2TP_SKB_CB(skb)->ns,
474 PPPOL2TP_SKB_CB(skb)->length, session->nr,
475 skb_queue_len(&session->reorder_q));
476 __skb_unlink(skb, &session->reorder_q);
477 kfree_skb(skb);
478 sock_put(session->sock);
479 continue;
482 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
483 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
484 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
485 "%s: holding oos pkt %hu len %d, "
486 "waiting for %hu, reorder_q_len=%d\n",
487 session->name, PPPOL2TP_SKB_CB(skb)->ns,
488 PPPOL2TP_SKB_CB(skb)->length, session->nr,
489 skb_queue_len(&session->reorder_q));
490 goto out;
493 __skb_unlink(skb, &session->reorder_q);
495 /* Process the skb. We release the queue lock while we
496 * do so to let other contexts process the queue.
498 spin_unlock_bh(&session->reorder_q.lock);
499 pppol2tp_recv_dequeue_skb(session, skb);
500 spin_lock_bh(&session->reorder_q.lock);
503 out:
504 spin_unlock_bh(&session->reorder_q.lock);
507 static inline int pppol2tp_verify_udp_checksum(struct sock *sk,
508 struct sk_buff *skb)
510 struct udphdr *uh = udp_hdr(skb);
511 u16 ulen = ntohs(uh->len);
512 struct inet_sock *inet;
513 __wsum psum;
515 if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
516 return 0;
518 inet = inet_sk(sk);
519 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
520 IPPROTO_UDP, 0);
522 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
523 !csum_fold(csum_add(psum, skb->csum)))
524 return 0;
526 skb->csum = psum;
528 return __skb_checksum_complete(skb);
531 /* Internal receive frame. Do the real work of receiving an L2TP data frame
532 * here. The skb is not on a list when we get here.
533 * Returns 0 if the packet was a data packet and was successfully passed on.
534 * Returns 1 if the packet was not a good data packet and could not be
535 * forwarded. All such packets are passed up to userspace to deal with.
537 static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
539 struct pppol2tp_session *session = NULL;
540 struct pppol2tp_tunnel *tunnel;
541 unsigned char *ptr, *optr;
542 u16 hdrflags;
543 u16 tunnel_id, session_id;
544 int length;
545 int offset;
547 tunnel = pppol2tp_sock_to_tunnel(sock);
548 if (tunnel == NULL)
549 goto no_tunnel;
551 if (tunnel->sock && pppol2tp_verify_udp_checksum(tunnel->sock, skb))
552 goto discard_bad_csum;
554 /* UDP always verifies the packet length. */
555 __skb_pull(skb, sizeof(struct udphdr));
557 /* Short packet? */
558 if (!pskb_may_pull(skb, 12)) {
559 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
560 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
561 goto error;
564 /* Point to L2TP header */
565 optr = ptr = skb->data;
567 /* Get L2TP header flags */
568 hdrflags = ntohs(*(__be16*)ptr);
570 /* Trace packet contents, if enabled */
571 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
572 length = min(16u, skb->len);
573 if (!pskb_may_pull(skb, length))
574 goto error;
576 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
578 offset = 0;
579 do {
580 printk(" %02X", ptr[offset]);
581 } while (++offset < length);
583 printk("\n");
586 /* Get length of L2TP packet */
587 length = skb->len;
589 /* If type is control packet, it is handled by userspace. */
590 if (hdrflags & L2TP_HDRFLAG_T) {
591 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
592 "%s: recv control packet, len=%d\n", tunnel->name, length);
593 goto error;
596 /* Skip flags */
597 ptr += 2;
599 /* If length is present, skip it */
600 if (hdrflags & L2TP_HDRFLAG_L)
601 ptr += 2;
603 /* Extract tunnel and session ID */
604 tunnel_id = ntohs(*(__be16 *) ptr);
605 ptr += 2;
606 session_id = ntohs(*(__be16 *) ptr);
607 ptr += 2;
609 /* Find the session context */
610 session = pppol2tp_session_find(tunnel, session_id);
611 if (!session) {
612 /* Not found? Pass to userspace to deal with */
613 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
614 "%s: no socket found (%hu/%hu). Passing up.\n",
615 tunnel->name, tunnel_id, session_id);
616 goto error;
618 sock_hold(session->sock);
620 /* The ref count on the socket was increased by the above call since
621 * we now hold a pointer to the session. Take care to do sock_put()
622 * when exiting this function from now on...
625 /* Handle the optional sequence numbers. If we are the LAC,
626 * enable/disable sequence numbers under the control of the LNS. If
627 * no sequence numbers present but we were expecting them, discard
628 * frame.
630 if (hdrflags & L2TP_HDRFLAG_S) {
631 u16 ns, nr;
632 ns = ntohs(*(__be16 *) ptr);
633 ptr += 2;
634 nr = ntohs(*(__be16 *) ptr);
635 ptr += 2;
637 /* Received a packet with sequence numbers. If we're the LNS,
638 * check if we sre sending sequence numbers and if not,
639 * configure it so.
641 if ((!session->lns_mode) && (!session->send_seq)) {
642 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
643 "%s: requested to enable seq numbers by LNS\n",
644 session->name);
645 session->send_seq = -1;
648 /* Store L2TP info in the skb */
649 PPPOL2TP_SKB_CB(skb)->ns = ns;
650 PPPOL2TP_SKB_CB(skb)->nr = nr;
651 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
653 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
654 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
655 session->name, ns, nr, session->nr);
656 } else {
657 /* No sequence numbers.
658 * If user has configured mandatory sequence numbers, discard.
660 if (session->recv_seq) {
661 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
662 "%s: recv data has no seq numbers when required. "
663 "Discarding\n", session->name);
664 session->stats.rx_seq_discards++;
665 goto discard;
668 /* If we're the LAC and we're sending sequence numbers, the
669 * LNS has requested that we no longer send sequence numbers.
670 * If we're the LNS and we're sending sequence numbers, the
671 * LAC is broken. Discard the frame.
673 if ((!session->lns_mode) && (session->send_seq)) {
674 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
675 "%s: requested to disable seq numbers by LNS\n",
676 session->name);
677 session->send_seq = 0;
678 } else if (session->send_seq) {
679 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
680 "%s: recv data has no seq numbers when required. "
681 "Discarding\n", session->name);
682 session->stats.rx_seq_discards++;
683 goto discard;
686 /* Store L2TP info in the skb */
687 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
690 /* If offset bit set, skip it. */
691 if (hdrflags & L2TP_HDRFLAG_O) {
692 offset = ntohs(*(__be16 *)ptr);
693 ptr += 2 + offset;
696 offset = ptr - optr;
697 if (!pskb_may_pull(skb, offset))
698 goto discard;
700 __skb_pull(skb, offset);
702 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
703 * don't send the PPP header (PPP header compression enabled), but
704 * other clients can include the header. So we cope with both cases
705 * here. The PPP header is always FF03 when using L2TP.
707 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
708 * the field may be unaligned.
710 if (!pskb_may_pull(skb, 2))
711 goto discard;
713 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
714 skb_pull(skb, 2);
716 /* Prepare skb for adding to the session's reorder_q. Hold
717 * packets for max reorder_timeout or 1 second if not
718 * reordering.
720 PPPOL2TP_SKB_CB(skb)->length = length;
721 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
722 (session->reorder_timeout ? session->reorder_timeout : HZ);
724 /* Add packet to the session's receive queue. Reordering is done here, if
725 * enabled. Saved L2TP protocol info is stored in skb->sb[].
727 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
728 if (session->reorder_timeout != 0) {
729 /* Packet reordering enabled. Add skb to session's
730 * reorder queue, in order of ns.
732 pppol2tp_recv_queue_skb(session, skb);
733 } else {
734 /* Packet reordering disabled. Discard out-of-sequence
735 * packets
737 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
738 session->stats.rx_seq_discards++;
739 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
740 "%s: oos pkt %hu len %d discarded, "
741 "waiting for %hu, reorder_q_len=%d\n",
742 session->name, PPPOL2TP_SKB_CB(skb)->ns,
743 PPPOL2TP_SKB_CB(skb)->length, session->nr,
744 skb_queue_len(&session->reorder_q));
745 goto discard;
747 skb_queue_tail(&session->reorder_q, skb);
749 } else {
750 /* No sequence numbers. Add the skb to the tail of the
751 * reorder queue. This ensures that it will be
752 * delivered after all previous sequenced skbs.
754 skb_queue_tail(&session->reorder_q, skb);
757 /* Try to dequeue as many skbs from reorder_q as we can. */
758 pppol2tp_recv_dequeue(session);
759 sock_put(sock);
761 return 0;
763 discard:
764 session->stats.rx_errors++;
765 kfree_skb(skb);
766 sock_put(session->sock);
767 sock_put(sock);
769 return 0;
771 discard_bad_csum:
772 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
773 UDP_INC_STATS_USER(&init_net, UDP_MIB_INERRORS, 0);
774 tunnel->stats.rx_errors++;
775 kfree_skb(skb);
776 sock_put(sock);
778 return 0;
780 error:
781 /* Put UDP header back */
782 __skb_push(skb, sizeof(struct udphdr));
783 sock_put(sock);
785 no_tunnel:
786 return 1;
789 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
790 * Return codes:
791 * 0 : success.
792 * <0: error
793 * >0: skb should be passed up to userspace as UDP.
795 static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
797 struct pppol2tp_tunnel *tunnel;
799 tunnel = pppol2tp_sock_to_tunnel(sk);
800 if (tunnel == NULL)
801 goto pass_up;
803 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
804 "%s: received %d bytes\n", tunnel->name, skb->len);
806 if (pppol2tp_recv_core(sk, skb))
807 goto pass_up_put;
809 sock_put(sk);
810 return 0;
812 pass_up_put:
813 sock_put(sk);
814 pass_up:
815 return 1;
818 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
820 static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
821 struct msghdr *msg, size_t len,
822 int flags)
824 int err;
825 struct sk_buff *skb;
826 struct sock *sk = sock->sk;
828 err = -EIO;
829 if (sk->sk_state & PPPOX_BOUND)
830 goto end;
832 msg->msg_namelen = 0;
834 err = 0;
835 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
836 flags & MSG_DONTWAIT, &err);
837 if (!skb)
838 goto end;
840 if (len > skb->len)
841 len = skb->len;
842 else if (len < skb->len)
843 msg->msg_flags |= MSG_TRUNC;
845 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
846 if (likely(err == 0))
847 err = len;
849 kfree_skb(skb);
850 end:
851 return err;
854 /************************************************************************
855 * Transmit handling
856 ***********************************************************************/
858 /* Tell how big L2TP headers are for a particular session. This
859 * depends on whether sequence numbers are being used.
861 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
863 if (session->send_seq)
864 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
866 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
869 /* Build an L2TP header for the session into the buffer provided.
871 static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
872 void *buf)
874 __be16 *bufp = buf;
875 u16 flags = L2TP_HDR_VER;
877 if (session->send_seq)
878 flags |= L2TP_HDRFLAG_S;
880 /* Setup L2TP header.
881 * FIXME: Can this ever be unaligned? Is direct dereferencing of
882 * 16-bit header fields safe here for all architectures?
884 *bufp++ = htons(flags);
885 *bufp++ = htons(session->tunnel_addr.d_tunnel);
886 *bufp++ = htons(session->tunnel_addr.d_session);
887 if (session->send_seq) {
888 *bufp++ = htons(session->ns);
889 *bufp++ = 0;
890 session->ns++;
891 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
892 "%s: updated ns to %hu\n", session->name, session->ns);
896 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
897 * when a user application does a sendmsg() on the session socket. L2TP and
898 * PPP headers must be inserted into the user's data.
900 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
901 size_t total_len)
903 static const unsigned char ppph[2] = { 0xff, 0x03 };
904 struct sock *sk = sock->sk;
905 struct inet_sock *inet;
906 __wsum csum;
907 struct sk_buff *skb;
908 int error;
909 int hdr_len;
910 struct pppol2tp_session *session;
911 struct pppol2tp_tunnel *tunnel;
912 struct udphdr *uh;
913 unsigned int len;
914 struct sock *sk_tun;
915 u16 udp_len;
917 error = -ENOTCONN;
918 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
919 goto error;
921 /* Get session and tunnel contexts */
922 error = -EBADF;
923 session = pppol2tp_sock_to_session(sk);
924 if (session == NULL)
925 goto error;
927 sk_tun = session->tunnel_sock;
928 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
929 if (tunnel == NULL)
930 goto error_put_sess;
932 /* What header length is configured for this session? */
933 hdr_len = pppol2tp_l2tp_header_len(session);
935 /* Allocate a socket buffer */
936 error = -ENOMEM;
937 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
938 sizeof(struct udphdr) + hdr_len +
939 sizeof(ppph) + total_len,
940 0, GFP_KERNEL);
941 if (!skb)
942 goto error_put_sess_tun;
944 /* Reserve space for headers. */
945 skb_reserve(skb, NET_SKB_PAD);
946 skb_reset_network_header(skb);
947 skb_reserve(skb, sizeof(struct iphdr));
948 skb_reset_transport_header(skb);
950 /* Build UDP header */
951 inet = inet_sk(sk_tun);
952 udp_len = hdr_len + sizeof(ppph) + total_len;
953 uh = (struct udphdr *) skb->data;
954 uh->source = inet->inet_sport;
955 uh->dest = inet->inet_dport;
956 uh->len = htons(udp_len);
957 uh->check = 0;
958 skb_put(skb, sizeof(struct udphdr));
960 /* Build L2TP header */
961 pppol2tp_build_l2tp_header(session, skb->data);
962 skb_put(skb, hdr_len);
964 /* Add PPP header */
965 skb->data[0] = ppph[0];
966 skb->data[1] = ppph[1];
967 skb_put(skb, 2);
969 /* Copy user data into skb */
970 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
971 if (error < 0) {
972 kfree_skb(skb);
973 goto error_put_sess_tun;
975 skb_put(skb, total_len);
977 /* Calculate UDP checksum if configured to do so */
978 if (sk_tun->sk_no_check == UDP_CSUM_NOXMIT)
979 skb->ip_summed = CHECKSUM_NONE;
980 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
981 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
982 skb->ip_summed = CHECKSUM_COMPLETE;
983 csum = skb_checksum(skb, 0, udp_len, 0);
984 uh->check = csum_tcpudp_magic(inet->inet_saddr,
985 inet->inet_daddr,
986 udp_len, IPPROTO_UDP, csum);
987 if (uh->check == 0)
988 uh->check = CSUM_MANGLED_0;
989 } else {
990 skb->ip_summed = CHECKSUM_PARTIAL;
991 skb->csum_start = skb_transport_header(skb) - skb->head;
992 skb->csum_offset = offsetof(struct udphdr, check);
993 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
994 inet->inet_daddr,
995 udp_len, IPPROTO_UDP, 0);
998 /* Debug */
999 if (session->send_seq)
1000 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1001 "%s: send %Zd bytes, ns=%hu\n", session->name,
1002 total_len, session->ns - 1);
1003 else
1004 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1005 "%s: send %Zd bytes\n", session->name, total_len);
1007 if (session->debug & PPPOL2TP_MSG_DATA) {
1008 int i;
1009 unsigned char *datap = skb->data;
1011 printk(KERN_DEBUG "%s: xmit:", session->name);
1012 for (i = 0; i < total_len; i++) {
1013 printk(" %02X", *datap++);
1014 if (i == 15) {
1015 printk(" ...");
1016 break;
1019 printk("\n");
1022 /* Queue the packet to IP for output */
1023 len = skb->len;
1024 error = ip_queue_xmit(skb, 1);
1026 /* Update stats */
1027 if (error >= 0) {
1028 tunnel->stats.tx_packets++;
1029 tunnel->stats.tx_bytes += len;
1030 session->stats.tx_packets++;
1031 session->stats.tx_bytes += len;
1032 } else {
1033 tunnel->stats.tx_errors++;
1034 session->stats.tx_errors++;
1037 return error;
1039 error_put_sess_tun:
1040 sock_put(session->tunnel_sock);
1041 error_put_sess:
1042 sock_put(sk);
1043 error:
1044 return error;
1047 /* Automatically called when the skb is freed.
1049 static void pppol2tp_sock_wfree(struct sk_buff *skb)
1051 sock_put(skb->sk);
1054 /* For data skbs that we transmit, we associate with the tunnel socket
1055 * but don't do accounting.
1057 static inline void pppol2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1059 sock_hold(sk);
1060 skb->sk = sk;
1061 skb->destructor = pppol2tp_sock_wfree;
1064 /* Transmit function called by generic PPP driver. Sends PPP frame
1065 * over PPPoL2TP socket.
1067 * This is almost the same as pppol2tp_sendmsg(), but rather than
1068 * being called with a msghdr from userspace, it is called with a skb
1069 * from the kernel.
1071 * The supplied skb from ppp doesn't have enough headroom for the
1072 * insertion of L2TP, UDP and IP headers so we need to allocate more
1073 * headroom in the skb. This will create a cloned skb. But we must be
1074 * careful in the error case because the caller will expect to free
1075 * the skb it supplied, not our cloned skb. So we take care to always
1076 * leave the original skb unfreed if we return an error.
1078 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
1080 static const u8 ppph[2] = { 0xff, 0x03 };
1081 struct sock *sk = (struct sock *) chan->private;
1082 struct sock *sk_tun;
1083 int hdr_len;
1084 u16 udp_len;
1085 struct pppol2tp_session *session;
1086 struct pppol2tp_tunnel *tunnel;
1087 int rc;
1088 int headroom;
1089 int data_len = skb->len;
1090 struct inet_sock *inet;
1091 __wsum csum;
1092 struct udphdr *uh;
1093 unsigned int len;
1094 int old_headroom;
1095 int new_headroom;
1097 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
1098 goto abort;
1100 /* Get session and tunnel contexts from the socket */
1101 session = pppol2tp_sock_to_session(sk);
1102 if (session == NULL)
1103 goto abort;
1105 sk_tun = session->tunnel_sock;
1106 if (sk_tun == NULL)
1107 goto abort_put_sess;
1108 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
1109 if (tunnel == NULL)
1110 goto abort_put_sess;
1112 /* What header length is configured for this session? */
1113 hdr_len = pppol2tp_l2tp_header_len(session);
1115 /* Check that there's enough headroom in the skb to insert IP,
1116 * UDP and L2TP and PPP headers. If not enough, expand it to
1117 * make room. Adjust truesize.
1119 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1120 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
1121 old_headroom = skb_headroom(skb);
1122 if (skb_cow_head(skb, headroom))
1123 goto abort_put_sess_tun;
1125 new_headroom = skb_headroom(skb);
1126 skb_orphan(skb);
1127 skb->truesize += new_headroom - old_headroom;
1129 /* Setup PPP header */
1130 __skb_push(skb, sizeof(ppph));
1131 skb->data[0] = ppph[0];
1132 skb->data[1] = ppph[1];
1134 /* Setup L2TP header */
1135 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
1137 udp_len = sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len;
1139 /* Setup UDP header */
1140 inet = inet_sk(sk_tun);
1141 __skb_push(skb, sizeof(*uh));
1142 skb_reset_transport_header(skb);
1143 uh = udp_hdr(skb);
1144 uh->source = inet->inet_sport;
1145 uh->dest = inet->inet_dport;
1146 uh->len = htons(udp_len);
1147 uh->check = 0;
1149 /* Debug */
1150 if (session->send_seq)
1151 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1152 "%s: send %d bytes, ns=%hu\n", session->name,
1153 data_len, session->ns - 1);
1154 else
1155 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1156 "%s: send %d bytes\n", session->name, data_len);
1158 if (session->debug & PPPOL2TP_MSG_DATA) {
1159 int i;
1160 unsigned char *datap = skb->data;
1162 printk(KERN_DEBUG "%s: xmit:", session->name);
1163 for (i = 0; i < data_len; i++) {
1164 printk(" %02X", *datap++);
1165 if (i == 31) {
1166 printk(" ...");
1167 break;
1170 printk("\n");
1173 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1174 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1175 IPSKB_REROUTED);
1176 nf_reset(skb);
1178 /* Get routing info from the tunnel socket */
1179 skb_dst_drop(skb);
1180 skb_dst_set(skb, dst_clone(__sk_dst_get(sk_tun)));
1181 pppol2tp_skb_set_owner_w(skb, sk_tun);
1183 /* Calculate UDP checksum if configured to do so */
1184 if (sk_tun->sk_no_check == UDP_CSUM_NOXMIT)
1185 skb->ip_summed = CHECKSUM_NONE;
1186 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1187 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1188 skb->ip_summed = CHECKSUM_COMPLETE;
1189 csum = skb_checksum(skb, 0, udp_len, 0);
1190 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1191 inet->inet_daddr,
1192 udp_len, IPPROTO_UDP, csum);
1193 if (uh->check == 0)
1194 uh->check = CSUM_MANGLED_0;
1195 } else {
1196 skb->ip_summed = CHECKSUM_PARTIAL;
1197 skb->csum_start = skb_transport_header(skb) - skb->head;
1198 skb->csum_offset = offsetof(struct udphdr, check);
1199 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1200 inet->inet_daddr,
1201 udp_len, IPPROTO_UDP, 0);
1204 /* Queue the packet to IP for output */
1205 len = skb->len;
1206 rc = ip_queue_xmit(skb, 1);
1208 /* Update stats */
1209 if (rc >= 0) {
1210 tunnel->stats.tx_packets++;
1211 tunnel->stats.tx_bytes += len;
1212 session->stats.tx_packets++;
1213 session->stats.tx_bytes += len;
1214 } else {
1215 tunnel->stats.tx_errors++;
1216 session->stats.tx_errors++;
1219 sock_put(sk_tun);
1220 sock_put(sk);
1221 return 1;
1223 abort_put_sess_tun:
1224 sock_put(sk_tun);
1225 abort_put_sess:
1226 sock_put(sk);
1227 abort:
1228 /* Free the original skb */
1229 kfree_skb(skb);
1230 return 1;
1233 /*****************************************************************************
1234 * Session (and tunnel control) socket create/destroy.
1235 *****************************************************************************/
1237 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1238 * too.
1240 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1242 int hash;
1243 struct hlist_node *walk;
1244 struct hlist_node *tmp;
1245 struct pppol2tp_session *session;
1246 struct sock *sk;
1248 BUG_ON(tunnel == NULL);
1250 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1251 "%s: closing all sessions...\n", tunnel->name);
1253 write_lock_bh(&tunnel->hlist_lock);
1254 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1255 again:
1256 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1257 struct sk_buff *skb;
1259 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1261 sk = session->sock;
1263 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1264 "%s: closing session\n", session->name);
1266 hlist_del_init(&session->hlist);
1268 /* Since we should hold the sock lock while
1269 * doing any unbinding, we need to release the
1270 * lock we're holding before taking that lock.
1271 * Hold a reference to the sock so it doesn't
1272 * disappear as we're jumping between locks.
1274 sock_hold(sk);
1275 write_unlock_bh(&tunnel->hlist_lock);
1276 lock_sock(sk);
1278 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1279 pppox_unbind_sock(sk);
1280 sk->sk_state = PPPOX_DEAD;
1281 sk->sk_state_change(sk);
1284 /* Purge any queued data */
1285 skb_queue_purge(&sk->sk_receive_queue);
1286 skb_queue_purge(&sk->sk_write_queue);
1287 while ((skb = skb_dequeue(&session->reorder_q))) {
1288 kfree_skb(skb);
1289 sock_put(sk);
1292 release_sock(sk);
1293 sock_put(sk);
1295 /* Now restart from the beginning of this hash
1296 * chain. We always remove a session from the
1297 * list so we are guaranteed to make forward
1298 * progress.
1300 write_lock_bh(&tunnel->hlist_lock);
1301 goto again;
1304 write_unlock_bh(&tunnel->hlist_lock);
1307 /* Really kill the tunnel.
1308 * Come here only when all sessions have been cleared from the tunnel.
1310 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1312 struct pppol2tp_net *pn = pppol2tp_pernet(tunnel->pppol2tp_net);
1314 /* Remove from socket list */
1315 write_lock_bh(&pn->pppol2tp_tunnel_list_lock);
1316 list_del_init(&tunnel->list);
1317 write_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
1319 atomic_dec(&pppol2tp_tunnel_count);
1320 kfree(tunnel);
1323 /* Tunnel UDP socket destruct hook.
1324 * The tunnel context is deleted only when all session sockets have been
1325 * closed.
1327 static void pppol2tp_tunnel_destruct(struct sock *sk)
1329 struct pppol2tp_tunnel *tunnel;
1331 tunnel = sk->sk_user_data;
1332 if (tunnel == NULL)
1333 goto end;
1335 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1336 "%s: closing...\n", tunnel->name);
1338 /* Close all sessions */
1339 pppol2tp_tunnel_closeall(tunnel);
1341 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1342 (udp_sk(sk))->encap_type = 0;
1343 (udp_sk(sk))->encap_rcv = NULL;
1345 /* Remove hooks into tunnel socket */
1346 tunnel->sock = NULL;
1347 sk->sk_destruct = tunnel->old_sk_destruct;
1348 sk->sk_user_data = NULL;
1350 /* Call original (UDP) socket descructor */
1351 if (sk->sk_destruct != NULL)
1352 (*sk->sk_destruct)(sk);
1354 pppol2tp_tunnel_dec_refcount(tunnel);
1356 end:
1357 return;
1360 /* Really kill the session socket. (Called from sock_put() if
1361 * refcnt == 0.)
1363 static void pppol2tp_session_destruct(struct sock *sk)
1365 struct pppol2tp_session *session = NULL;
1367 if (sk->sk_user_data != NULL) {
1368 struct pppol2tp_tunnel *tunnel;
1370 session = sk->sk_user_data;
1371 if (session == NULL)
1372 goto out;
1374 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
1376 /* Don't use pppol2tp_sock_to_tunnel() here to
1377 * get the tunnel context because the tunnel
1378 * socket might have already been closed (its
1379 * sk->sk_user_data will be NULL) so use the
1380 * session's private tunnel ptr instead.
1382 tunnel = session->tunnel;
1383 if (tunnel != NULL) {
1384 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1386 /* If session_id is zero, this is a null
1387 * session context, which was created for a
1388 * socket that is being used only to manage
1389 * tunnels.
1391 if (session->tunnel_addr.s_session != 0) {
1392 /* Delete the session socket from the
1393 * hash
1395 write_lock_bh(&tunnel->hlist_lock);
1396 hlist_del_init(&session->hlist);
1397 write_unlock_bh(&tunnel->hlist_lock);
1399 atomic_dec(&pppol2tp_session_count);
1402 /* This will delete the tunnel context if this
1403 * is the last session on the tunnel.
1405 session->tunnel = NULL;
1406 session->tunnel_sock = NULL;
1407 pppol2tp_tunnel_dec_refcount(tunnel);
1411 kfree(session);
1412 out:
1413 return;
1416 /* Called when the PPPoX socket (session) is closed.
1418 static int pppol2tp_release(struct socket *sock)
1420 struct sock *sk = sock->sk;
1421 struct pppol2tp_session *session;
1422 int error;
1424 if (!sk)
1425 return 0;
1427 error = -EBADF;
1428 lock_sock(sk);
1429 if (sock_flag(sk, SOCK_DEAD) != 0)
1430 goto error;
1432 pppox_unbind_sock(sk);
1434 /* Signal the death of the socket. */
1435 sk->sk_state = PPPOX_DEAD;
1436 sock_orphan(sk);
1437 sock->sk = NULL;
1439 session = pppol2tp_sock_to_session(sk);
1441 /* Purge any queued data */
1442 skb_queue_purge(&sk->sk_receive_queue);
1443 skb_queue_purge(&sk->sk_write_queue);
1444 if (session != NULL) {
1445 struct sk_buff *skb;
1446 while ((skb = skb_dequeue(&session->reorder_q))) {
1447 kfree_skb(skb);
1448 sock_put(sk);
1450 sock_put(sk);
1453 release_sock(sk);
1455 /* This will delete the session context via
1456 * pppol2tp_session_destruct() if the socket's refcnt drops to
1457 * zero.
1459 sock_put(sk);
1461 return 0;
1463 error:
1464 release_sock(sk);
1465 return error;
1468 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1469 * sockets attached to it.
1471 static struct sock *pppol2tp_prepare_tunnel_socket(struct net *net,
1472 int fd, u16 tunnel_id, int *error)
1474 int err;
1475 struct socket *sock = NULL;
1476 struct sock *sk;
1477 struct pppol2tp_tunnel *tunnel;
1478 struct pppol2tp_net *pn;
1479 struct sock *ret = NULL;
1481 /* Get the tunnel UDP socket from the fd, which was opened by
1482 * the userspace L2TP daemon.
1484 err = -EBADF;
1485 sock = sockfd_lookup(fd, &err);
1486 if (!sock) {
1487 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1488 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1489 tunnel_id, fd, err);
1490 goto err;
1493 sk = sock->sk;
1495 /* Quick sanity checks */
1496 err = -EPROTONOSUPPORT;
1497 if (sk->sk_protocol != IPPROTO_UDP) {
1498 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1499 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1500 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1501 goto err;
1503 err = -EAFNOSUPPORT;
1504 if (sock->ops->family != AF_INET) {
1505 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1506 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1507 tunnel_id, fd, sock->ops->family, AF_INET);
1508 goto err;
1511 err = -ENOTCONN;
1513 /* Check if this socket has already been prepped */
1514 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1515 if (tunnel != NULL) {
1516 /* User-data field already set */
1517 err = -EBUSY;
1518 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1520 /* This socket has already been prepped */
1521 ret = tunnel->sock;
1522 goto out;
1525 /* This socket is available and needs prepping. Create a new tunnel
1526 * context and init it.
1528 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1529 if (sk->sk_user_data == NULL) {
1530 err = -ENOMEM;
1531 goto err;
1534 tunnel->magic = L2TP_TUNNEL_MAGIC;
1535 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1537 tunnel->stats.tunnel_id = tunnel_id;
1538 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1540 /* Hook on the tunnel socket destructor so that we can cleanup
1541 * if the tunnel socket goes away.
1543 tunnel->old_sk_destruct = sk->sk_destruct;
1544 sk->sk_destruct = pppol2tp_tunnel_destruct;
1546 tunnel->sock = sk;
1547 sk->sk_allocation = GFP_ATOMIC;
1549 /* Misc init */
1550 rwlock_init(&tunnel->hlist_lock);
1552 /* The net we belong to */
1553 tunnel->pppol2tp_net = net;
1554 pn = pppol2tp_pernet(net);
1556 /* Add tunnel to our list */
1557 INIT_LIST_HEAD(&tunnel->list);
1558 write_lock_bh(&pn->pppol2tp_tunnel_list_lock);
1559 list_add(&tunnel->list, &pn->pppol2tp_tunnel_list);
1560 write_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
1561 atomic_inc(&pppol2tp_tunnel_count);
1563 /* Bump the reference count. The tunnel context is deleted
1564 * only when this drops to zero.
1566 pppol2tp_tunnel_inc_refcount(tunnel);
1568 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1569 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1570 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1572 ret = tunnel->sock;
1574 *error = 0;
1575 out:
1576 if (sock)
1577 sockfd_put(sock);
1579 return ret;
1581 err:
1582 *error = err;
1583 goto out;
1586 static struct proto pppol2tp_sk_proto = {
1587 .name = "PPPOL2TP",
1588 .owner = THIS_MODULE,
1589 .obj_size = sizeof(struct pppox_sock),
1592 /* socket() handler. Initialize a new struct sock.
1594 static int pppol2tp_create(struct net *net, struct socket *sock)
1596 int error = -ENOMEM;
1597 struct sock *sk;
1599 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
1600 if (!sk)
1601 goto out;
1603 sock_init_data(sock, sk);
1605 sock->state = SS_UNCONNECTED;
1606 sock->ops = &pppol2tp_ops;
1608 sk->sk_backlog_rcv = pppol2tp_recv_core;
1609 sk->sk_protocol = PX_PROTO_OL2TP;
1610 sk->sk_family = PF_PPPOX;
1611 sk->sk_state = PPPOX_NONE;
1612 sk->sk_type = SOCK_STREAM;
1613 sk->sk_destruct = pppol2tp_session_destruct;
1615 error = 0;
1617 out:
1618 return error;
1621 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1623 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1624 int sockaddr_len, int flags)
1626 struct sock *sk = sock->sk;
1627 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1628 struct pppox_sock *po = pppox_sk(sk);
1629 struct sock *tunnel_sock = NULL;
1630 struct pppol2tp_session *session = NULL;
1631 struct pppol2tp_tunnel *tunnel;
1632 struct dst_entry *dst;
1633 int error = 0;
1635 lock_sock(sk);
1637 error = -EINVAL;
1638 if (sp->sa_protocol != PX_PROTO_OL2TP)
1639 goto end;
1641 /* Check for already bound sockets */
1642 error = -EBUSY;
1643 if (sk->sk_state & PPPOX_CONNECTED)
1644 goto end;
1646 /* We don't supporting rebinding anyway */
1647 error = -EALREADY;
1648 if (sk->sk_user_data)
1649 goto end; /* socket is already attached */
1651 /* Don't bind if s_tunnel is 0 */
1652 error = -EINVAL;
1653 if (sp->pppol2tp.s_tunnel == 0)
1654 goto end;
1656 /* Special case: prepare tunnel socket if s_session and
1657 * d_session is 0. Otherwise look up tunnel using supplied
1658 * tunnel id.
1660 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
1661 tunnel_sock = pppol2tp_prepare_tunnel_socket(sock_net(sk),
1662 sp->pppol2tp.fd,
1663 sp->pppol2tp.s_tunnel,
1664 &error);
1665 if (tunnel_sock == NULL)
1666 goto end;
1668 sock_hold(tunnel_sock);
1669 tunnel = tunnel_sock->sk_user_data;
1670 } else {
1671 tunnel = pppol2tp_tunnel_find(sock_net(sk), sp->pppol2tp.s_tunnel);
1673 /* Error if we can't find the tunnel */
1674 error = -ENOENT;
1675 if (tunnel == NULL)
1676 goto end;
1678 tunnel_sock = tunnel->sock;
1681 /* Check that this session doesn't already exist */
1682 error = -EEXIST;
1683 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1684 if (session != NULL)
1685 goto end;
1687 /* Allocate and initialize a new session context. */
1688 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1689 if (session == NULL) {
1690 error = -ENOMEM;
1691 goto end;
1694 skb_queue_head_init(&session->reorder_q);
1696 session->magic = L2TP_SESSION_MAGIC;
1697 session->owner = current->pid;
1698 session->sock = sk;
1699 session->tunnel = tunnel;
1700 session->tunnel_sock = tunnel_sock;
1701 session->tunnel_addr = sp->pppol2tp;
1702 sprintf(&session->name[0], "sess %hu/%hu",
1703 session->tunnel_addr.s_tunnel,
1704 session->tunnel_addr.s_session);
1706 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1707 session->stats.session_id = session->tunnel_addr.s_session;
1709 INIT_HLIST_NODE(&session->hlist);
1711 /* Inherit debug options from tunnel */
1712 session->debug = tunnel->debug;
1714 /* Default MTU must allow space for UDP/L2TP/PPP
1715 * headers.
1717 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1719 /* If PMTU discovery was enabled, use the MTU that was discovered */
1720 dst = sk_dst_get(sk);
1721 if (dst != NULL) {
1722 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1723 if (pmtu != 0)
1724 session->mtu = session->mru = pmtu -
1725 PPPOL2TP_HEADER_OVERHEAD;
1726 dst_release(dst);
1729 /* Special case: if source & dest session_id == 0x0000, this socket is
1730 * being created to manage the tunnel. Don't add the session to the
1731 * session hash list, just set up the internal context for use by
1732 * ioctl() and sockopt() handlers.
1734 if ((session->tunnel_addr.s_session == 0) &&
1735 (session->tunnel_addr.d_session == 0)) {
1736 error = 0;
1737 sk->sk_user_data = session;
1738 goto out_no_ppp;
1741 /* Get tunnel context from the tunnel socket */
1742 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1743 if (tunnel == NULL) {
1744 error = -EBADF;
1745 goto end;
1748 /* Right now, because we don't have a way to push the incoming skb's
1749 * straight through the UDP layer, the only header we need to worry
1750 * about is the L2TP header. This size is different depending on
1751 * whether sequence numbers are enabled for the data channel.
1753 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1755 po->chan.private = sk;
1756 po->chan.ops = &pppol2tp_chan_ops;
1757 po->chan.mtu = session->mtu;
1759 error = ppp_register_net_channel(sock_net(sk), &po->chan);
1760 if (error)
1761 goto end_put_tun;
1763 /* This is how we get the session context from the socket. */
1764 sk->sk_user_data = session;
1766 /* Add session to the tunnel's hash list */
1767 write_lock_bh(&tunnel->hlist_lock);
1768 hlist_add_head(&session->hlist,
1769 pppol2tp_session_id_hash(tunnel,
1770 session->tunnel_addr.s_session));
1771 write_unlock_bh(&tunnel->hlist_lock);
1773 atomic_inc(&pppol2tp_session_count);
1775 out_no_ppp:
1776 pppol2tp_tunnel_inc_refcount(tunnel);
1777 sk->sk_state = PPPOX_CONNECTED;
1778 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1779 "%s: created\n", session->name);
1781 end_put_tun:
1782 sock_put(tunnel_sock);
1783 end:
1784 release_sock(sk);
1786 if (error != 0) {
1787 if (session)
1788 PRINTK(session->debug,
1789 PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1790 "%s: connect failed: %d\n",
1791 session->name, error);
1792 else
1793 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1794 "connect failed: %d\n", error);
1797 return error;
1800 /* getname() support.
1802 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1803 int *usockaddr_len, int peer)
1805 int len = sizeof(struct sockaddr_pppol2tp);
1806 struct sockaddr_pppol2tp sp;
1807 int error = 0;
1808 struct pppol2tp_session *session;
1810 error = -ENOTCONN;
1811 if (sock->sk->sk_state != PPPOX_CONNECTED)
1812 goto end;
1814 session = pppol2tp_sock_to_session(sock->sk);
1815 if (session == NULL) {
1816 error = -EBADF;
1817 goto end;
1820 sp.sa_family = AF_PPPOX;
1821 sp.sa_protocol = PX_PROTO_OL2TP;
1822 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1823 sizeof(struct pppol2tp_addr));
1825 memcpy(uaddr, &sp, len);
1827 *usockaddr_len = len;
1829 error = 0;
1830 sock_put(sock->sk);
1832 end:
1833 return error;
1836 /****************************************************************************
1837 * ioctl() handlers.
1839 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1840 * sockets. However, in order to control kernel tunnel features, we allow
1841 * userspace to create a special "tunnel" PPPoX socket which is used for
1842 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1843 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1844 * calls.
1845 ****************************************************************************/
1847 /* Session ioctl helper.
1849 static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1850 unsigned int cmd, unsigned long arg)
1852 struct ifreq ifr;
1853 int err = 0;
1854 struct sock *sk = session->sock;
1855 int val = (int) arg;
1857 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1858 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1859 session->name, cmd, arg);
1861 sock_hold(sk);
1863 switch (cmd) {
1864 case SIOCGIFMTU:
1865 err = -ENXIO;
1866 if (!(sk->sk_state & PPPOX_CONNECTED))
1867 break;
1869 err = -EFAULT;
1870 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1871 break;
1872 ifr.ifr_mtu = session->mtu;
1873 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1874 break;
1876 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1877 "%s: get mtu=%d\n", session->name, session->mtu);
1878 err = 0;
1879 break;
1881 case SIOCSIFMTU:
1882 err = -ENXIO;
1883 if (!(sk->sk_state & PPPOX_CONNECTED))
1884 break;
1886 err = -EFAULT;
1887 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1888 break;
1890 session->mtu = ifr.ifr_mtu;
1892 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1893 "%s: set mtu=%d\n", session->name, session->mtu);
1894 err = 0;
1895 break;
1897 case PPPIOCGMRU:
1898 err = -ENXIO;
1899 if (!(sk->sk_state & PPPOX_CONNECTED))
1900 break;
1902 err = -EFAULT;
1903 if (put_user(session->mru, (int __user *) arg))
1904 break;
1906 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1907 "%s: get mru=%d\n", session->name, session->mru);
1908 err = 0;
1909 break;
1911 case PPPIOCSMRU:
1912 err = -ENXIO;
1913 if (!(sk->sk_state & PPPOX_CONNECTED))
1914 break;
1916 err = -EFAULT;
1917 if (get_user(val,(int __user *) arg))
1918 break;
1920 session->mru = val;
1921 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1922 "%s: set mru=%d\n", session->name, session->mru);
1923 err = 0;
1924 break;
1926 case PPPIOCGFLAGS:
1927 err = -EFAULT;
1928 if (put_user(session->flags, (int __user *) arg))
1929 break;
1931 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1932 "%s: get flags=%d\n", session->name, session->flags);
1933 err = 0;
1934 break;
1936 case PPPIOCSFLAGS:
1937 err = -EFAULT;
1938 if (get_user(val, (int __user *) arg))
1939 break;
1940 session->flags = val;
1941 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1942 "%s: set flags=%d\n", session->name, session->flags);
1943 err = 0;
1944 break;
1946 case PPPIOCGL2TPSTATS:
1947 err = -ENXIO;
1948 if (!(sk->sk_state & PPPOX_CONNECTED))
1949 break;
1951 if (copy_to_user((void __user *) arg, &session->stats,
1952 sizeof(session->stats)))
1953 break;
1954 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1955 "%s: get L2TP stats\n", session->name);
1956 err = 0;
1957 break;
1959 default:
1960 err = -ENOSYS;
1961 break;
1964 sock_put(sk);
1966 return err;
1969 /* Tunnel ioctl helper.
1971 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1972 * specifies a session_id, the session ioctl handler is called. This allows an
1973 * application to retrieve session stats via a tunnel socket.
1975 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1976 unsigned int cmd, unsigned long arg)
1978 int err = 0;
1979 struct sock *sk = tunnel->sock;
1980 struct pppol2tp_ioc_stats stats_req;
1982 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1983 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1984 cmd, arg);
1986 sock_hold(sk);
1988 switch (cmd) {
1989 case PPPIOCGL2TPSTATS:
1990 err = -ENXIO;
1991 if (!(sk->sk_state & PPPOX_CONNECTED))
1992 break;
1994 if (copy_from_user(&stats_req, (void __user *) arg,
1995 sizeof(stats_req))) {
1996 err = -EFAULT;
1997 break;
1999 if (stats_req.session_id != 0) {
2000 /* resend to session ioctl handler */
2001 struct pppol2tp_session *session =
2002 pppol2tp_session_find(tunnel, stats_req.session_id);
2003 if (session != NULL)
2004 err = pppol2tp_session_ioctl(session, cmd, arg);
2005 else
2006 err = -EBADR;
2007 break;
2009 #ifdef CONFIG_XFRM
2010 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
2011 #endif
2012 if (copy_to_user((void __user *) arg, &tunnel->stats,
2013 sizeof(tunnel->stats))) {
2014 err = -EFAULT;
2015 break;
2017 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2018 "%s: get L2TP stats\n", tunnel->name);
2019 err = 0;
2020 break;
2022 default:
2023 err = -ENOSYS;
2024 break;
2027 sock_put(sk);
2029 return err;
2032 /* Main ioctl() handler.
2033 * Dispatch to tunnel or session helpers depending on the socket.
2035 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
2036 unsigned long arg)
2038 struct sock *sk = sock->sk;
2039 struct pppol2tp_session *session;
2040 struct pppol2tp_tunnel *tunnel;
2041 int err;
2043 if (!sk)
2044 return 0;
2046 err = -EBADF;
2047 if (sock_flag(sk, SOCK_DEAD) != 0)
2048 goto end;
2050 err = -ENOTCONN;
2051 if ((sk->sk_user_data == NULL) ||
2052 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
2053 goto end;
2055 /* Get session context from the socket */
2056 err = -EBADF;
2057 session = pppol2tp_sock_to_session(sk);
2058 if (session == NULL)
2059 goto end;
2061 /* Special case: if session's session_id is zero, treat ioctl as a
2062 * tunnel ioctl
2064 if ((session->tunnel_addr.s_session == 0) &&
2065 (session->tunnel_addr.d_session == 0)) {
2066 err = -EBADF;
2067 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2068 if (tunnel == NULL)
2069 goto end_put_sess;
2071 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
2072 sock_put(session->tunnel_sock);
2073 goto end_put_sess;
2076 err = pppol2tp_session_ioctl(session, cmd, arg);
2078 end_put_sess:
2079 sock_put(sk);
2080 end:
2081 return err;
2084 /*****************************************************************************
2085 * setsockopt() / getsockopt() support.
2087 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2088 * sockets. In order to control kernel tunnel features, we allow userspace to
2089 * create a special "tunnel" PPPoX socket which is used for control only.
2090 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2091 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2092 *****************************************************************************/
2094 /* Tunnel setsockopt() helper.
2096 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
2097 struct pppol2tp_tunnel *tunnel,
2098 int optname, int val)
2100 int err = 0;
2102 switch (optname) {
2103 case PPPOL2TP_SO_DEBUG:
2104 tunnel->debug = val;
2105 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2106 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
2107 break;
2109 default:
2110 err = -ENOPROTOOPT;
2111 break;
2114 return err;
2117 /* Session setsockopt helper.
2119 static int pppol2tp_session_setsockopt(struct sock *sk,
2120 struct pppol2tp_session *session,
2121 int optname, int val)
2123 int err = 0;
2125 switch (optname) {
2126 case PPPOL2TP_SO_RECVSEQ:
2127 if ((val != 0) && (val != 1)) {
2128 err = -EINVAL;
2129 break;
2131 session->recv_seq = val ? -1 : 0;
2132 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2133 "%s: set recv_seq=%d\n", session->name,
2134 session->recv_seq);
2135 break;
2137 case PPPOL2TP_SO_SENDSEQ:
2138 if ((val != 0) && (val != 1)) {
2139 err = -EINVAL;
2140 break;
2142 session->send_seq = val ? -1 : 0;
2144 struct sock *ssk = session->sock;
2145 struct pppox_sock *po = pppox_sk(ssk);
2146 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
2147 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
2149 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2150 "%s: set send_seq=%d\n", session->name, session->send_seq);
2151 break;
2153 case PPPOL2TP_SO_LNSMODE:
2154 if ((val != 0) && (val != 1)) {
2155 err = -EINVAL;
2156 break;
2158 session->lns_mode = val ? -1 : 0;
2159 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2160 "%s: set lns_mode=%d\n", session->name,
2161 session->lns_mode);
2162 break;
2164 case PPPOL2TP_SO_DEBUG:
2165 session->debug = val;
2166 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2167 "%s: set debug=%x\n", session->name, session->debug);
2168 break;
2170 case PPPOL2TP_SO_REORDERTO:
2171 session->reorder_timeout = msecs_to_jiffies(val);
2172 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2173 "%s: set reorder_timeout=%d\n", session->name,
2174 session->reorder_timeout);
2175 break;
2177 default:
2178 err = -ENOPROTOOPT;
2179 break;
2182 return err;
2185 /* Main setsockopt() entry point.
2186 * Does API checks, then calls either the tunnel or session setsockopt
2187 * handler, according to whether the PPPoL2TP socket is a for a regular
2188 * session or the special tunnel type.
2190 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
2191 char __user *optval, unsigned int optlen)
2193 struct sock *sk = sock->sk;
2194 struct pppol2tp_session *session = sk->sk_user_data;
2195 struct pppol2tp_tunnel *tunnel;
2196 int val;
2197 int err;
2199 if (level != SOL_PPPOL2TP)
2200 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2202 if (optlen < sizeof(int))
2203 return -EINVAL;
2205 if (get_user(val, (int __user *)optval))
2206 return -EFAULT;
2208 err = -ENOTCONN;
2209 if (sk->sk_user_data == NULL)
2210 goto end;
2212 /* Get session context from the socket */
2213 err = -EBADF;
2214 session = pppol2tp_sock_to_session(sk);
2215 if (session == NULL)
2216 goto end;
2218 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2220 if ((session->tunnel_addr.s_session == 0) &&
2221 (session->tunnel_addr.d_session == 0)) {
2222 err = -EBADF;
2223 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2224 if (tunnel == NULL)
2225 goto end_put_sess;
2227 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2228 sock_put(session->tunnel_sock);
2229 } else
2230 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2232 err = 0;
2234 end_put_sess:
2235 sock_put(sk);
2236 end:
2237 return err;
2240 /* Tunnel getsockopt helper. Called with sock locked.
2242 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2243 struct pppol2tp_tunnel *tunnel,
2244 int optname, int *val)
2246 int err = 0;
2248 switch (optname) {
2249 case PPPOL2TP_SO_DEBUG:
2250 *val = tunnel->debug;
2251 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2252 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2253 break;
2255 default:
2256 err = -ENOPROTOOPT;
2257 break;
2260 return err;
2263 /* Session getsockopt helper. Called with sock locked.
2265 static int pppol2tp_session_getsockopt(struct sock *sk,
2266 struct pppol2tp_session *session,
2267 int optname, int *val)
2269 int err = 0;
2271 switch (optname) {
2272 case PPPOL2TP_SO_RECVSEQ:
2273 *val = session->recv_seq;
2274 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2275 "%s: get recv_seq=%d\n", session->name, *val);
2276 break;
2278 case PPPOL2TP_SO_SENDSEQ:
2279 *val = session->send_seq;
2280 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2281 "%s: get send_seq=%d\n", session->name, *val);
2282 break;
2284 case PPPOL2TP_SO_LNSMODE:
2285 *val = session->lns_mode;
2286 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2287 "%s: get lns_mode=%d\n", session->name, *val);
2288 break;
2290 case PPPOL2TP_SO_DEBUG:
2291 *val = session->debug;
2292 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2293 "%s: get debug=%d\n", session->name, *val);
2294 break;
2296 case PPPOL2TP_SO_REORDERTO:
2297 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2298 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2299 "%s: get reorder_timeout=%d\n", session->name, *val);
2300 break;
2302 default:
2303 err = -ENOPROTOOPT;
2306 return err;
2309 /* Main getsockopt() entry point.
2310 * Does API checks, then calls either the tunnel or session getsockopt
2311 * handler, according to whether the PPPoX socket is a for a regular session
2312 * or the special tunnel type.
2314 static int pppol2tp_getsockopt(struct socket *sock, int level,
2315 int optname, char __user *optval, int __user *optlen)
2317 struct sock *sk = sock->sk;
2318 struct pppol2tp_session *session = sk->sk_user_data;
2319 struct pppol2tp_tunnel *tunnel;
2320 int val, len;
2321 int err;
2323 if (level != SOL_PPPOL2TP)
2324 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2326 if (get_user(len, (int __user *) optlen))
2327 return -EFAULT;
2329 len = min_t(unsigned int, len, sizeof(int));
2331 if (len < 0)
2332 return -EINVAL;
2334 err = -ENOTCONN;
2335 if (sk->sk_user_data == NULL)
2336 goto end;
2338 /* Get the session context */
2339 err = -EBADF;
2340 session = pppol2tp_sock_to_session(sk);
2341 if (session == NULL)
2342 goto end;
2344 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2345 if ((session->tunnel_addr.s_session == 0) &&
2346 (session->tunnel_addr.d_session == 0)) {
2347 err = -EBADF;
2348 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2349 if (tunnel == NULL)
2350 goto end_put_sess;
2352 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2353 sock_put(session->tunnel_sock);
2354 } else
2355 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2357 err = -EFAULT;
2358 if (put_user(len, (int __user *) optlen))
2359 goto end_put_sess;
2361 if (copy_to_user((void __user *) optval, &val, len))
2362 goto end_put_sess;
2364 err = 0;
2366 end_put_sess:
2367 sock_put(sk);
2368 end:
2369 return err;
2372 /*****************************************************************************
2373 * /proc filesystem for debug
2374 *****************************************************************************/
2376 #ifdef CONFIG_PROC_FS
2378 #include <linux/seq_file.h>
2380 struct pppol2tp_seq_data {
2381 struct seq_net_private p;
2382 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2383 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
2386 static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2388 struct pppol2tp_session *session = NULL;
2389 struct hlist_node *walk;
2390 int found = 0;
2391 int next = 0;
2392 int i;
2394 read_lock_bh(&tunnel->hlist_lock);
2395 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2396 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2397 if (curr == NULL) {
2398 found = 1;
2399 goto out;
2401 if (session == curr) {
2402 next = 1;
2403 continue;
2405 if (next) {
2406 found = 1;
2407 goto out;
2411 out:
2412 read_unlock_bh(&tunnel->hlist_lock);
2413 if (!found)
2414 session = NULL;
2416 return session;
2419 static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_net *pn,
2420 struct pppol2tp_tunnel *curr)
2422 struct pppol2tp_tunnel *tunnel = NULL;
2424 read_lock_bh(&pn->pppol2tp_tunnel_list_lock);
2425 if (list_is_last(&curr->list, &pn->pppol2tp_tunnel_list)) {
2426 goto out;
2428 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2429 out:
2430 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
2432 return tunnel;
2435 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2437 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
2438 struct pppol2tp_net *pn;
2439 loff_t pos = *offs;
2441 if (!pos)
2442 goto out;
2444 BUG_ON(m->private == NULL);
2445 pd = m->private;
2446 pn = pppol2tp_pernet(seq_file_net(m));
2448 if (pd->tunnel == NULL) {
2449 if (!list_empty(&pn->pppol2tp_tunnel_list))
2450 pd->tunnel = list_entry(pn->pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
2451 } else {
2452 pd->session = next_session(pd->tunnel, pd->session);
2453 if (pd->session == NULL) {
2454 pd->tunnel = next_tunnel(pn, pd->tunnel);
2458 /* NULL tunnel and session indicates end of list */
2459 if ((pd->tunnel == NULL) && (pd->session == NULL))
2460 pd = NULL;
2462 out:
2463 return pd;
2466 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2468 (*pos)++;
2469 return NULL;
2472 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2474 /* nothing to do */
2477 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2479 struct pppol2tp_tunnel *tunnel = v;
2481 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2482 tunnel->name,
2483 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2484 atomic_read(&tunnel->ref_count) - 1);
2485 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2486 tunnel->debug,
2487 (unsigned long long)tunnel->stats.tx_packets,
2488 (unsigned long long)tunnel->stats.tx_bytes,
2489 (unsigned long long)tunnel->stats.tx_errors,
2490 (unsigned long long)tunnel->stats.rx_packets,
2491 (unsigned long long)tunnel->stats.rx_bytes,
2492 (unsigned long long)tunnel->stats.rx_errors);
2495 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2497 struct pppol2tp_session *session = v;
2499 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2500 "%04X/%04X %d %c\n",
2501 session->name,
2502 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2503 ntohs(session->tunnel_addr.addr.sin_port),
2504 session->tunnel_addr.s_tunnel,
2505 session->tunnel_addr.s_session,
2506 session->tunnel_addr.d_tunnel,
2507 session->tunnel_addr.d_session,
2508 session->sock->sk_state,
2509 (session == session->sock->sk_user_data) ?
2510 'Y' : 'N');
2511 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2512 session->mtu, session->mru,
2513 session->recv_seq ? 'R' : '-',
2514 session->send_seq ? 'S' : '-',
2515 session->lns_mode ? "LNS" : "LAC",
2516 session->debug,
2517 jiffies_to_msecs(session->reorder_timeout));
2518 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2519 session->nr, session->ns,
2520 (unsigned long long)session->stats.tx_packets,
2521 (unsigned long long)session->stats.tx_bytes,
2522 (unsigned long long)session->stats.tx_errors,
2523 (unsigned long long)session->stats.rx_packets,
2524 (unsigned long long)session->stats.rx_bytes,
2525 (unsigned long long)session->stats.rx_errors);
2528 static int pppol2tp_seq_show(struct seq_file *m, void *v)
2530 struct pppol2tp_seq_data *pd = v;
2532 /* display header on line 1 */
2533 if (v == SEQ_START_TOKEN) {
2534 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2535 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2536 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2537 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2538 "dest-tid/sid state user-data-ok\n");
2539 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2540 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2541 goto out;
2544 /* Show the tunnel or session context.
2546 if (pd->session == NULL)
2547 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2548 else
2549 pppol2tp_seq_session_show(m, pd->session);
2551 out:
2552 return 0;
2555 static const struct seq_operations pppol2tp_seq_ops = {
2556 .start = pppol2tp_seq_start,
2557 .next = pppol2tp_seq_next,
2558 .stop = pppol2tp_seq_stop,
2559 .show = pppol2tp_seq_show,
2562 /* Called when our /proc file is opened. We allocate data for use when
2563 * iterating our tunnel / session contexts and store it in the private
2564 * data of the seq_file.
2566 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2568 return seq_open_net(inode, file, &pppol2tp_seq_ops,
2569 sizeof(struct pppol2tp_seq_data));
2572 static const struct file_operations pppol2tp_proc_fops = {
2573 .owner = THIS_MODULE,
2574 .open = pppol2tp_proc_open,
2575 .read = seq_read,
2576 .llseek = seq_lseek,
2577 .release = seq_release_net,
2580 #endif /* CONFIG_PROC_FS */
2582 /*****************************************************************************
2583 * Init and cleanup
2584 *****************************************************************************/
2586 static const struct proto_ops pppol2tp_ops = {
2587 .family = AF_PPPOX,
2588 .owner = THIS_MODULE,
2589 .release = pppol2tp_release,
2590 .bind = sock_no_bind,
2591 .connect = pppol2tp_connect,
2592 .socketpair = sock_no_socketpair,
2593 .accept = sock_no_accept,
2594 .getname = pppol2tp_getname,
2595 .poll = datagram_poll,
2596 .listen = sock_no_listen,
2597 .shutdown = sock_no_shutdown,
2598 .setsockopt = pppol2tp_setsockopt,
2599 .getsockopt = pppol2tp_getsockopt,
2600 .sendmsg = pppol2tp_sendmsg,
2601 .recvmsg = pppol2tp_recvmsg,
2602 .mmap = sock_no_mmap,
2603 .ioctl = pppox_ioctl,
2606 static struct pppox_proto pppol2tp_proto = {
2607 .create = pppol2tp_create,
2608 .ioctl = pppol2tp_ioctl
2611 static __net_init int pppol2tp_init_net(struct net *net)
2613 struct pppol2tp_net *pn = pppol2tp_pernet(net);
2614 struct proc_dir_entry *pde;
2616 INIT_LIST_HEAD(&pn->pppol2tp_tunnel_list);
2617 rwlock_init(&pn->pppol2tp_tunnel_list_lock);
2619 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
2620 #ifdef CONFIG_PROC_FS
2621 if (!pde)
2622 return -ENOMEM;
2623 #endif
2625 return 0;
2628 static __net_exit void pppol2tp_exit_net(struct net *net)
2630 proc_net_remove(net, "pppol2tp");
2633 static struct pernet_operations pppol2tp_net_ops = {
2634 .init = pppol2tp_init_net,
2635 .exit = pppol2tp_exit_net,
2636 .id = &pppol2tp_net_id,
2637 .size = sizeof(struct pppol2tp_net),
2640 static int __init pppol2tp_init(void)
2642 int err;
2644 err = proto_register(&pppol2tp_sk_proto, 0);
2645 if (err)
2646 goto out;
2647 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2648 if (err)
2649 goto out_unregister_pppol2tp_proto;
2651 err = register_pernet_device(&pppol2tp_net_ops);
2652 if (err)
2653 goto out_unregister_pppox_proto;
2655 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2656 PPPOL2TP_DRV_VERSION);
2658 out:
2659 return err;
2660 out_unregister_pppox_proto:
2661 unregister_pppox_proto(PX_PROTO_OL2TP);
2662 out_unregister_pppol2tp_proto:
2663 proto_unregister(&pppol2tp_sk_proto);
2664 goto out;
2667 static void __exit pppol2tp_exit(void)
2669 unregister_pppox_proto(PX_PROTO_OL2TP);
2670 unregister_pernet_device(&pppol2tp_net_ops);
2671 proto_unregister(&pppol2tp_sk_proto);
2674 module_init(pppol2tp_init);
2675 module_exit(pppol2tp_exit);
2677 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
2678 "James Chapman <jchapman@katalix.com>");
2679 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2680 MODULE_LICENSE("GPL");
2681 MODULE_VERSION(PPPOL2TP_DRV_VERSION);