blob: 856610f0624eb41641be7a882bd07d07f5cfe0f1 [file] [log] [blame]
James Chapman3557baa2007-06-27 15:49:24 -07001/*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
3 *
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
6 *
7 * Version: 1.0.0
8 *
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)
15 *
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.
21 *
22 */
23
24/* This driver handles only L2TP data frames; control frames are handled by a
25 * userspace application.
26 *
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.
32 *
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.
35 *
36 * Here's example userspace code to create a socket for sending/receiving data
37 * over an L2TP session:-
38 *
39 * struct sockaddr_pppol2tp sax;
40 * int fd;
41 * int session_fd;
42 *
43 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
44 *
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;
55 *
56 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
57 *
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.
61 */
62
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>
68
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>
76
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/dst.h>
95#include <net/ip.h>
96#include <net/udp.h>
97#include <net/xfrm.h>
98
99#include <asm/byteorder.h>
100#include <asm/atomic.h>
101
102
103#define PPPOL2TP_DRV_VERSION "V1.0"
104
105/* L2TP header constants */
106#define L2TP_HDRFLAG_T 0x8000
107#define L2TP_HDRFLAG_L 0x4000
108#define L2TP_HDRFLAG_S 0x0800
109#define L2TP_HDRFLAG_O 0x0200
110#define L2TP_HDRFLAG_P 0x0100
111
112#define L2TP_HDR_VER_MASK 0x000F
113#define L2TP_HDR_VER 0x0002
114
115/* Space for UDP, L2TP and PPP headers */
116#define PPPOL2TP_HEADER_OVERHEAD 40
117
118/* Just some random numbers */
119#define L2TP_TUNNEL_MAGIC 0x42114DDA
120#define L2TP_SESSION_MAGIC 0x0C04EB7D
121
122#define PPPOL2TP_HASH_BITS 4
123#define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
124
125/* Default trace flags */
126#define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
127
128#define PRINTK(_mask, _type, _lvl, _fmt, args...) \
129 do { \
130 if ((_mask) & (_type)) \
131 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
132 } while(0)
133
134/* Number of bytes to build transmit L2TP headers.
135 * Unfortunately the size is different depending on whether sequence numbers
136 * are enabled.
137 */
138#define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
139#define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
140
141struct pppol2tp_tunnel;
142
143/* Describes a session. It is the sk_user_data field in the PPPoL2TP
144 * socket. Contains information to determine incoming packets and transmit
145 * outgoing ones.
146 */
147struct pppol2tp_session
148{
149 int magic; /* should be
150 * L2TP_SESSION_MAGIC */
151 int owner; /* pid that opened the socket */
152
153 struct sock *sock; /* Pointer to the session
154 * PPPoX socket */
155 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
156 * socket */
157
158 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
159
160 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
161 * context */
162
163 char name[20]; /* "sess xxxxx/yyyyy", where
164 * x=tunnel_id, y=session_id */
165 int mtu;
166 int mru;
167 int flags; /* accessed by PPPIOCGFLAGS.
168 * Unused. */
169 unsigned recv_seq:1; /* expect receive packets with
170 * sequence numbers? */
171 unsigned send_seq:1; /* send packets with sequence
172 * numbers? */
173 unsigned lns_mode:1; /* behave as LNS? LAC enables
174 * sequence numbers under
175 * control of LNS. */
176 int debug; /* bitmask of debug message
177 * categories */
178 int reorder_timeout; /* configured reorder timeout
179 * (in jiffies) */
180 u16 nr; /* session NR state (receive) */
181 u16 ns; /* session NR state (send) */
182 struct sk_buff_head reorder_q; /* receive reorder queue */
183 struct pppol2tp_ioc_stats stats;
184 struct hlist_node hlist; /* Hash list node */
185};
186
187/* The sk_user_data field of the tunnel's UDP socket. It contains info to track
188 * all the associated sessions so incoming packets can be sorted out
189 */
190struct pppol2tp_tunnel
191{
192 int magic; /* Should be L2TP_TUNNEL_MAGIC */
193 rwlock_t hlist_lock; /* protect session_hlist */
194 struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE];
195 /* hashed list of sessions,
196 * hashed by id */
197 int debug; /* bitmask of debug message
198 * categories */
199 char name[12]; /* "tunl xxxxx" */
200 struct pppol2tp_ioc_stats stats;
201
202 void (*old_sk_destruct)(struct sock *);
203
204 struct sock *sock; /* Parent socket */
205 struct list_head list; /* Keep a list of all open
206 * prepared sockets */
207
208 atomic_t ref_count;
209};
210
211/* Private data stored for received packets in the skb.
212 */
213struct pppol2tp_skb_cb {
214 u16 ns;
215 u16 nr;
216 u16 has_seq;
217 u16 length;
218 unsigned long expires;
219};
220
221#define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
222
223static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
224static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel);
225
226static atomic_t pppol2tp_tunnel_count;
227static atomic_t pppol2tp_session_count;
228static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
229static struct proto_ops pppol2tp_ops;
230static LIST_HEAD(pppol2tp_tunnel_list);
231static DEFINE_RWLOCK(pppol2tp_tunnel_list_lock);
232
233/* Helpers to obtain tunnel/session contexts from sockets.
234 */
235static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk)
236{
237 struct pppol2tp_session *session;
238
239 if (sk == NULL)
240 return NULL;
241
242 session = (struct pppol2tp_session *)(sk->sk_user_data);
243 if (session == NULL)
244 return NULL;
245
246 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
247
248 return session;
249}
250
251static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk)
252{
253 struct pppol2tp_tunnel *tunnel;
254
255 if (sk == NULL)
256 return NULL;
257
258 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data);
259 if (tunnel == NULL)
260 return NULL;
261
262 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
263
264 return tunnel;
265}
266
267/* Tunnel reference counts. Incremented per session that is added to
268 * the tunnel.
269 */
270static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel)
271{
272 atomic_inc(&tunnel->ref_count);
273}
274
275static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel)
276{
277 if (atomic_dec_and_test(&tunnel->ref_count))
278 pppol2tp_tunnel_free(tunnel);
279}
280
281/* Session hash list.
282 * The session_id SHOULD be random according to RFC2661, but several
283 * L2TP implementations (Cisco and Microsoft) use incrementing
284 * session_ids. So we do a real hash on the session_id, rather than a
285 * simple bitmask.
286 */
287static inline struct hlist_head *
288pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id)
289{
290 unsigned long hash_val = (unsigned long) session_id;
291 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)];
292}
293
294/* Lookup a session by id
295 */
296static struct pppol2tp_session *
297pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id)
298{
299 struct hlist_head *session_list =
300 pppol2tp_session_id_hash(tunnel, session_id);
301 struct pppol2tp_session *session;
302 struct hlist_node *walk;
303
304 read_lock(&tunnel->hlist_lock);
305 hlist_for_each_entry(session, walk, session_list, hlist) {
306 if (session->tunnel_addr.s_session == session_id) {
307 read_unlock(&tunnel->hlist_lock);
308 return session;
309 }
310 }
311 read_unlock(&tunnel->hlist_lock);
312
313 return NULL;
314}
315
316/* Lookup a tunnel by id
317 */
318static struct pppol2tp_tunnel *pppol2tp_tunnel_find(u16 tunnel_id)
319{
320 struct pppol2tp_tunnel *tunnel = NULL;
321
322 read_lock(&pppol2tp_tunnel_list_lock);
323 list_for_each_entry(tunnel, &pppol2tp_tunnel_list, list) {
324 if (tunnel->stats.tunnel_id == tunnel_id) {
325 read_unlock(&pppol2tp_tunnel_list_lock);
326 return tunnel;
327 }
328 }
329 read_unlock(&pppol2tp_tunnel_list_lock);
330
331 return NULL;
332}
333
334/*****************************************************************************
335 * Receive data handling
336 *****************************************************************************/
337
338/* Queue a skb in order. We come here only if the skb has an L2TP sequence
339 * number.
340 */
341static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
342{
343 struct sk_buff *skbp;
344 u16 ns = PPPOL2TP_SKB_CB(skb)->ns;
345
346 spin_lock(&session->reorder_q.lock);
347 skb_queue_walk(&session->reorder_q, skbp) {
348 if (PPPOL2TP_SKB_CB(skbp)->ns > ns) {
349 __skb_insert(skb, skbp->prev, skbp, &session->reorder_q);
350 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
351 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
352 session->name, ns, PPPOL2TP_SKB_CB(skbp)->ns,
353 skb_queue_len(&session->reorder_q));
354 session->stats.rx_oos_packets++;
355 goto out;
356 }
357 }
358
359 __skb_queue_tail(&session->reorder_q, skb);
360
361out:
362 spin_unlock(&session->reorder_q.lock);
363}
364
365/* Dequeue a single skb.
366 */
367static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
368{
369 struct pppol2tp_tunnel *tunnel = session->tunnel;
370 int length = PPPOL2TP_SKB_CB(skb)->length;
371 struct sock *session_sock = NULL;
372
373 /* We're about to requeue the skb, so unlink it and return resources
374 * to its current owner (a socket receive buffer).
375 */
376 skb_unlink(skb, &session->reorder_q);
377 skb_orphan(skb);
378
379 tunnel->stats.rx_packets++;
380 tunnel->stats.rx_bytes += length;
381 session->stats.rx_packets++;
382 session->stats.rx_bytes += length;
383
384 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
385 /* Bump our Nr */
386 session->nr++;
387 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
388 "%s: updated nr to %hu\n", session->name, session->nr);
389 }
390
391 /* If the socket is bound, send it in to PPP's input queue. Otherwise
392 * queue it on the session socket.
393 */
394 session_sock = session->sock;
395 if (session_sock->sk_state & PPPOX_BOUND) {
396 struct pppox_sock *po;
397 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
398 "%s: recv %d byte data frame, passing to ppp\n",
399 session->name, length);
400
401 /* We need to forget all info related to the L2TP packet
402 * gathered in the skb as we are going to reuse the same
403 * skb for the inner packet.
404 * Namely we need to:
405 * - reset xfrm (IPSec) information as it applies to
406 * the outer L2TP packet and not to the inner one
407 * - release the dst to force a route lookup on the inner
408 * IP packet since skb->dst currently points to the dst
409 * of the UDP tunnel
410 * - reset netfilter information as it doesn't apply
411 * to the inner packet either
412 */
413 secpath_reset(skb);
414 dst_release(skb->dst);
415 skb->dst = NULL;
416 nf_reset(skb);
417
418 po = pppox_sk(session_sock);
419 ppp_input(&po->chan, skb);
420 } else {
421 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
422 "%s: socket not bound\n", session->name);
423
424 /* Not bound. Nothing we can do, so discard. */
425 session->stats.rx_errors++;
426 kfree_skb(skb);
427 }
428
429 sock_put(session->sock);
430}
431
432/* Dequeue skbs from the session's reorder_q, subject to packet order.
433 * Skbs that have been in the queue for too long are simply discarded.
434 */
435static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
436{
437 struct sk_buff *skb;
438 struct sk_buff *tmp;
439
440 /* If the pkt at the head of the queue has the nr that we
441 * expect to send up next, dequeue it and any other
442 * in-sequence packets behind it.
443 */
444 spin_lock(&session->reorder_q.lock);
445 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
446 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
447 session->stats.rx_seq_discards++;
448 session->stats.rx_errors++;
449 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
450 "%s: oos pkt %hu len %d discarded (too old), "
451 "waiting for %hu, reorder_q_len=%d\n",
452 session->name, PPPOL2TP_SKB_CB(skb)->ns,
453 PPPOL2TP_SKB_CB(skb)->length, session->nr,
454 skb_queue_len(&session->reorder_q));
455 __skb_unlink(skb, &session->reorder_q);
456 kfree_skb(skb);
457 continue;
458 }
459
460 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
461 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
462 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
463 "%s: holding oos pkt %hu len %d, "
464 "waiting for %hu, reorder_q_len=%d\n",
465 session->name, PPPOL2TP_SKB_CB(skb)->ns,
466 PPPOL2TP_SKB_CB(skb)->length, session->nr,
467 skb_queue_len(&session->reorder_q));
468 goto out;
469 }
470 }
471 spin_unlock(&session->reorder_q.lock);
472 pppol2tp_recv_dequeue_skb(session, skb);
473 spin_lock(&session->reorder_q.lock);
474 }
475
476out:
477 spin_unlock(&session->reorder_q.lock);
478}
479
480/* Internal receive frame. Do the real work of receiving an L2TP data frame
481 * here. The skb is not on a list when we get here.
482 * Returns 0 if the packet was a data packet and was successfully passed on.
483 * Returns 1 if the packet was not a good data packet and could not be
484 * forwarded. All such packets are passed up to userspace to deal with.
485 */
486static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
487{
488 struct pppol2tp_session *session = NULL;
489 struct pppol2tp_tunnel *tunnel;
490 unsigned char *ptr;
491 u16 hdrflags;
492 u16 tunnel_id, session_id;
493 int length;
494 struct udphdr *uh;
495
496 tunnel = pppol2tp_sock_to_tunnel(sock);
497 if (tunnel == NULL)
498 goto error;
499
500 /* Short packet? */
501 if (skb->len < sizeof(struct udphdr)) {
502 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
503 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
504 goto error;
505 }
506
507 /* Point to L2TP header */
508 ptr = skb->data + sizeof(struct udphdr);
509
510 /* Get L2TP header flags */
511 hdrflags = ntohs(*(__be16*)ptr);
512
513 /* Trace packet contents, if enabled */
514 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
515 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
516
517 for (length = 0; length < 16; length++)
518 printk(" %02X", ptr[length]);
519 printk("\n");
520 }
521
522 /* Get length of L2TP packet */
523 uh = (struct udphdr *) skb_transport_header(skb);
524 length = ntohs(uh->len) - sizeof(struct udphdr);
525
526 /* Too short? */
527 if (length < 12) {
528 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
529 "%s: recv short L2TP packet (len=%d)\n", tunnel->name, length);
530 goto error;
531 }
532
533 /* If type is control packet, it is handled by userspace. */
534 if (hdrflags & L2TP_HDRFLAG_T) {
535 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
536 "%s: recv control packet, len=%d\n", tunnel->name, length);
537 goto error;
538 }
539
540 /* Skip flags */
541 ptr += 2;
542
543 /* If length is present, skip it */
544 if (hdrflags & L2TP_HDRFLAG_L)
545 ptr += 2;
546
547 /* Extract tunnel and session ID */
548 tunnel_id = ntohs(*(__be16 *) ptr);
549 ptr += 2;
550 session_id = ntohs(*(__be16 *) ptr);
551 ptr += 2;
552
553 /* Find the session context */
554 session = pppol2tp_session_find(tunnel, session_id);
555 if (!session) {
556 /* Not found? Pass to userspace to deal with */
557 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
558 "%s: no socket found (%hu/%hu). Passing up.\n",
559 tunnel->name, tunnel_id, session_id);
560 goto error;
561 }
562 sock_hold(session->sock);
563
564 /* The ref count on the socket was increased by the above call since
565 * we now hold a pointer to the session. Take care to do sock_put()
566 * when exiting this function from now on...
567 */
568
569 /* Handle the optional sequence numbers. If we are the LAC,
570 * enable/disable sequence numbers under the control of the LNS. If
571 * no sequence numbers present but we were expecting them, discard
572 * frame.
573 */
574 if (hdrflags & L2TP_HDRFLAG_S) {
575 u16 ns, nr;
576 ns = ntohs(*(__be16 *) ptr);
577 ptr += 2;
578 nr = ntohs(*(__be16 *) ptr);
579 ptr += 2;
580
581 /* Received a packet with sequence numbers. If we're the LNS,
582 * check if we sre sending sequence numbers and if not,
583 * configure it so.
584 */
585 if ((!session->lns_mode) && (!session->send_seq)) {
586 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
587 "%s: requested to enable seq numbers by LNS\n",
588 session->name);
589 session->send_seq = -1;
590 }
591
592 /* Store L2TP info in the skb */
593 PPPOL2TP_SKB_CB(skb)->ns = ns;
594 PPPOL2TP_SKB_CB(skb)->nr = nr;
595 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
596
597 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
598 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
599 session->name, ns, nr, session->nr);
600 } else {
601 /* No sequence numbers.
602 * If user has configured mandatory sequence numbers, discard.
603 */
604 if (session->recv_seq) {
605 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
606 "%s: recv data has no seq numbers when required. "
607 "Discarding\n", session->name);
608 session->stats.rx_seq_discards++;
609 session->stats.rx_errors++;
610 goto discard;
611 }
612
613 /* If we're the LAC and we're sending sequence numbers, the
614 * LNS has requested that we no longer send sequence numbers.
615 * If we're the LNS and we're sending sequence numbers, the
616 * LAC is broken. Discard the frame.
617 */
618 if ((!session->lns_mode) && (session->send_seq)) {
619 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
620 "%s: requested to disable seq numbers by LNS\n",
621 session->name);
622 session->send_seq = 0;
623 } else if (session->send_seq) {
624 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
625 "%s: recv data has no seq numbers when required. "
626 "Discarding\n", session->name);
627 session->stats.rx_seq_discards++;
628 session->stats.rx_errors++;
629 goto discard;
630 }
631
632 /* Store L2TP info in the skb */
633 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
634 }
635
636 /* If offset bit set, skip it. */
637 if (hdrflags & L2TP_HDRFLAG_O)
638 ptr += 2 + ntohs(*(__be16 *) ptr);
639
640 skb_pull(skb, ptr - skb->data);
641
642 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
643 * don't send the PPP header (PPP header compression enabled), but
644 * other clients can include the header. So we cope with both cases
645 * here. The PPP header is always FF03 when using L2TP.
646 *
647 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
648 * the field may be unaligned.
649 */
650 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
651 skb_pull(skb, 2);
652
653 /* Prepare skb for adding to the session's reorder_q. Hold
654 * packets for max reorder_timeout or 1 second if not
655 * reordering.
656 */
657 PPPOL2TP_SKB_CB(skb)->length = length;
658 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
659 (session->reorder_timeout ? session->reorder_timeout : HZ);
660
661 /* Add packet to the session's receive queue. Reordering is done here, if
662 * enabled. Saved L2TP protocol info is stored in skb->sb[].
663 */
664 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
665 if (session->reorder_timeout != 0) {
666 /* Packet reordering enabled. Add skb to session's
667 * reorder queue, in order of ns.
668 */
669 pppol2tp_recv_queue_skb(session, skb);
670 } else {
671 /* Packet reordering disabled. Discard out-of-sequence
672 * packets
673 */
674 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
675 session->stats.rx_seq_discards++;
676 session->stats.rx_errors++;
677 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
678 "%s: oos pkt %hu len %d discarded, "
679 "waiting for %hu, reorder_q_len=%d\n",
680 session->name, PPPOL2TP_SKB_CB(skb)->ns,
681 PPPOL2TP_SKB_CB(skb)->length, session->nr,
682 skb_queue_len(&session->reorder_q));
683 goto discard;
684 }
685 skb_queue_tail(&session->reorder_q, skb);
686 }
687 } else {
688 /* No sequence numbers. Add the skb to the tail of the
689 * reorder queue. This ensures that it will be
690 * delivered after all previous sequenced skbs.
691 */
692 skb_queue_tail(&session->reorder_q, skb);
693 }
694
695 /* Try to dequeue as many skbs from reorder_q as we can. */
696 pppol2tp_recv_dequeue(session);
697
698 return 0;
699
700discard:
701 kfree_skb(skb);
702 sock_put(session->sock);
703
704 return 0;
705
706error:
707 return 1;
708}
709
710/* UDP encapsulation receive handler. See net/ipv4/udp.c.
711 * Return codes:
712 * 0 : success.
713 * <0: error
714 * >0: skb should be passed up to userspace as UDP.
715 */
716static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
717{
718 struct pppol2tp_tunnel *tunnel;
719
720 tunnel = pppol2tp_sock_to_tunnel(sk);
721 if (tunnel == NULL)
722 goto pass_up;
723
724 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
725 "%s: received %d bytes\n", tunnel->name, skb->len);
726
727 if (pppol2tp_recv_core(sk, skb))
728 goto pass_up;
729
730 return 0;
731
732pass_up:
733 return 1;
734}
735
736/* Receive message. This is the recvmsg for the PPPoL2TP socket.
737 */
738static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
739 struct msghdr *msg, size_t len,
740 int flags)
741{
742 int err;
743 struct sk_buff *skb;
744 struct sock *sk = sock->sk;
745
746 err = -EIO;
747 if (sk->sk_state & PPPOX_BOUND)
748 goto end;
749
750 msg->msg_namelen = 0;
751
752 err = 0;
753 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
754 flags & MSG_DONTWAIT, &err);
755 if (skb) {
756 err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data,
757 skb->len);
758 if (err < 0)
759 goto do_skb_free;
760 err = skb->len;
761 }
762do_skb_free:
763 kfree_skb(skb);
764end:
765 return err;
766}
767
768/************************************************************************
769 * Transmit handling
770 ***********************************************************************/
771
772/* Tell how big L2TP headers are for a particular session. This
773 * depends on whether sequence numbers are being used.
774 */
775static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
776{
777 if (session->send_seq)
778 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
779
780 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
781}
782
783/* Build an L2TP header for the session into the buffer provided.
784 */
785static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
786 void *buf)
787{
788 __be16 *bufp = buf;
789 u16 flags = L2TP_HDR_VER;
790
791 if (session->send_seq)
792 flags |= L2TP_HDRFLAG_S;
793
794 /* Setup L2TP header.
795 * FIXME: Can this ever be unaligned? Is direct dereferencing of
796 * 16-bit header fields safe here for all architectures?
797 */
798 *bufp++ = htons(flags);
799 *bufp++ = htons(session->tunnel_addr.d_tunnel);
800 *bufp++ = htons(session->tunnel_addr.d_session);
801 if (session->send_seq) {
802 *bufp++ = htons(session->ns);
803 *bufp++ = 0;
804 session->ns++;
805 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
806 "%s: updated ns to %hu\n", session->name, session->ns);
807 }
808}
809
810/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
811 * when a user application does a sendmsg() on the session socket. L2TP and
812 * PPP headers must be inserted into the user's data.
813 */
814static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
815 size_t total_len)
816{
817 static const unsigned char ppph[2] = { 0xff, 0x03 };
818 struct sock *sk = sock->sk;
819 struct inet_sock *inet;
820 __wsum csum = 0;
821 struct sk_buff *skb;
822 int error;
823 int hdr_len;
824 struct pppol2tp_session *session;
825 struct pppol2tp_tunnel *tunnel;
826 struct udphdr *uh;
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700827 unsigned int len;
James Chapman3557baa2007-06-27 15:49:24 -0700828
829 error = -ENOTCONN;
830 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
831 goto error;
832
833 /* Get session and tunnel contexts */
834 error = -EBADF;
835 session = pppol2tp_sock_to_session(sk);
836 if (session == NULL)
837 goto error;
838
839 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
840 if (tunnel == NULL)
841 goto error;
842
843 /* What header length is configured for this session? */
844 hdr_len = pppol2tp_l2tp_header_len(session);
845
846 /* Allocate a socket buffer */
847 error = -ENOMEM;
848 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
849 sizeof(struct udphdr) + hdr_len +
850 sizeof(ppph) + total_len,
851 0, GFP_KERNEL);
852 if (!skb)
853 goto error;
854
855 /* Reserve space for headers. */
856 skb_reserve(skb, NET_SKB_PAD);
857 skb_reset_network_header(skb);
858 skb_reserve(skb, sizeof(struct iphdr));
859 skb_reset_transport_header(skb);
860
861 /* Build UDP header */
862 inet = inet_sk(session->tunnel_sock);
863 uh = (struct udphdr *) skb->data;
864 uh->source = inet->sport;
865 uh->dest = inet->dport;
866 uh->len = htons(hdr_len + sizeof(ppph) + total_len);
867 uh->check = 0;
868 skb_put(skb, sizeof(struct udphdr));
869
870 /* Build L2TP header */
871 pppol2tp_build_l2tp_header(session, skb->data);
872 skb_put(skb, hdr_len);
873
874 /* Add PPP header */
875 skb->data[0] = ppph[0];
876 skb->data[1] = ppph[1];
877 skb_put(skb, 2);
878
879 /* Copy user data into skb */
880 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
881 if (error < 0) {
882 kfree_skb(skb);
883 goto error;
884 }
885 skb_put(skb, total_len);
886
887 /* Calculate UDP checksum if configured to do so */
888 if (session->tunnel_sock->sk_no_check != UDP_CSUM_NOXMIT)
889 csum = udp_csum_outgoing(sk, skb);
890
891 /* Debug */
892 if (session->send_seq)
893 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
David S. Miller38d15b62007-06-27 15:52:25 -0700894 "%s: send %Zd bytes, ns=%hu\n", session->name,
James Chapman3557baa2007-06-27 15:49:24 -0700895 total_len, session->ns - 1);
896 else
897 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
David S. Miller38d15b62007-06-27 15:52:25 -0700898 "%s: send %Zd bytes\n", session->name, total_len);
James Chapman3557baa2007-06-27 15:49:24 -0700899
900 if (session->debug & PPPOL2TP_MSG_DATA) {
901 int i;
902 unsigned char *datap = skb->data;
903
904 printk(KERN_DEBUG "%s: xmit:", session->name);
905 for (i = 0; i < total_len; i++) {
906 printk(" %02X", *datap++);
907 if (i == 15) {
908 printk(" ...");
909 break;
910 }
911 }
912 printk("\n");
913 }
914
915 /* Queue the packet to IP for output */
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700916 len = skb->len;
James Chapman3557baa2007-06-27 15:49:24 -0700917 error = ip_queue_xmit(skb, 1);
918
919 /* Update stats */
920 if (error >= 0) {
921 tunnel->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700922 tunnel->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -0700923 session->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700924 session->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -0700925 } else {
926 tunnel->stats.tx_errors++;
927 session->stats.tx_errors++;
928 }
929
930error:
931 return error;
932}
933
934/* Transmit function called by generic PPP driver. Sends PPP frame
935 * over PPPoL2TP socket.
936 *
937 * This is almost the same as pppol2tp_sendmsg(), but rather than
938 * being called with a msghdr from userspace, it is called with a skb
939 * from the kernel.
940 *
941 * The supplied skb from ppp doesn't have enough headroom for the
942 * insertion of L2TP, UDP and IP headers so we need to allocate more
943 * headroom in the skb. This will create a cloned skb. But we must be
944 * careful in the error case because the caller will expect to free
945 * the skb it supplied, not our cloned skb. So we take care to always
946 * leave the original skb unfreed if we return an error.
947 */
948static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
949{
950 static const u8 ppph[2] = { 0xff, 0x03 };
951 struct sock *sk = (struct sock *) chan->private;
952 struct sock *sk_tun;
953 int hdr_len;
954 struct pppol2tp_session *session;
955 struct pppol2tp_tunnel *tunnel;
956 int rc;
957 int headroom;
958 int data_len = skb->len;
959 struct inet_sock *inet;
960 __wsum csum = 0;
961 struct sk_buff *skb2 = NULL;
962 struct udphdr *uh;
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700963 unsigned int len;
James Chapman3557baa2007-06-27 15:49:24 -0700964
965 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
966 goto abort;
967
968 /* Get session and tunnel contexts from the socket */
969 session = pppol2tp_sock_to_session(sk);
970 if (session == NULL)
971 goto abort;
972
973 sk_tun = session->tunnel_sock;
974 if (sk_tun == NULL)
975 goto abort;
976 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
977 if (tunnel == NULL)
978 goto abort;
979
980 /* What header length is configured for this session? */
981 hdr_len = pppol2tp_l2tp_header_len(session);
982
983 /* Check that there's enough headroom in the skb to insert IP,
984 * UDP and L2TP and PPP headers. If not enough, expand it to
985 * make room. Note that a new skb (or a clone) is
986 * allocated. If we return an error from this point on, make
987 * sure we free the new skb but do not free the original skb
988 * since that is done by the caller for the error case.
989 */
990 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
991 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
992 if (skb_headroom(skb) < headroom) {
993 skb2 = skb_realloc_headroom(skb, headroom);
994 if (skb2 == NULL)
995 goto abort;
996 } else
997 skb2 = skb;
998
999 /* Check that the socket has room */
1000 if (atomic_read(&sk_tun->sk_wmem_alloc) < sk_tun->sk_sndbuf)
1001 skb_set_owner_w(skb2, sk_tun);
1002 else
1003 goto discard;
1004
1005 /* Setup PPP header */
1006 skb_push(skb2, sizeof(ppph));
1007 skb2->data[0] = ppph[0];
1008 skb2->data[1] = ppph[1];
1009
1010 /* Setup L2TP header */
1011 skb_push(skb2, hdr_len);
1012 pppol2tp_build_l2tp_header(session, skb2->data);
1013
1014 /* Setup UDP header */
1015 inet = inet_sk(sk_tun);
1016 skb_push(skb2, sizeof(struct udphdr));
1017 skb_reset_transport_header(skb2);
1018 uh = (struct udphdr *) skb2->data;
1019 uh->source = inet->sport;
1020 uh->dest = inet->dport;
1021 uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len);
1022 uh->check = 0;
1023
1024 /* Calculate UDP checksum if configured to do so */
1025 if (sk_tun->sk_no_check != UDP_CSUM_NOXMIT)
1026 csum = udp_csum_outgoing(sk_tun, skb2);
1027
1028 /* Debug */
1029 if (session->send_seq)
1030 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1031 "%s: send %d bytes, ns=%hu\n", session->name,
1032 data_len, session->ns - 1);
1033 else
1034 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1035 "%s: send %d bytes\n", session->name, data_len);
1036
1037 if (session->debug & PPPOL2TP_MSG_DATA) {
1038 int i;
1039 unsigned char *datap = skb2->data;
1040
1041 printk(KERN_DEBUG "%s: xmit:", session->name);
1042 for (i = 0; i < data_len; i++) {
1043 printk(" %02X", *datap++);
1044 if (i == 31) {
1045 printk(" ...");
1046 break;
1047 }
1048 }
1049 printk("\n");
1050 }
1051
1052 /* Get routing info from the tunnel socket */
1053 skb2->dst = sk_dst_get(sk_tun);
1054
1055 /* Queue the packet to IP for output */
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001056 len = skb2->len;
James Chapman3557baa2007-06-27 15:49:24 -07001057 rc = ip_queue_xmit(skb2, 1);
1058
1059 /* Update stats */
1060 if (rc >= 0) {
1061 tunnel->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001062 tunnel->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001063 session->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001064 session->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001065 } else {
1066 tunnel->stats.tx_errors++;
1067 session->stats.tx_errors++;
1068 }
1069
1070 /* Free the original skb */
1071 kfree_skb(skb);
1072
1073 return 1;
1074
1075discard:
1076 /* Free the new skb. Caller will free original skb. */
1077 if (skb2 != skb)
1078 kfree_skb(skb2);
1079abort:
1080 return 0;
1081}
1082
1083/*****************************************************************************
1084 * Session (and tunnel control) socket create/destroy.
1085 *****************************************************************************/
1086
1087/* When the tunnel UDP socket is closed, all the attached sockets need to go
1088 * too.
1089 */
1090static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1091{
1092 int hash;
1093 struct hlist_node *walk;
1094 struct hlist_node *tmp;
1095 struct pppol2tp_session *session;
1096 struct sock *sk;
1097
1098 if (tunnel == NULL)
1099 BUG();
1100
1101 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1102 "%s: closing all sessions...\n", tunnel->name);
1103
1104 write_lock(&tunnel->hlist_lock);
1105 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1106again:
1107 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1108 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1109
1110 sk = session->sock;
1111
1112 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1113 "%s: closing session\n", session->name);
1114
1115 hlist_del_init(&session->hlist);
1116
1117 /* Since we should hold the sock lock while
1118 * doing any unbinding, we need to release the
1119 * lock we're holding before taking that lock.
1120 * Hold a reference to the sock so it doesn't
1121 * disappear as we're jumping between locks.
1122 */
1123 sock_hold(sk);
1124 write_unlock(&tunnel->hlist_lock);
1125 lock_sock(sk);
1126
1127 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1128 pppox_unbind_sock(sk);
1129 sk->sk_state = PPPOX_DEAD;
1130 sk->sk_state_change(sk);
1131 }
1132
1133 /* Purge any queued data */
1134 skb_queue_purge(&sk->sk_receive_queue);
1135 skb_queue_purge(&sk->sk_write_queue);
1136 skb_queue_purge(&session->reorder_q);
1137
1138 release_sock(sk);
1139 sock_put(sk);
1140
1141 /* Now restart from the beginning of this hash
1142 * chain. We always remove a session from the
1143 * list so we are guaranteed to make forward
1144 * progress.
1145 */
1146 write_lock(&tunnel->hlist_lock);
1147 goto again;
1148 }
1149 }
1150 write_unlock(&tunnel->hlist_lock);
1151}
1152
1153/* Really kill the tunnel.
1154 * Come here only when all sessions have been cleared from the tunnel.
1155 */
1156static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1157{
1158 /* Remove from socket list */
1159 write_lock(&pppol2tp_tunnel_list_lock);
1160 list_del_init(&tunnel->list);
1161 write_unlock(&pppol2tp_tunnel_list_lock);
1162
1163 atomic_dec(&pppol2tp_tunnel_count);
1164 kfree(tunnel);
1165}
1166
1167/* Tunnel UDP socket destruct hook.
1168 * The tunnel context is deleted only when all session sockets have been
1169 * closed.
1170 */
1171static void pppol2tp_tunnel_destruct(struct sock *sk)
1172{
1173 struct pppol2tp_tunnel *tunnel;
1174
1175 tunnel = pppol2tp_sock_to_tunnel(sk);
1176 if (tunnel == NULL)
1177 goto end;
1178
1179 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1180 "%s: closing...\n", tunnel->name);
1181
1182 /* Close all sessions */
1183 pppol2tp_tunnel_closeall(tunnel);
1184
1185 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1186 (udp_sk(sk))->encap_type = 0;
1187 (udp_sk(sk))->encap_rcv = NULL;
1188
1189 /* Remove hooks into tunnel socket */
1190 tunnel->sock = NULL;
1191 sk->sk_destruct = tunnel->old_sk_destruct;
1192 sk->sk_user_data = NULL;
1193
1194 /* Call original (UDP) socket descructor */
1195 if (sk->sk_destruct != NULL)
1196 (*sk->sk_destruct)(sk);
1197
1198 pppol2tp_tunnel_dec_refcount(tunnel);
1199
1200end:
1201 return;
1202}
1203
1204/* Really kill the session socket. (Called from sock_put() if
1205 * refcnt == 0.)
1206 */
1207static void pppol2tp_session_destruct(struct sock *sk)
1208{
1209 struct pppol2tp_session *session = NULL;
1210
1211 if (sk->sk_user_data != NULL) {
1212 struct pppol2tp_tunnel *tunnel;
1213
1214 session = pppol2tp_sock_to_session(sk);
1215 if (session == NULL)
1216 goto out;
1217
1218 /* Don't use pppol2tp_sock_to_tunnel() here to
1219 * get the tunnel context because the tunnel
1220 * socket might have already been closed (its
1221 * sk->sk_user_data will be NULL) so use the
1222 * session's private tunnel ptr instead.
1223 */
1224 tunnel = session->tunnel;
1225 if (tunnel != NULL) {
1226 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1227
1228 /* If session_id is zero, this is a null
1229 * session context, which was created for a
1230 * socket that is being used only to manage
1231 * tunnels.
1232 */
1233 if (session->tunnel_addr.s_session != 0) {
1234 /* Delete the session socket from the
1235 * hash
1236 */
1237 write_lock(&tunnel->hlist_lock);
1238 hlist_del_init(&session->hlist);
1239 write_unlock(&tunnel->hlist_lock);
1240
1241 atomic_dec(&pppol2tp_session_count);
1242 }
1243
1244 /* This will delete the tunnel context if this
1245 * is the last session on the tunnel.
1246 */
1247 session->tunnel = NULL;
1248 session->tunnel_sock = NULL;
1249 pppol2tp_tunnel_dec_refcount(tunnel);
1250 }
1251 }
1252
1253 kfree(session);
1254out:
1255 return;
1256}
1257
1258/* Called when the PPPoX socket (session) is closed.
1259 */
1260static int pppol2tp_release(struct socket *sock)
1261{
1262 struct sock *sk = sock->sk;
1263 int error;
1264
1265 if (!sk)
1266 return 0;
1267
1268 error = -EBADF;
1269 lock_sock(sk);
1270 if (sock_flag(sk, SOCK_DEAD) != 0)
1271 goto error;
1272
1273 pppox_unbind_sock(sk);
1274
1275 /* Signal the death of the socket. */
1276 sk->sk_state = PPPOX_DEAD;
1277 sock_orphan(sk);
1278 sock->sk = NULL;
1279
1280 /* Purge any queued data */
1281 skb_queue_purge(&sk->sk_receive_queue);
1282 skb_queue_purge(&sk->sk_write_queue);
1283
1284 release_sock(sk);
1285
1286 /* This will delete the session context via
1287 * pppol2tp_session_destruct() if the socket's refcnt drops to
1288 * zero.
1289 */
1290 sock_put(sk);
1291
1292 return 0;
1293
1294error:
1295 release_sock(sk);
1296 return error;
1297}
1298
1299/* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1300 * sockets attached to it.
1301 */
1302static struct sock *pppol2tp_prepare_tunnel_socket(int fd, u16 tunnel_id,
1303 int *error)
1304{
1305 int err;
1306 struct socket *sock = NULL;
1307 struct sock *sk;
1308 struct pppol2tp_tunnel *tunnel;
1309 struct sock *ret = NULL;
1310
1311 /* Get the tunnel UDP socket from the fd, which was opened by
1312 * the userspace L2TP daemon.
1313 */
1314 err = -EBADF;
1315 sock = sockfd_lookup(fd, &err);
1316 if (!sock) {
1317 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1318 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1319 tunnel_id, fd, err);
1320 goto err;
1321 }
1322
1323 /* Quick sanity checks */
1324 err = -ESOCKTNOSUPPORT;
1325 if (sock->type != SOCK_DGRAM) {
1326 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1327 "tunl %hu: fd %d wrong type, got %d, expected %d\n",
1328 tunnel_id, fd, sock->type, SOCK_DGRAM);
1329 goto err;
1330 }
1331 err = -EAFNOSUPPORT;
1332 if (sock->ops->family != AF_INET) {
1333 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1334 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1335 tunnel_id, fd, sock->ops->family, AF_INET);
1336 goto err;
1337 }
1338
1339 err = -ENOTCONN;
1340 sk = sock->sk;
1341
1342 /* Check if this socket has already been prepped */
1343 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1344 if (tunnel != NULL) {
1345 /* User-data field already set */
1346 err = -EBUSY;
1347 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1348
1349 /* This socket has already been prepped */
1350 ret = tunnel->sock;
1351 goto out;
1352 }
1353
1354 /* This socket is available and needs prepping. Create a new tunnel
1355 * context and init it.
1356 */
1357 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1358 if (sk->sk_user_data == NULL) {
1359 err = -ENOMEM;
1360 goto err;
1361 }
1362
1363 tunnel->magic = L2TP_TUNNEL_MAGIC;
1364 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1365
1366 tunnel->stats.tunnel_id = tunnel_id;
1367 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1368
1369 /* Hook on the tunnel socket destructor so that we can cleanup
1370 * if the tunnel socket goes away.
1371 */
1372 tunnel->old_sk_destruct = sk->sk_destruct;
1373 sk->sk_destruct = &pppol2tp_tunnel_destruct;
1374
1375 tunnel->sock = sk;
1376 sk->sk_allocation = GFP_ATOMIC;
1377
1378 /* Misc init */
1379 rwlock_init(&tunnel->hlist_lock);
1380
1381 /* Add tunnel to our list */
1382 INIT_LIST_HEAD(&tunnel->list);
1383 write_lock(&pppol2tp_tunnel_list_lock);
1384 list_add(&tunnel->list, &pppol2tp_tunnel_list);
1385 write_unlock(&pppol2tp_tunnel_list_lock);
1386 atomic_inc(&pppol2tp_tunnel_count);
1387
1388 /* Bump the reference count. The tunnel context is deleted
1389 * only when this drops to zero.
1390 */
1391 pppol2tp_tunnel_inc_refcount(tunnel);
1392
1393 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1394 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1395 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1396
1397 ret = tunnel->sock;
1398
1399 *error = 0;
1400out:
1401 if (sock)
1402 sockfd_put(sock);
1403
1404 return ret;
1405
1406err:
1407 *error = err;
1408 goto out;
1409}
1410
1411static struct proto pppol2tp_sk_proto = {
1412 .name = "PPPOL2TP",
1413 .owner = THIS_MODULE,
1414 .obj_size = sizeof(struct pppox_sock),
1415};
1416
1417/* socket() handler. Initialize a new struct sock.
1418 */
1419static int pppol2tp_create(struct socket *sock)
1420{
1421 int error = -ENOMEM;
1422 struct sock *sk;
1423
1424 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, 1);
1425 if (!sk)
1426 goto out;
1427
1428 sock_init_data(sock, sk);
1429
1430 sock->state = SS_UNCONNECTED;
1431 sock->ops = &pppol2tp_ops;
1432
1433 sk->sk_backlog_rcv = pppol2tp_recv_core;
1434 sk->sk_protocol = PX_PROTO_OL2TP;
1435 sk->sk_family = PF_PPPOX;
1436 sk->sk_state = PPPOX_NONE;
1437 sk->sk_type = SOCK_STREAM;
1438 sk->sk_destruct = pppol2tp_session_destruct;
1439
1440 error = 0;
1441
1442out:
1443 return error;
1444}
1445
1446/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1447 */
1448static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1449 int sockaddr_len, int flags)
1450{
1451 struct sock *sk = sock->sk;
1452 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1453 struct pppox_sock *po = pppox_sk(sk);
1454 struct sock *tunnel_sock = NULL;
1455 struct pppol2tp_session *session = NULL;
1456 struct pppol2tp_tunnel *tunnel;
1457 struct dst_entry *dst;
1458 int error = 0;
1459
1460 lock_sock(sk);
1461
1462 error = -EINVAL;
1463 if (sp->sa_protocol != PX_PROTO_OL2TP)
1464 goto end;
1465
1466 /* Check for already bound sockets */
1467 error = -EBUSY;
1468 if (sk->sk_state & PPPOX_CONNECTED)
1469 goto end;
1470
1471 /* We don't supporting rebinding anyway */
1472 error = -EALREADY;
1473 if (sk->sk_user_data)
1474 goto end; /* socket is already attached */
1475
1476 /* Don't bind if s_tunnel is 0 */
1477 error = -EINVAL;
1478 if (sp->pppol2tp.s_tunnel == 0)
1479 goto end;
1480
1481 /* Special case: prepare tunnel socket if s_session and
1482 * d_session is 0. Otherwise look up tunnel using supplied
1483 * tunnel id.
1484 */
1485 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
1486 tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.fd,
1487 sp->pppol2tp.s_tunnel,
1488 &error);
1489 if (tunnel_sock == NULL)
1490 goto end;
1491
1492 tunnel = tunnel_sock->sk_user_data;
1493 } else {
1494 tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel);
1495
1496 /* Error if we can't find the tunnel */
1497 error = -ENOENT;
1498 if (tunnel == NULL)
1499 goto end;
1500
1501 tunnel_sock = tunnel->sock;
1502 }
1503
1504 /* Check that this session doesn't already exist */
1505 error = -EEXIST;
1506 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1507 if (session != NULL)
1508 goto end;
1509
1510 /* Allocate and initialize a new session context. */
1511 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1512 if (session == NULL) {
1513 error = -ENOMEM;
1514 goto end;
1515 }
1516
1517 skb_queue_head_init(&session->reorder_q);
1518
1519 session->magic = L2TP_SESSION_MAGIC;
1520 session->owner = current->pid;
1521 session->sock = sk;
1522 session->tunnel = tunnel;
1523 session->tunnel_sock = tunnel_sock;
1524 session->tunnel_addr = sp->pppol2tp;
1525 sprintf(&session->name[0], "sess %hu/%hu",
1526 session->tunnel_addr.s_tunnel,
1527 session->tunnel_addr.s_session);
1528
1529 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1530 session->stats.session_id = session->tunnel_addr.s_session;
1531
1532 INIT_HLIST_NODE(&session->hlist);
1533
1534 /* Inherit debug options from tunnel */
1535 session->debug = tunnel->debug;
1536
1537 /* Default MTU must allow space for UDP/L2TP/PPP
1538 * headers.
1539 */
1540 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1541
1542 /* If PMTU discovery was enabled, use the MTU that was discovered */
1543 dst = sk_dst_get(sk);
1544 if (dst != NULL) {
1545 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1546 if (pmtu != 0)
1547 session->mtu = session->mru = pmtu -
1548 PPPOL2TP_HEADER_OVERHEAD;
1549 dst_release(dst);
1550 }
1551
1552 /* Special case: if source & dest session_id == 0x0000, this socket is
1553 * being created to manage the tunnel. Don't add the session to the
1554 * session hash list, just set up the internal context for use by
1555 * ioctl() and sockopt() handlers.
1556 */
1557 if ((session->tunnel_addr.s_session == 0) &&
1558 (session->tunnel_addr.d_session == 0)) {
1559 error = 0;
1560 sk->sk_user_data = session;
1561 goto out_no_ppp;
1562 }
1563
1564 /* Get tunnel context from the tunnel socket */
1565 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1566 if (tunnel == NULL) {
1567 error = -EBADF;
1568 goto end;
1569 }
1570
1571 /* Right now, because we don't have a way to push the incoming skb's
1572 * straight through the UDP layer, the only header we need to worry
1573 * about is the L2TP header. This size is different depending on
1574 * whether sequence numbers are enabled for the data channel.
1575 */
1576 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1577
1578 po->chan.private = sk;
1579 po->chan.ops = &pppol2tp_chan_ops;
1580 po->chan.mtu = session->mtu;
1581
1582 error = ppp_register_channel(&po->chan);
1583 if (error)
1584 goto end;
1585
1586 /* This is how we get the session context from the socket. */
1587 sk->sk_user_data = session;
1588
1589 /* Add session to the tunnel's hash list */
1590 write_lock(&tunnel->hlist_lock);
1591 hlist_add_head(&session->hlist,
1592 pppol2tp_session_id_hash(tunnel,
1593 session->tunnel_addr.s_session));
1594 write_unlock(&tunnel->hlist_lock);
1595
1596 atomic_inc(&pppol2tp_session_count);
1597
1598out_no_ppp:
1599 pppol2tp_tunnel_inc_refcount(tunnel);
1600 sk->sk_state = PPPOX_CONNECTED;
1601 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1602 "%s: created\n", session->name);
1603
1604end:
1605 release_sock(sk);
1606
1607 if (error != 0)
1608 PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1609 "%s: connect failed: %d\n", session->name, error);
1610
1611 return error;
1612}
1613
1614/* getname() support.
1615 */
1616static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1617 int *usockaddr_len, int peer)
1618{
1619 int len = sizeof(struct sockaddr_pppol2tp);
1620 struct sockaddr_pppol2tp sp;
1621 int error = 0;
1622 struct pppol2tp_session *session;
1623
1624 error = -ENOTCONN;
1625 if (sock->sk->sk_state != PPPOX_CONNECTED)
1626 goto end;
1627
1628 session = pppol2tp_sock_to_session(sock->sk);
1629 if (session == NULL) {
1630 error = -EBADF;
1631 goto end;
1632 }
1633
1634 sp.sa_family = AF_PPPOX;
1635 sp.sa_protocol = PX_PROTO_OL2TP;
1636 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1637 sizeof(struct pppol2tp_addr));
1638
1639 memcpy(uaddr, &sp, len);
1640
1641 *usockaddr_len = len;
1642
1643 error = 0;
1644
1645end:
1646 return error;
1647}
1648
1649/****************************************************************************
1650 * ioctl() handlers.
1651 *
1652 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1653 * sockets. However, in order to control kernel tunnel features, we allow
1654 * userspace to create a special "tunnel" PPPoX socket which is used for
1655 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1656 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1657 * calls.
1658 ****************************************************************************/
1659
1660/* Session ioctl helper.
1661 */
1662static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1663 unsigned int cmd, unsigned long arg)
1664{
1665 struct ifreq ifr;
1666 int err = 0;
1667 struct sock *sk = session->sock;
1668 int val = (int) arg;
1669
1670 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1671 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1672 session->name, cmd, arg);
1673
1674 sock_hold(sk);
1675
1676 switch (cmd) {
1677 case SIOCGIFMTU:
1678 err = -ENXIO;
1679 if (!(sk->sk_state & PPPOX_CONNECTED))
1680 break;
1681
1682 err = -EFAULT;
1683 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1684 break;
1685 ifr.ifr_mtu = session->mtu;
1686 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1687 break;
1688
1689 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1690 "%s: get mtu=%d\n", session->name, session->mtu);
1691 err = 0;
1692 break;
1693
1694 case SIOCSIFMTU:
1695 err = -ENXIO;
1696 if (!(sk->sk_state & PPPOX_CONNECTED))
1697 break;
1698
1699 err = -EFAULT;
1700 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1701 break;
1702
1703 session->mtu = ifr.ifr_mtu;
1704
1705 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1706 "%s: set mtu=%d\n", session->name, session->mtu);
1707 err = 0;
1708 break;
1709
1710 case PPPIOCGMRU:
1711 err = -ENXIO;
1712 if (!(sk->sk_state & PPPOX_CONNECTED))
1713 break;
1714
1715 err = -EFAULT;
1716 if (put_user(session->mru, (int __user *) arg))
1717 break;
1718
1719 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1720 "%s: get mru=%d\n", session->name, session->mru);
1721 err = 0;
1722 break;
1723
1724 case PPPIOCSMRU:
1725 err = -ENXIO;
1726 if (!(sk->sk_state & PPPOX_CONNECTED))
1727 break;
1728
1729 err = -EFAULT;
1730 if (get_user(val,(int __user *) arg))
1731 break;
1732
1733 session->mru = val;
1734 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1735 "%s: set mru=%d\n", session->name, session->mru);
1736 err = 0;
1737 break;
1738
1739 case PPPIOCGFLAGS:
1740 err = -EFAULT;
1741 if (put_user(session->flags, (int __user *) arg))
1742 break;
1743
1744 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1745 "%s: get flags=%d\n", session->name, session->flags);
1746 err = 0;
1747 break;
1748
1749 case PPPIOCSFLAGS:
1750 err = -EFAULT;
1751 if (get_user(val, (int __user *) arg))
1752 break;
1753 session->flags = val;
1754 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1755 "%s: set flags=%d\n", session->name, session->flags);
1756 err = 0;
1757 break;
1758
1759 case PPPIOCGL2TPSTATS:
1760 err = -ENXIO;
1761 if (!(sk->sk_state & PPPOX_CONNECTED))
1762 break;
1763
1764 if (copy_to_user((void __user *) arg, &session->stats,
1765 sizeof(session->stats)))
1766 break;
1767 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1768 "%s: get L2TP stats\n", session->name);
1769 err = 0;
1770 break;
1771
1772 default:
1773 err = -ENOSYS;
1774 break;
1775 }
1776
1777 sock_put(sk);
1778
1779 return err;
1780}
1781
1782/* Tunnel ioctl helper.
1783 *
1784 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1785 * specifies a session_id, the session ioctl handler is called. This allows an
1786 * application to retrieve session stats via a tunnel socket.
1787 */
1788static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1789 unsigned int cmd, unsigned long arg)
1790{
1791 int err = 0;
1792 struct sock *sk = tunnel->sock;
1793 struct pppol2tp_ioc_stats stats_req;
1794
1795 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1796 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1797 cmd, arg);
1798
1799 sock_hold(sk);
1800
1801 switch (cmd) {
1802 case PPPIOCGL2TPSTATS:
1803 err = -ENXIO;
1804 if (!(sk->sk_state & PPPOX_CONNECTED))
1805 break;
1806
1807 if (copy_from_user(&stats_req, (void __user *) arg,
1808 sizeof(stats_req))) {
1809 err = -EFAULT;
1810 break;
1811 }
1812 if (stats_req.session_id != 0) {
1813 /* resend to session ioctl handler */
1814 struct pppol2tp_session *session =
1815 pppol2tp_session_find(tunnel, stats_req.session_id);
1816 if (session != NULL)
1817 err = pppol2tp_session_ioctl(session, cmd, arg);
1818 else
1819 err = -EBADR;
1820 break;
1821 }
1822#ifdef CONFIG_XFRM
1823 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1824#endif
1825 if (copy_to_user((void __user *) arg, &tunnel->stats,
1826 sizeof(tunnel->stats))) {
1827 err = -EFAULT;
1828 break;
1829 }
1830 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1831 "%s: get L2TP stats\n", tunnel->name);
1832 err = 0;
1833 break;
1834
1835 default:
1836 err = -ENOSYS;
1837 break;
1838 }
1839
1840 sock_put(sk);
1841
1842 return err;
1843}
1844
1845/* Main ioctl() handler.
1846 * Dispatch to tunnel or session helpers depending on the socket.
1847 */
1848static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1849 unsigned long arg)
1850{
1851 struct sock *sk = sock->sk;
1852 struct pppol2tp_session *session;
1853 struct pppol2tp_tunnel *tunnel;
1854 int err;
1855
1856 if (!sk)
1857 return 0;
1858
1859 err = -EBADF;
1860 if (sock_flag(sk, SOCK_DEAD) != 0)
1861 goto end;
1862
1863 err = -ENOTCONN;
1864 if ((sk->sk_user_data == NULL) ||
1865 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1866 goto end;
1867
1868 /* Get session context from the socket */
1869 err = -EBADF;
1870 session = pppol2tp_sock_to_session(sk);
1871 if (session == NULL)
1872 goto end;
1873
1874 /* Special case: if session's session_id is zero, treat ioctl as a
1875 * tunnel ioctl
1876 */
1877 if ((session->tunnel_addr.s_session == 0) &&
1878 (session->tunnel_addr.d_session == 0)) {
1879 err = -EBADF;
1880 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
1881 if (tunnel == NULL)
1882 goto end;
1883
1884 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1885 goto end;
1886 }
1887
1888 err = pppol2tp_session_ioctl(session, cmd, arg);
1889
1890end:
1891 return err;
1892}
1893
1894/*****************************************************************************
1895 * setsockopt() / getsockopt() support.
1896 *
1897 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1898 * sockets. In order to control kernel tunnel features, we allow userspace to
1899 * create a special "tunnel" PPPoX socket which is used for control only.
1900 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1901 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1902 *****************************************************************************/
1903
1904/* Tunnel setsockopt() helper.
1905 */
1906static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1907 struct pppol2tp_tunnel *tunnel,
1908 int optname, int val)
1909{
1910 int err = 0;
1911
1912 switch (optname) {
1913 case PPPOL2TP_SO_DEBUG:
1914 tunnel->debug = val;
1915 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1916 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1917 break;
1918
1919 default:
1920 err = -ENOPROTOOPT;
1921 break;
1922 }
1923
1924 return err;
1925}
1926
1927/* Session setsockopt helper.
1928 */
1929static int pppol2tp_session_setsockopt(struct sock *sk,
1930 struct pppol2tp_session *session,
1931 int optname, int val)
1932{
1933 int err = 0;
1934
1935 switch (optname) {
1936 case PPPOL2TP_SO_RECVSEQ:
1937 if ((val != 0) && (val != 1)) {
1938 err = -EINVAL;
1939 break;
1940 }
1941 session->recv_seq = val ? -1 : 0;
1942 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1943 "%s: set recv_seq=%d\n", session->name,
1944 session->recv_seq);
1945 break;
1946
1947 case PPPOL2TP_SO_SENDSEQ:
1948 if ((val != 0) && (val != 1)) {
1949 err = -EINVAL;
1950 break;
1951 }
1952 session->send_seq = val ? -1 : 0;
1953 {
1954 struct sock *ssk = session->sock;
1955 struct pppox_sock *po = pppox_sk(ssk);
1956 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1957 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1958 }
1959 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1960 "%s: set send_seq=%d\n", session->name, session->send_seq);
1961 break;
1962
1963 case PPPOL2TP_SO_LNSMODE:
1964 if ((val != 0) && (val != 1)) {
1965 err = -EINVAL;
1966 break;
1967 }
1968 session->lns_mode = val ? -1 : 0;
1969 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1970 "%s: set lns_mode=%d\n", session->name,
1971 session->lns_mode);
1972 break;
1973
1974 case PPPOL2TP_SO_DEBUG:
1975 session->debug = val;
1976 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1977 "%s: set debug=%x\n", session->name, session->debug);
1978 break;
1979
1980 case PPPOL2TP_SO_REORDERTO:
1981 session->reorder_timeout = msecs_to_jiffies(val);
1982 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1983 "%s: set reorder_timeout=%d\n", session->name,
1984 session->reorder_timeout);
1985 break;
1986
1987 default:
1988 err = -ENOPROTOOPT;
1989 break;
1990 }
1991
1992 return err;
1993}
1994
1995/* Main setsockopt() entry point.
1996 * Does API checks, then calls either the tunnel or session setsockopt
1997 * handler, according to whether the PPPoL2TP socket is a for a regular
1998 * session or the special tunnel type.
1999 */
2000static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
2001 char __user *optval, int optlen)
2002{
2003 struct sock *sk = sock->sk;
2004 struct pppol2tp_session *session = sk->sk_user_data;
2005 struct pppol2tp_tunnel *tunnel;
2006 int val;
2007 int err;
2008
2009 if (level != SOL_PPPOL2TP)
2010 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2011
2012 if (optlen < sizeof(int))
2013 return -EINVAL;
2014
2015 if (get_user(val, (int __user *)optval))
2016 return -EFAULT;
2017
2018 err = -ENOTCONN;
2019 if (sk->sk_user_data == NULL)
2020 goto end;
2021
2022 /* Get session context from the socket */
2023 err = -EBADF;
2024 session = pppol2tp_sock_to_session(sk);
2025 if (session == NULL)
2026 goto end;
2027
2028 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2029 */
2030 if ((session->tunnel_addr.s_session == 0) &&
2031 (session->tunnel_addr.d_session == 0)) {
2032 err = -EBADF;
2033 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2034 if (tunnel == NULL)
2035 goto end;
2036
2037 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2038 } else
2039 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2040
2041 err = 0;
2042
2043end:
2044 return err;
2045}
2046
2047/* Tunnel getsockopt helper. Called with sock locked.
2048 */
2049static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2050 struct pppol2tp_tunnel *tunnel,
2051 int optname, int __user *val)
2052{
2053 int err = 0;
2054
2055 switch (optname) {
2056 case PPPOL2TP_SO_DEBUG:
2057 *val = tunnel->debug;
2058 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2059 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2060 break;
2061
2062 default:
2063 err = -ENOPROTOOPT;
2064 break;
2065 }
2066
2067 return err;
2068}
2069
2070/* Session getsockopt helper. Called with sock locked.
2071 */
2072static int pppol2tp_session_getsockopt(struct sock *sk,
2073 struct pppol2tp_session *session,
2074 int optname, int __user *val)
2075{
2076 int err = 0;
2077
2078 switch (optname) {
2079 case PPPOL2TP_SO_RECVSEQ:
2080 *val = session->recv_seq;
2081 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2082 "%s: get recv_seq=%d\n", session->name, *val);
2083 break;
2084
2085 case PPPOL2TP_SO_SENDSEQ:
2086 *val = session->send_seq;
2087 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2088 "%s: get send_seq=%d\n", session->name, *val);
2089 break;
2090
2091 case PPPOL2TP_SO_LNSMODE:
2092 *val = session->lns_mode;
2093 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2094 "%s: get lns_mode=%d\n", session->name, *val);
2095 break;
2096
2097 case PPPOL2TP_SO_DEBUG:
2098 *val = session->debug;
2099 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2100 "%s: get debug=%d\n", session->name, *val);
2101 break;
2102
2103 case PPPOL2TP_SO_REORDERTO:
2104 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2105 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2106 "%s: get reorder_timeout=%d\n", session->name, *val);
2107 break;
2108
2109 default:
2110 err = -ENOPROTOOPT;
2111 }
2112
2113 return err;
2114}
2115
2116/* Main getsockopt() entry point.
2117 * Does API checks, then calls either the tunnel or session getsockopt
2118 * handler, according to whether the PPPoX socket is a for a regular session
2119 * or the special tunnel type.
2120 */
2121static int pppol2tp_getsockopt(struct socket *sock, int level,
2122 int optname, char __user *optval, int __user *optlen)
2123{
2124 struct sock *sk = sock->sk;
2125 struct pppol2tp_session *session = sk->sk_user_data;
2126 struct pppol2tp_tunnel *tunnel;
2127 int val, len;
2128 int err;
2129
2130 if (level != SOL_PPPOL2TP)
2131 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2132
2133 if (get_user(len, (int __user *) optlen))
2134 return -EFAULT;
2135
2136 len = min_t(unsigned int, len, sizeof(int));
2137
2138 if (len < 0)
2139 return -EINVAL;
2140
2141 err = -ENOTCONN;
2142 if (sk->sk_user_data == NULL)
2143 goto end;
2144
2145 /* Get the session context */
2146 err = -EBADF;
2147 session = pppol2tp_sock_to_session(sk);
2148 if (session == NULL)
2149 goto end;
2150
2151 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2152 if ((session->tunnel_addr.s_session == 0) &&
2153 (session->tunnel_addr.d_session == 0)) {
2154 err = -EBADF;
2155 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2156 if (tunnel == NULL)
2157 goto end;
2158
2159 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2160 } else
2161 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2162
2163 err = -EFAULT;
2164 if (put_user(len, (int __user *) optlen))
2165 goto end;
2166
2167 if (copy_to_user((void __user *) optval, &val, len))
2168 goto end;
2169
2170 err = 0;
2171end:
2172 return err;
2173}
2174
2175/*****************************************************************************
2176 * /proc filesystem for debug
2177 *****************************************************************************/
2178
2179#ifdef CONFIG_PROC_FS
2180
2181#include <linux/seq_file.h>
2182
2183struct pppol2tp_seq_data {
2184 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2185 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
2186};
2187
2188static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2189{
2190 struct pppol2tp_session *session = NULL;
2191 struct hlist_node *walk;
2192 int found = 0;
2193 int next = 0;
2194 int i;
2195
2196 read_lock(&tunnel->hlist_lock);
2197 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2198 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2199 if (curr == NULL) {
2200 found = 1;
2201 goto out;
2202 }
2203 if (session == curr) {
2204 next = 1;
2205 continue;
2206 }
2207 if (next) {
2208 found = 1;
2209 goto out;
2210 }
2211 }
2212 }
2213out:
2214 read_unlock(&tunnel->hlist_lock);
2215 if (!found)
2216 session = NULL;
2217
2218 return session;
2219}
2220
2221static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_tunnel *curr)
2222{
2223 struct pppol2tp_tunnel *tunnel = NULL;
2224
2225 read_lock(&pppol2tp_tunnel_list_lock);
2226 if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) {
2227 goto out;
2228 }
2229 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2230out:
2231 read_unlock(&pppol2tp_tunnel_list_lock);
2232
2233 return tunnel;
2234}
2235
2236static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2237{
2238 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
2239 loff_t pos = *offs;
2240
2241 if (!pos)
2242 goto out;
2243
2244 BUG_ON(m->private == NULL);
2245 pd = m->private;
2246
2247 if (pd->tunnel == NULL) {
2248 if (!list_empty(&pppol2tp_tunnel_list))
2249 pd->tunnel = list_entry(pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
2250 } else {
2251 pd->session = next_session(pd->tunnel, pd->session);
2252 if (pd->session == NULL) {
2253 pd->tunnel = next_tunnel(pd->tunnel);
2254 }
2255 }
2256
2257 /* NULL tunnel and session indicates end of list */
2258 if ((pd->tunnel == NULL) && (pd->session == NULL))
2259 pd = NULL;
2260
2261out:
2262 return pd;
2263}
2264
2265static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2266{
2267 (*pos)++;
2268 return NULL;
2269}
2270
2271static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2272{
2273 /* nothing to do */
2274}
2275
2276static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2277{
2278 struct pppol2tp_tunnel *tunnel = v;
2279
2280 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2281 tunnel->name,
2282 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2283 atomic_read(&tunnel->ref_count) - 1);
2284 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2285 tunnel->debug,
2286 tunnel->stats.tx_packets, tunnel->stats.tx_bytes,
2287 tunnel->stats.tx_errors,
2288 tunnel->stats.rx_packets, tunnel->stats.rx_bytes,
2289 tunnel->stats.rx_errors);
2290}
2291
2292static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2293{
2294 struct pppol2tp_session *session = v;
2295
2296 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2297 "%04X/%04X %d %c\n",
2298 session->name,
2299 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2300 ntohs(session->tunnel_addr.addr.sin_port),
2301 session->tunnel_addr.s_tunnel,
2302 session->tunnel_addr.s_session,
2303 session->tunnel_addr.d_tunnel,
2304 session->tunnel_addr.d_session,
2305 session->sock->sk_state,
2306 (session == session->sock->sk_user_data) ?
2307 'Y' : 'N');
2308 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2309 session->mtu, session->mru,
2310 session->recv_seq ? 'R' : '-',
2311 session->send_seq ? 'S' : '-',
2312 session->lns_mode ? "LNS" : "LAC",
2313 session->debug,
2314 jiffies_to_msecs(session->reorder_timeout));
2315 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2316 session->nr, session->ns,
2317 session->stats.tx_packets,
2318 session->stats.tx_bytes,
2319 session->stats.tx_errors,
2320 session->stats.rx_packets,
2321 session->stats.rx_bytes,
2322 session->stats.rx_errors);
2323}
2324
2325static int pppol2tp_seq_show(struct seq_file *m, void *v)
2326{
2327 struct pppol2tp_seq_data *pd = v;
2328
2329 /* display header on line 1 */
2330 if (v == SEQ_START_TOKEN) {
2331 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2332 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2333 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2334 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2335 "dest-tid/sid state user-data-ok\n");
2336 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2337 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2338 goto out;
2339 }
2340
2341 /* Show the tunnel or session context.
2342 */
2343 if (pd->session == NULL)
2344 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2345 else
2346 pppol2tp_seq_session_show(m, pd->session);
2347
2348out:
2349 return 0;
2350}
2351
2352static struct seq_operations pppol2tp_seq_ops = {
2353 .start = pppol2tp_seq_start,
2354 .next = pppol2tp_seq_next,
2355 .stop = pppol2tp_seq_stop,
2356 .show = pppol2tp_seq_show,
2357};
2358
2359/* Called when our /proc file is opened. We allocate data for use when
2360 * iterating our tunnel / session contexts and store it in the private
2361 * data of the seq_file.
2362 */
2363static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2364{
2365 struct seq_file *m;
2366 struct pppol2tp_seq_data *pd;
2367 int ret = 0;
2368
2369 ret = seq_open(file, &pppol2tp_seq_ops);
2370 if (ret < 0)
2371 goto out;
2372
2373 m = file->private_data;
2374
2375 /* Allocate and fill our proc_data for access later */
2376 ret = -ENOMEM;
2377 m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL);
2378 if (m->private == NULL)
2379 goto out;
2380
2381 pd = m->private;
2382 ret = 0;
2383
2384out:
2385 return ret;
2386}
2387
2388/* Called when /proc file access completes.
2389 */
2390static int pppol2tp_proc_release(struct inode *inode, struct file *file)
2391{
2392 struct seq_file *m = (struct seq_file *)file->private_data;
2393
2394 kfree(m->private);
2395 m->private = NULL;
2396
2397 return seq_release(inode, file);
2398}
2399
2400static struct file_operations pppol2tp_proc_fops = {
2401 .owner = THIS_MODULE,
2402 .open = pppol2tp_proc_open,
2403 .read = seq_read,
2404 .llseek = seq_lseek,
2405 .release = pppol2tp_proc_release,
2406};
2407
2408static struct proc_dir_entry *pppol2tp_proc;
2409
2410#endif /* CONFIG_PROC_FS */
2411
2412/*****************************************************************************
2413 * Init and cleanup
2414 *****************************************************************************/
2415
2416static struct proto_ops pppol2tp_ops = {
2417 .family = AF_PPPOX,
2418 .owner = THIS_MODULE,
2419 .release = pppol2tp_release,
2420 .bind = sock_no_bind,
2421 .connect = pppol2tp_connect,
2422 .socketpair = sock_no_socketpair,
2423 .accept = sock_no_accept,
2424 .getname = pppol2tp_getname,
2425 .poll = datagram_poll,
2426 .listen = sock_no_listen,
2427 .shutdown = sock_no_shutdown,
2428 .setsockopt = pppol2tp_setsockopt,
2429 .getsockopt = pppol2tp_getsockopt,
2430 .sendmsg = pppol2tp_sendmsg,
2431 .recvmsg = pppol2tp_recvmsg,
2432 .mmap = sock_no_mmap,
2433 .ioctl = pppox_ioctl,
2434};
2435
2436static struct pppox_proto pppol2tp_proto = {
2437 .create = pppol2tp_create,
2438 .ioctl = pppol2tp_ioctl
2439};
2440
2441static int __init pppol2tp_init(void)
2442{
2443 int err;
2444
2445 err = proto_register(&pppol2tp_sk_proto, 0);
2446 if (err)
2447 goto out;
2448 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2449 if (err)
2450 goto out_unregister_pppol2tp_proto;
2451
2452#ifdef CONFIG_PROC_FS
2453 pppol2tp_proc = create_proc_entry("pppol2tp", 0, proc_net);
2454 if (!pppol2tp_proc) {
2455 err = -ENOMEM;
2456 goto out_unregister_pppox_proto;
2457 }
2458 pppol2tp_proc->proc_fops = &pppol2tp_proc_fops;
2459#endif /* CONFIG_PROC_FS */
2460 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2461 PPPOL2TP_DRV_VERSION);
2462
2463out:
2464 return err;
2465
2466out_unregister_pppox_proto:
2467 unregister_pppox_proto(PX_PROTO_OL2TP);
2468out_unregister_pppol2tp_proto:
2469 proto_unregister(&pppol2tp_sk_proto);
2470 goto out;
2471}
2472
2473static void __exit pppol2tp_exit(void)
2474{
2475 unregister_pppox_proto(PX_PROTO_OL2TP);
2476
2477#ifdef CONFIG_PROC_FS
2478 remove_proc_entry("pppol2tp", proc_net);
2479#endif
2480 proto_unregister(&pppol2tp_sk_proto);
2481}
2482
2483module_init(pppol2tp_init);
2484module_exit(pppol2tp_exit);
2485
2486MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>,"
2487 "James Chapman <jchapman@katalix.com>");
2488MODULE_DESCRIPTION("PPP over L2TP over UDP");
2489MODULE_LICENSE("GPL");
2490MODULE_VERSION(PPPOL2TP_DRV_VERSION);