blob: 5981debcde5e4895fa1f787aa0baeb4bf5aba803 [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>
James Chapman3557baa2007-06-27 15:49:24 -070064#include <linux/string.h>
65#include <linux/list.h>
66#include <asm/uaccess.h>
67
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>
75
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>
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -080093#include <linux/nsproxy.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020094#include <net/net_namespace.h>
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -080095#include <net/netns/generic.h>
James Chapman3557baa2007-06-27 15:49:24 -070096#include <net/dst.h>
97#include <net/ip.h>
98#include <net/udp.h>
99#include <net/xfrm.h>
100
101#include <asm/byteorder.h>
102#include <asm/atomic.h>
103
104
105#define PPPOL2TP_DRV_VERSION "V1.0"
106
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
113
114#define L2TP_HDR_VER_MASK 0x000F
115#define L2TP_HDR_VER 0x0002
116
117/* Space for UDP, L2TP and PPP headers */
118#define PPPOL2TP_HEADER_OVERHEAD 40
119
120/* Just some random numbers */
121#define L2TP_TUNNEL_MAGIC 0x42114DDA
122#define L2TP_SESSION_MAGIC 0x0C04EB7D
123
124#define PPPOL2TP_HASH_BITS 4
125#define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
126
127/* Default trace flags */
128#define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
129
130#define PRINTK(_mask, _type, _lvl, _fmt, args...) \
131 do { \
132 if ((_mask) & (_type)) \
133 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
134 } while(0)
135
136/* Number of bytes to build transmit L2TP headers.
137 * Unfortunately the size is different depending on whether sequence numbers
138 * are enabled.
139 */
140#define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
141#define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
142
143struct pppol2tp_tunnel;
144
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.
148 */
149struct pppol2tp_session
150{
151 int magic; /* should be
152 * L2TP_SESSION_MAGIC */
153 int owner; /* pid that opened the socket */
154
155 struct sock *sock; /* Pointer to the session
156 * PPPoX socket */
157 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
158 * socket */
159
160 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
161
162 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
163 * context */
164
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 */
187};
188
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
191 */
192struct pppol2tp_tunnel
193{
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;
203
204 void (*old_sk_destruct)(struct sock *);
205
206 struct sock *sock; /* Parent socket */
207 struct list_head list; /* Keep a list of all open
208 * prepared sockets */
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800209 struct net *pppol2tp_net; /* the net we belong to */
James Chapman3557baa2007-06-27 15:49:24 -0700210
211 atomic_t ref_count;
212};
213
214/* Private data stored for received packets in the skb.
215 */
216struct pppol2tp_skb_cb {
217 u16 ns;
218 u16 nr;
219 u16 has_seq;
220 u16 length;
221 unsigned long expires;
222};
223
224#define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
225
226static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
227static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel);
228
229static atomic_t pppol2tp_tunnel_count;
230static atomic_t pppol2tp_session_count;
231static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
232static struct proto_ops pppol2tp_ops;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800233
234/* per-net private data for this module */
Hannes Ederd6781f22009-02-14 11:13:52 +0000235static int pppol2tp_net_id;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800236struct pppol2tp_net {
237 struct list_head pppol2tp_tunnel_list;
238 rwlock_t pppol2tp_tunnel_list_lock;
239};
240
241static inline struct pppol2tp_net *pppol2tp_pernet(struct net *net)
242{
243 BUG_ON(!net);
244
245 return net_generic(net, pppol2tp_net_id);
246}
James Chapman3557baa2007-06-27 15:49:24 -0700247
248/* Helpers to obtain tunnel/session contexts from sockets.
249 */
250static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk)
251{
252 struct pppol2tp_session *session;
253
254 if (sk == NULL)
255 return NULL;
256
James Chapman24b95682008-06-04 15:54:07 -0700257 sock_hold(sk);
James Chapman3557baa2007-06-27 15:49:24 -0700258 session = (struct pppol2tp_session *)(sk->sk_user_data);
James Chapman24b95682008-06-04 15:54:07 -0700259 if (session == NULL) {
260 sock_put(sk);
261 goto out;
262 }
James Chapman3557baa2007-06-27 15:49:24 -0700263
264 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
James Chapman24b95682008-06-04 15:54:07 -0700265out:
James Chapman3557baa2007-06-27 15:49:24 -0700266 return session;
267}
268
269static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk)
270{
271 struct pppol2tp_tunnel *tunnel;
272
273 if (sk == NULL)
274 return NULL;
275
James Chapman24b95682008-06-04 15:54:07 -0700276 sock_hold(sk);
James Chapman3557baa2007-06-27 15:49:24 -0700277 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data);
James Chapman24b95682008-06-04 15:54:07 -0700278 if (tunnel == NULL) {
279 sock_put(sk);
280 goto out;
281 }
James Chapman3557baa2007-06-27 15:49:24 -0700282
283 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapman24b95682008-06-04 15:54:07 -0700284out:
James Chapman3557baa2007-06-27 15:49:24 -0700285 return tunnel;
286}
287
288/* Tunnel reference counts. Incremented per session that is added to
289 * the tunnel.
290 */
291static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel)
292{
293 atomic_inc(&tunnel->ref_count);
294}
295
296static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel)
297{
298 if (atomic_dec_and_test(&tunnel->ref_count))
299 pppol2tp_tunnel_free(tunnel);
300}
301
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.
307 */
308static inline struct hlist_head *
309pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id)
310{
311 unsigned long hash_val = (unsigned long) session_id;
312 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)];
313}
314
315/* Lookup a session by id
316 */
317static struct pppol2tp_session *
318pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id)
319{
320 struct hlist_head *session_list =
321 pppol2tp_session_id_hash(tunnel, session_id);
322 struct pppol2tp_session *session;
323 struct hlist_node *walk;
324
James Chapmancf3752e2008-03-05 18:39:08 -0800325 read_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -0700326 hlist_for_each_entry(session, walk, session_list, hlist) {
327 if (session->tunnel_addr.s_session == session_id) {
James Chapmancf3752e2008-03-05 18:39:08 -0800328 read_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -0700329 return session;
330 }
331 }
James Chapmancf3752e2008-03-05 18:39:08 -0800332 read_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -0700333
334 return NULL;
335}
336
337/* Lookup a tunnel by id
338 */
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800339static struct pppol2tp_tunnel *pppol2tp_tunnel_find(struct net *net, u16 tunnel_id)
James Chapman3557baa2007-06-27 15:49:24 -0700340{
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800341 struct pppol2tp_tunnel *tunnel;
342 struct pppol2tp_net *pn = pppol2tp_pernet(net);
James Chapman3557baa2007-06-27 15:49:24 -0700343
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800344 read_lock_bh(&pn->pppol2tp_tunnel_list_lock);
345 list_for_each_entry(tunnel, &pn->pppol2tp_tunnel_list, list) {
James Chapman3557baa2007-06-27 15:49:24 -0700346 if (tunnel->stats.tunnel_id == tunnel_id) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800347 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -0700348 return tunnel;
349 }
350 }
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800351 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -0700352
353 return NULL;
354}
355
356/*****************************************************************************
357 * Receive data handling
358 *****************************************************************************/
359
360/* Queue a skb in order. We come here only if the skb has an L2TP sequence
361 * number.
362 */
363static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
364{
365 struct sk_buff *skbp;
James Chapmane6531812008-03-05 18:40:01 -0800366 struct sk_buff *tmp;
James Chapman3557baa2007-06-27 15:49:24 -0700367 u16 ns = PPPOL2TP_SKB_CB(skb)->ns;
368
James Chapmancf3752e2008-03-05 18:39:08 -0800369 spin_lock_bh(&session->reorder_q.lock);
James Chapmane6531812008-03-05 18:40:01 -0800370 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
James Chapman3557baa2007-06-27 15:49:24 -0700371 if (PPPOL2TP_SKB_CB(skbp)->ns > ns) {
David S. Miller43f59c82008-09-21 21:28:51 -0700372 __skb_queue_before(&session->reorder_q, skbp, skb);
James Chapman3557baa2007-06-27 15:49:24 -0700373 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;
379 }
380 }
381
382 __skb_queue_tail(&session->reorder_q, skb);
383
384out:
James Chapmancf3752e2008-03-05 18:39:08 -0800385 spin_unlock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700386}
387
388/* Dequeue a single skb.
389 */
390static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
391{
392 struct pppol2tp_tunnel *tunnel = session->tunnel;
393 int length = PPPOL2TP_SKB_CB(skb)->length;
394 struct sock *session_sock = NULL;
395
James Chapmane6531812008-03-05 18:40:01 -0800396 /* We're about to requeue the skb, so return resources
James Chapman3557baa2007-06-27 15:49:24 -0700397 * to its current owner (a socket receive buffer).
398 */
James Chapman3557baa2007-06-27 15:49:24 -0700399 skb_orphan(skb);
400
401 tunnel->stats.rx_packets++;
402 tunnel->stats.rx_bytes += length;
403 session->stats.rx_packets++;
404 session->stats.rx_bytes += length;
405
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);
411 }
412
413 /* If the socket is bound, send it in to PPP's input queue. Otherwise
414 * queue it on the session socket.
415 */
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);
422
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
434 */
435 secpath_reset(skb);
436 dst_release(skb->dst);
437 skb->dst = NULL;
438 nf_reset(skb);
439
440 po = pppox_sk(session_sock);
441 ppp_input(&po->chan, skb);
442 } else {
443 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
444 "%s: socket not bound\n", session->name);
445
446 /* Not bound. Nothing we can do, so discard. */
447 session->stats.rx_errors++;
448 kfree_skb(skb);
449 }
450
451 sock_put(session->sock);
452}
453
454/* Dequeue skbs from the session's reorder_q, subject to packet order.
455 * Skbs that have been in the queue for too long are simply discarded.
456 */
457static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
458{
459 struct sk_buff *skb;
460 struct sk_buff *tmp;
461
462 /* If the pkt at the head of the queue has the nr that we
463 * expect to send up next, dequeue it and any other
464 * in-sequence packets behind it.
465 */
James Chapmancf3752e2008-03-05 18:39:08 -0800466 spin_lock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700467 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
468 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
469 session->stats.rx_seq_discards++;
470 session->stats.rx_errors++;
471 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
472 "%s: oos pkt %hu len %d discarded (too old), "
473 "waiting for %hu, reorder_q_len=%d\n",
474 session->name, PPPOL2TP_SKB_CB(skb)->ns,
475 PPPOL2TP_SKB_CB(skb)->length, session->nr,
476 skb_queue_len(&session->reorder_q));
477 __skb_unlink(skb, &session->reorder_q);
478 kfree_skb(skb);
Jarek Poplawskic8fff1c2008-03-03 20:48:53 -0800479 sock_put(session->sock);
James Chapman3557baa2007-06-27 15:49:24 -0700480 continue;
481 }
482
483 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
484 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
485 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
486 "%s: holding oos pkt %hu len %d, "
487 "waiting for %hu, reorder_q_len=%d\n",
488 session->name, PPPOL2TP_SKB_CB(skb)->ns,
489 PPPOL2TP_SKB_CB(skb)->length, session->nr,
490 skb_queue_len(&session->reorder_q));
491 goto out;
492 }
493 }
James Chapmane6531812008-03-05 18:40:01 -0800494 __skb_unlink(skb, &session->reorder_q);
495
496 /* Process the skb. We release the queue lock while we
497 * do so to let other contexts process the queue.
498 */
James Chapmancf3752e2008-03-05 18:39:08 -0800499 spin_unlock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700500 pppol2tp_recv_dequeue_skb(session, skb);
James Chapmancf3752e2008-03-05 18:39:08 -0800501 spin_lock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700502 }
503
504out:
James Chapmancf3752e2008-03-05 18:39:08 -0800505 spin_unlock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700506}
507
James Chapmanffcebb12008-12-16 01:23:49 -0800508static inline int pppol2tp_verify_udp_checksum(struct sock *sk,
509 struct sk_buff *skb)
510{
511 struct udphdr *uh = udp_hdr(skb);
512 u16 ulen = ntohs(uh->len);
513 struct inet_sock *inet;
514 __wsum psum;
515
516 if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
517 return 0;
518
519 inet = inet_sk(sk);
520 psum = csum_tcpudp_nofold(inet->saddr, inet->daddr, ulen,
521 IPPROTO_UDP, 0);
522
523 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
524 !csum_fold(csum_add(psum, skb->csum)))
525 return 0;
526
527 skb->csum = psum;
528
529 return __skb_checksum_complete(skb);
530}
531
James Chapman3557baa2007-06-27 15:49:24 -0700532/* Internal receive frame. Do the real work of receiving an L2TP data frame
533 * here. The skb is not on a list when we get here.
534 * Returns 0 if the packet was a data packet and was successfully passed on.
535 * Returns 1 if the packet was not a good data packet and could not be
536 * forwarded. All such packets are passed up to userspace to deal with.
537 */
538static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
539{
540 struct pppol2tp_session *session = NULL;
541 struct pppol2tp_tunnel *tunnel;
James Chapman91781002007-11-05 23:32:37 -0800542 unsigned char *ptr, *optr;
James Chapman3557baa2007-06-27 15:49:24 -0700543 u16 hdrflags;
544 u16 tunnel_id, session_id;
545 int length;
Herbert Xu7a70e392007-09-18 13:18:42 -0700546 int offset;
James Chapman3557baa2007-06-27 15:49:24 -0700547
548 tunnel = pppol2tp_sock_to_tunnel(sock);
549 if (tunnel == NULL)
James Chapman91781002007-11-05 23:32:37 -0800550 goto no_tunnel;
James Chapman3557baa2007-06-27 15:49:24 -0700551
James Chapmanffcebb12008-12-16 01:23:49 -0800552 if (tunnel->sock && pppol2tp_verify_udp_checksum(tunnel->sock, skb))
553 goto discard_bad_csum;
554
Herbert Xu7a70e392007-09-18 13:18:42 -0700555 /* UDP always verifies the packet length. */
556 __skb_pull(skb, sizeof(struct udphdr));
557
James Chapman3557baa2007-06-27 15:49:24 -0700558 /* Short packet? */
Herbert Xu7a70e392007-09-18 13:18:42 -0700559 if (!pskb_may_pull(skb, 12)) {
James Chapman3557baa2007-06-27 15:49:24 -0700560 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
561 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
562 goto error;
563 }
564
565 /* Point to L2TP header */
James Chapman91781002007-11-05 23:32:37 -0800566 optr = ptr = skb->data;
James Chapman3557baa2007-06-27 15:49:24 -0700567
568 /* Get L2TP header flags */
569 hdrflags = ntohs(*(__be16*)ptr);
570
571 /* Trace packet contents, if enabled */
572 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
Herbert Xu7a70e392007-09-18 13:18:42 -0700573 length = min(16u, skb->len);
574 if (!pskb_may_pull(skb, length))
575 goto error;
576
James Chapman3557baa2007-06-27 15:49:24 -0700577 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
578
Herbert Xu7a70e392007-09-18 13:18:42 -0700579 offset = 0;
580 do {
581 printk(" %02X", ptr[offset]);
582 } while (++offset < length);
583
James Chapman3557baa2007-06-27 15:49:24 -0700584 printk("\n");
585 }
586
587 /* Get length of L2TP packet */
Herbert Xu7a70e392007-09-18 13:18:42 -0700588 length = skb->len;
James Chapman3557baa2007-06-27 15:49:24 -0700589
590 /* If type is control packet, it is handled by userspace. */
591 if (hdrflags & L2TP_HDRFLAG_T) {
592 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
593 "%s: recv control packet, len=%d\n", tunnel->name, length);
594 goto error;
595 }
596
597 /* Skip flags */
598 ptr += 2;
599
600 /* If length is present, skip it */
601 if (hdrflags & L2TP_HDRFLAG_L)
602 ptr += 2;
603
604 /* Extract tunnel and session ID */
605 tunnel_id = ntohs(*(__be16 *) ptr);
606 ptr += 2;
607 session_id = ntohs(*(__be16 *) ptr);
608 ptr += 2;
609
610 /* Find the session context */
611 session = pppol2tp_session_find(tunnel, session_id);
612 if (!session) {
613 /* Not found? Pass to userspace to deal with */
614 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
615 "%s: no socket found (%hu/%hu). Passing up.\n",
616 tunnel->name, tunnel_id, session_id);
617 goto error;
618 }
619 sock_hold(session->sock);
620
621 /* The ref count on the socket was increased by the above call since
622 * we now hold a pointer to the session. Take care to do sock_put()
623 * when exiting this function from now on...
624 */
625
626 /* Handle the optional sequence numbers. If we are the LAC,
627 * enable/disable sequence numbers under the control of the LNS. If
628 * no sequence numbers present but we were expecting them, discard
629 * frame.
630 */
631 if (hdrflags & L2TP_HDRFLAG_S) {
632 u16 ns, nr;
633 ns = ntohs(*(__be16 *) ptr);
634 ptr += 2;
635 nr = ntohs(*(__be16 *) ptr);
636 ptr += 2;
637
638 /* Received a packet with sequence numbers. If we're the LNS,
639 * check if we sre sending sequence numbers and if not,
640 * configure it so.
641 */
642 if ((!session->lns_mode) && (!session->send_seq)) {
643 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
644 "%s: requested to enable seq numbers by LNS\n",
645 session->name);
646 session->send_seq = -1;
647 }
648
649 /* Store L2TP info in the skb */
650 PPPOL2TP_SKB_CB(skb)->ns = ns;
651 PPPOL2TP_SKB_CB(skb)->nr = nr;
652 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
653
654 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
655 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
656 session->name, ns, nr, session->nr);
657 } else {
658 /* No sequence numbers.
659 * If user has configured mandatory sequence numbers, discard.
660 */
661 if (session->recv_seq) {
662 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
663 "%s: recv data has no seq numbers when required. "
664 "Discarding\n", session->name);
665 session->stats.rx_seq_discards++;
James Chapman3557baa2007-06-27 15:49:24 -0700666 goto discard;
667 }
668
669 /* If we're the LAC and we're sending sequence numbers, the
670 * LNS has requested that we no longer send sequence numbers.
671 * If we're the LNS and we're sending sequence numbers, the
672 * LAC is broken. Discard the frame.
673 */
674 if ((!session->lns_mode) && (session->send_seq)) {
675 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
676 "%s: requested to disable seq numbers by LNS\n",
677 session->name);
678 session->send_seq = 0;
679 } else if (session->send_seq) {
680 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
681 "%s: recv data has no seq numbers when required. "
682 "Discarding\n", session->name);
683 session->stats.rx_seq_discards++;
James Chapman3557baa2007-06-27 15:49:24 -0700684 goto discard;
685 }
686
687 /* Store L2TP info in the skb */
688 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
689 }
690
691 /* If offset bit set, skip it. */
Herbert Xu7a70e392007-09-18 13:18:42 -0700692 if (hdrflags & L2TP_HDRFLAG_O) {
693 offset = ntohs(*(__be16 *)ptr);
James Chapman91781002007-11-05 23:32:37 -0800694 ptr += 2 + offset;
Herbert Xu7a70e392007-09-18 13:18:42 -0700695 }
James Chapman3557baa2007-06-27 15:49:24 -0700696
James Chapman91781002007-11-05 23:32:37 -0800697 offset = ptr - optr;
698 if (!pskb_may_pull(skb, offset))
699 goto discard;
700
701 __skb_pull(skb, offset);
James Chapman3557baa2007-06-27 15:49:24 -0700702
703 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
704 * don't send the PPP header (PPP header compression enabled), but
705 * other clients can include the header. So we cope with both cases
706 * here. The PPP header is always FF03 when using L2TP.
707 *
708 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
709 * the field may be unaligned.
710 */
James Chapman91781002007-11-05 23:32:37 -0800711 if (!pskb_may_pull(skb, 2))
712 goto discard;
713
James Chapman3557baa2007-06-27 15:49:24 -0700714 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
715 skb_pull(skb, 2);
716
717 /* Prepare skb for adding to the session's reorder_q. Hold
718 * packets for max reorder_timeout or 1 second if not
719 * reordering.
720 */
721 PPPOL2TP_SKB_CB(skb)->length = length;
722 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
723 (session->reorder_timeout ? session->reorder_timeout : HZ);
724
725 /* Add packet to the session's receive queue. Reordering is done here, if
726 * enabled. Saved L2TP protocol info is stored in skb->sb[].
727 */
728 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
729 if (session->reorder_timeout != 0) {
730 /* Packet reordering enabled. Add skb to session's
731 * reorder queue, in order of ns.
732 */
733 pppol2tp_recv_queue_skb(session, skb);
734 } else {
735 /* Packet reordering disabled. Discard out-of-sequence
736 * packets
737 */
738 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
739 session->stats.rx_seq_discards++;
James Chapman3557baa2007-06-27 15:49:24 -0700740 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
741 "%s: oos pkt %hu len %d discarded, "
742 "waiting for %hu, reorder_q_len=%d\n",
743 session->name, PPPOL2TP_SKB_CB(skb)->ns,
744 PPPOL2TP_SKB_CB(skb)->length, session->nr,
745 skb_queue_len(&session->reorder_q));
746 goto discard;
747 }
748 skb_queue_tail(&session->reorder_q, skb);
749 }
750 } else {
751 /* No sequence numbers. Add the skb to the tail of the
752 * reorder queue. This ensures that it will be
753 * delivered after all previous sequenced skbs.
754 */
755 skb_queue_tail(&session->reorder_q, skb);
756 }
757
758 /* Try to dequeue as many skbs from reorder_q as we can. */
759 pppol2tp_recv_dequeue(session);
760
761 return 0;
762
763discard:
Herbert Xu7a70e392007-09-18 13:18:42 -0700764 session->stats.rx_errors++;
James Chapman3557baa2007-06-27 15:49:24 -0700765 kfree_skb(skb);
766 sock_put(session->sock);
James Chapman24b95682008-06-04 15:54:07 -0700767 sock_put(sock);
James Chapman3557baa2007-06-27 15:49:24 -0700768
769 return 0;
770
James Chapmanffcebb12008-12-16 01:23:49 -0800771discard_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
777 return 0;
778
James Chapman3557baa2007-06-27 15:49:24 -0700779error:
James Chapman91781002007-11-05 23:32:37 -0800780 /* Put UDP header back */
781 __skb_push(skb, sizeof(struct udphdr));
James Chapman24b95682008-06-04 15:54:07 -0700782 sock_put(sock);
James Chapman91781002007-11-05 23:32:37 -0800783
784no_tunnel:
James Chapman3557baa2007-06-27 15:49:24 -0700785 return 1;
786}
787
788/* UDP encapsulation receive handler. See net/ipv4/udp.c.
789 * Return codes:
790 * 0 : success.
791 * <0: error
792 * >0: skb should be passed up to userspace as UDP.
793 */
794static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
795{
796 struct pppol2tp_tunnel *tunnel;
797
798 tunnel = pppol2tp_sock_to_tunnel(sk);
799 if (tunnel == NULL)
800 goto pass_up;
801
802 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
803 "%s: received %d bytes\n", tunnel->name, skb->len);
804
805 if (pppol2tp_recv_core(sk, skb))
James Chapman24b95682008-06-04 15:54:07 -0700806 goto pass_up_put;
James Chapman3557baa2007-06-27 15:49:24 -0700807
James Chapman24b95682008-06-04 15:54:07 -0700808 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -0700809 return 0;
810
James Chapman24b95682008-06-04 15:54:07 -0700811pass_up_put:
812 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -0700813pass_up:
814 return 1;
815}
816
817/* Receive message. This is the recvmsg for the PPPoL2TP socket.
818 */
819static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
820 struct msghdr *msg, size_t len,
821 int flags)
822{
823 int err;
824 struct sk_buff *skb;
825 struct sock *sk = sock->sk;
826
827 err = -EIO;
828 if (sk->sk_state & PPPOX_BOUND)
829 goto end;
830
831 msg->msg_namelen = 0;
832
833 err = 0;
834 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
835 flags & MSG_DONTWAIT, &err);
James Chapman6b6707a2008-06-10 12:35:00 -0700836 if (!skb)
837 goto end;
838
839 if (len > skb->len)
840 len = skb->len;
841 else if (len < skb->len)
842 msg->msg_flags |= MSG_TRUNC;
843
844 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
845 if (likely(err == 0))
846 err = len;
847
James Chapman3557baa2007-06-27 15:49:24 -0700848 kfree_skb(skb);
849end:
850 return err;
851}
852
853/************************************************************************
854 * Transmit handling
855 ***********************************************************************/
856
857/* Tell how big L2TP headers are for a particular session. This
858 * depends on whether sequence numbers are being used.
859 */
860static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
861{
862 if (session->send_seq)
863 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
864
865 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
866}
867
868/* Build an L2TP header for the session into the buffer provided.
869 */
870static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
871 void *buf)
872{
873 __be16 *bufp = buf;
874 u16 flags = L2TP_HDR_VER;
875
876 if (session->send_seq)
877 flags |= L2TP_HDRFLAG_S;
878
879 /* Setup L2TP header.
880 * FIXME: Can this ever be unaligned? Is direct dereferencing of
881 * 16-bit header fields safe here for all architectures?
882 */
883 *bufp++ = htons(flags);
884 *bufp++ = htons(session->tunnel_addr.d_tunnel);
885 *bufp++ = htons(session->tunnel_addr.d_session);
886 if (session->send_seq) {
887 *bufp++ = htons(session->ns);
888 *bufp++ = 0;
889 session->ns++;
890 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
891 "%s: updated ns to %hu\n", session->name, session->ns);
892 }
893}
894
895/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
896 * when a user application does a sendmsg() on the session socket. L2TP and
897 * PPP headers must be inserted into the user's data.
898 */
899static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
900 size_t total_len)
901{
902 static const unsigned char ppph[2] = { 0xff, 0x03 };
903 struct sock *sk = sock->sk;
904 struct inet_sock *inet;
James Chapmanffcebb12008-12-16 01:23:49 -0800905 __wsum csum;
James Chapman3557baa2007-06-27 15:49:24 -0700906 struct sk_buff *skb;
907 int error;
908 int hdr_len;
909 struct pppol2tp_session *session;
910 struct pppol2tp_tunnel *tunnel;
911 struct udphdr *uh;
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700912 unsigned int len;
James Chapmanffcebb12008-12-16 01:23:49 -0800913 struct sock *sk_tun;
914 u16 udp_len;
James Chapman3557baa2007-06-27 15:49:24 -0700915
916 error = -ENOTCONN;
917 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
918 goto error;
919
920 /* Get session and tunnel contexts */
921 error = -EBADF;
922 session = pppol2tp_sock_to_session(sk);
923 if (session == NULL)
924 goto error;
925
James Chapmanffcebb12008-12-16 01:23:49 -0800926 sk_tun = session->tunnel_sock;
927 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
James Chapman3557baa2007-06-27 15:49:24 -0700928 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -0700929 goto error_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -0700930
931 /* What header length is configured for this session? */
932 hdr_len = pppol2tp_l2tp_header_len(session);
933
934 /* Allocate a socket buffer */
935 error = -ENOMEM;
936 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
937 sizeof(struct udphdr) + hdr_len +
938 sizeof(ppph) + total_len,
939 0, GFP_KERNEL);
940 if (!skb)
James Chapman24b95682008-06-04 15:54:07 -0700941 goto error_put_sess_tun;
James Chapman3557baa2007-06-27 15:49:24 -0700942
943 /* Reserve space for headers. */
944 skb_reserve(skb, NET_SKB_PAD);
945 skb_reset_network_header(skb);
946 skb_reserve(skb, sizeof(struct iphdr));
947 skb_reset_transport_header(skb);
948
949 /* Build UDP header */
James Chapmanffcebb12008-12-16 01:23:49 -0800950 inet = inet_sk(sk_tun);
951 udp_len = hdr_len + sizeof(ppph) + total_len;
James Chapman3557baa2007-06-27 15:49:24 -0700952 uh = (struct udphdr *) skb->data;
953 uh->source = inet->sport;
954 uh->dest = inet->dport;
James Chapmanffcebb12008-12-16 01:23:49 -0800955 uh->len = htons(udp_len);
James Chapman3557baa2007-06-27 15:49:24 -0700956 uh->check = 0;
957 skb_put(skb, sizeof(struct udphdr));
958
959 /* Build L2TP header */
960 pppol2tp_build_l2tp_header(session, skb->data);
961 skb_put(skb, hdr_len);
962
963 /* Add PPP header */
964 skb->data[0] = ppph[0];
965 skb->data[1] = ppph[1];
966 skb_put(skb, 2);
967
968 /* Copy user data into skb */
969 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
970 if (error < 0) {
971 kfree_skb(skb);
James Chapman24b95682008-06-04 15:54:07 -0700972 goto error_put_sess_tun;
James Chapman3557baa2007-06-27 15:49:24 -0700973 }
974 skb_put(skb, total_len);
975
976 /* Calculate UDP checksum if configured to do so */
James Chapmanffcebb12008-12-16 01:23:49 -0800977 if (sk_tun->sk_no_check == UDP_CSUM_NOXMIT)
978 skb->ip_summed = CHECKSUM_NONE;
979 else if (!(skb->dst->dev->features & NETIF_F_V4_CSUM)) {
980 skb->ip_summed = CHECKSUM_COMPLETE;
981 csum = skb_checksum(skb, 0, udp_len, 0);
982 uh->check = csum_tcpudp_magic(inet->saddr, inet->daddr,
983 udp_len, IPPROTO_UDP, csum);
984 if (uh->check == 0)
985 uh->check = CSUM_MANGLED_0;
986 } else {
987 skb->ip_summed = CHECKSUM_PARTIAL;
988 skb->csum_start = skb_transport_header(skb) - skb->head;
989 skb->csum_offset = offsetof(struct udphdr, check);
990 uh->check = ~csum_tcpudp_magic(inet->saddr, inet->daddr,
991 udp_len, IPPROTO_UDP, 0);
992 }
James Chapman3557baa2007-06-27 15:49:24 -0700993
994 /* Debug */
995 if (session->send_seq)
996 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
David S. Miller38d15b62007-06-27 15:52:25 -0700997 "%s: send %Zd bytes, ns=%hu\n", session->name,
James Chapman3557baa2007-06-27 15:49:24 -0700998 total_len, session->ns - 1);
999 else
1000 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
David S. Miller38d15b62007-06-27 15:52:25 -07001001 "%s: send %Zd bytes\n", session->name, total_len);
James Chapman3557baa2007-06-27 15:49:24 -07001002
1003 if (session->debug & PPPOL2TP_MSG_DATA) {
1004 int i;
1005 unsigned char *datap = skb->data;
1006
1007 printk(KERN_DEBUG "%s: xmit:", session->name);
1008 for (i = 0; i < total_len; i++) {
1009 printk(" %02X", *datap++);
1010 if (i == 15) {
1011 printk(" ...");
1012 break;
1013 }
1014 }
1015 printk("\n");
1016 }
1017
1018 /* Queue the packet to IP for output */
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001019 len = skb->len;
James Chapman3557baa2007-06-27 15:49:24 -07001020 error = ip_queue_xmit(skb, 1);
1021
1022 /* Update stats */
1023 if (error >= 0) {
1024 tunnel->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001025 tunnel->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001026 session->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001027 session->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001028 } else {
1029 tunnel->stats.tx_errors++;
1030 session->stats.tx_errors++;
1031 }
1032
James Chapman24b95682008-06-04 15:54:07 -07001033 return error;
1034
1035error_put_sess_tun:
1036 sock_put(session->tunnel_sock);
1037error_put_sess:
1038 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07001039error:
1040 return error;
1041}
1042
James Chapman24b95682008-06-04 15:54:07 -07001043/* Automatically called when the skb is freed.
1044 */
1045static void pppol2tp_sock_wfree(struct sk_buff *skb)
1046{
1047 sock_put(skb->sk);
1048}
1049
1050/* For data skbs that we transmit, we associate with the tunnel socket
1051 * but don't do accounting.
1052 */
1053static inline void pppol2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1054{
1055 sock_hold(sk);
1056 skb->sk = sk;
1057 skb->destructor = pppol2tp_sock_wfree;
1058}
1059
James Chapman3557baa2007-06-27 15:49:24 -07001060/* Transmit function called by generic PPP driver. Sends PPP frame
1061 * over PPPoL2TP socket.
1062 *
1063 * This is almost the same as pppol2tp_sendmsg(), but rather than
1064 * being called with a msghdr from userspace, it is called with a skb
1065 * from the kernel.
1066 *
1067 * The supplied skb from ppp doesn't have enough headroom for the
1068 * insertion of L2TP, UDP and IP headers so we need to allocate more
1069 * headroom in the skb. This will create a cloned skb. But we must be
1070 * careful in the error case because the caller will expect to free
1071 * the skb it supplied, not our cloned skb. So we take care to always
1072 * leave the original skb unfreed if we return an error.
1073 */
1074static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
1075{
1076 static const u8 ppph[2] = { 0xff, 0x03 };
1077 struct sock *sk = (struct sock *) chan->private;
1078 struct sock *sk_tun;
1079 int hdr_len;
James Chapmanffcebb12008-12-16 01:23:49 -08001080 u16 udp_len;
James Chapman3557baa2007-06-27 15:49:24 -07001081 struct pppol2tp_session *session;
1082 struct pppol2tp_tunnel *tunnel;
1083 int rc;
1084 int headroom;
1085 int data_len = skb->len;
1086 struct inet_sock *inet;
James Chapmanffcebb12008-12-16 01:23:49 -08001087 __wsum csum;
James Chapman3557baa2007-06-27 15:49:24 -07001088 struct udphdr *uh;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001089 unsigned int len;
James Chapman090c48d2008-05-19 14:10:01 -07001090 int old_headroom;
1091 int new_headroom;
James Chapman3557baa2007-06-27 15:49:24 -07001092
1093 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
1094 goto abort;
1095
1096 /* Get session and tunnel contexts from the socket */
1097 session = pppol2tp_sock_to_session(sk);
1098 if (session == NULL)
1099 goto abort;
1100
1101 sk_tun = session->tunnel_sock;
1102 if (sk_tun == NULL)
James Chapman24b95682008-06-04 15:54:07 -07001103 goto abort_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07001104 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
1105 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07001106 goto abort_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07001107
1108 /* What header length is configured for this session? */
1109 hdr_len = pppol2tp_l2tp_header_len(session);
1110
1111 /* Check that there's enough headroom in the skb to insert IP,
1112 * UDP and L2TP and PPP headers. If not enough, expand it to
James Chapman090c48d2008-05-19 14:10:01 -07001113 * make room. Adjust truesize.
James Chapman3557baa2007-06-27 15:49:24 -07001114 */
1115 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1116 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
James Chapman090c48d2008-05-19 14:10:01 -07001117 old_headroom = skb_headroom(skb);
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001118 if (skb_cow_head(skb, headroom))
James Chapman24b95682008-06-04 15:54:07 -07001119 goto abort_put_sess_tun;
James Chapman3557baa2007-06-27 15:49:24 -07001120
James Chapman090c48d2008-05-19 14:10:01 -07001121 new_headroom = skb_headroom(skb);
1122 skb_orphan(skb);
1123 skb->truesize += new_headroom - old_headroom;
1124
James Chapman3557baa2007-06-27 15:49:24 -07001125 /* Setup PPP header */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001126 __skb_push(skb, sizeof(ppph));
1127 skb->data[0] = ppph[0];
1128 skb->data[1] = ppph[1];
James Chapman3557baa2007-06-27 15:49:24 -07001129
1130 /* Setup L2TP header */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001131 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
James Chapman3557baa2007-06-27 15:49:24 -07001132
James Chapmanffcebb12008-12-16 01:23:49 -08001133 udp_len = sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len;
1134
James Chapman3557baa2007-06-27 15:49:24 -07001135 /* Setup UDP header */
1136 inet = inet_sk(sk_tun);
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001137 __skb_push(skb, sizeof(*uh));
1138 skb_reset_transport_header(skb);
1139 uh = udp_hdr(skb);
James Chapman3557baa2007-06-27 15:49:24 -07001140 uh->source = inet->sport;
1141 uh->dest = inet->dport;
James Chapmanffcebb12008-12-16 01:23:49 -08001142 uh->len = htons(udp_len);
James Chapman3557baa2007-06-27 15:49:24 -07001143 uh->check = 0;
1144
James Chapman3557baa2007-06-27 15:49:24 -07001145 /* Debug */
1146 if (session->send_seq)
1147 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1148 "%s: send %d bytes, ns=%hu\n", session->name,
1149 data_len, session->ns - 1);
1150 else
1151 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1152 "%s: send %d bytes\n", session->name, data_len);
1153
1154 if (session->debug & PPPOL2TP_MSG_DATA) {
1155 int i;
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001156 unsigned char *datap = skb->data;
James Chapman3557baa2007-06-27 15:49:24 -07001157
1158 printk(KERN_DEBUG "%s: xmit:", session->name);
1159 for (i = 0; i < data_len; i++) {
1160 printk(" %02X", *datap++);
1161 if (i == 31) {
1162 printk(" ...");
1163 break;
1164 }
1165 }
1166 printk("\n");
1167 }
1168
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001169 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1170 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1171 IPSKB_REROUTED);
1172 nf_reset(skb);
Patrick McHardyf77ae932007-07-18 02:04:39 -07001173
James Chapman3557baa2007-06-27 15:49:24 -07001174 /* Get routing info from the tunnel socket */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001175 dst_release(skb->dst);
James Chapmancf3752e2008-03-05 18:39:08 -08001176 skb->dst = dst_clone(__sk_dst_get(sk_tun));
James Chapman24b95682008-06-04 15:54:07 -07001177 pppol2tp_skb_set_owner_w(skb, sk_tun);
James Chapman3557baa2007-06-27 15:49:24 -07001178
James Chapmanffcebb12008-12-16 01:23:49 -08001179 /* Calculate UDP checksum if configured to do so */
1180 if (sk_tun->sk_no_check == UDP_CSUM_NOXMIT)
1181 skb->ip_summed = CHECKSUM_NONE;
1182 else if (!(skb->dst->dev->features & NETIF_F_V4_CSUM)) {
1183 skb->ip_summed = CHECKSUM_COMPLETE;
1184 csum = skb_checksum(skb, 0, udp_len, 0);
1185 uh->check = csum_tcpudp_magic(inet->saddr, inet->daddr,
1186 udp_len, IPPROTO_UDP, csum);
1187 if (uh->check == 0)
1188 uh->check = CSUM_MANGLED_0;
1189 } else {
1190 skb->ip_summed = CHECKSUM_PARTIAL;
1191 skb->csum_start = skb_transport_header(skb) - skb->head;
1192 skb->csum_offset = offsetof(struct udphdr, check);
1193 uh->check = ~csum_tcpudp_magic(inet->saddr, inet->daddr,
1194 udp_len, IPPROTO_UDP, 0);
1195 }
1196
James Chapman3557baa2007-06-27 15:49:24 -07001197 /* Queue the packet to IP for output */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001198 len = skb->len;
1199 rc = ip_queue_xmit(skb, 1);
James Chapman3557baa2007-06-27 15:49:24 -07001200
1201 /* Update stats */
1202 if (rc >= 0) {
1203 tunnel->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001204 tunnel->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001205 session->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001206 session->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001207 } else {
1208 tunnel->stats.tx_errors++;
1209 session->stats.tx_errors++;
1210 }
1211
James Chapman24b95682008-06-04 15:54:07 -07001212 sock_put(sk_tun);
1213 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07001214 return 1;
1215
James Chapman24b95682008-06-04 15:54:07 -07001216abort_put_sess_tun:
1217 sock_put(sk_tun);
1218abort_put_sess:
1219 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07001220abort:
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001221 /* Free the original skb */
1222 kfree_skb(skb);
1223 return 1;
James Chapman3557baa2007-06-27 15:49:24 -07001224}
1225
1226/*****************************************************************************
1227 * Session (and tunnel control) socket create/destroy.
1228 *****************************************************************************/
1229
1230/* When the tunnel UDP socket is closed, all the attached sockets need to go
1231 * too.
1232 */
1233static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1234{
1235 int hash;
1236 struct hlist_node *walk;
1237 struct hlist_node *tmp;
1238 struct pppol2tp_session *session;
1239 struct sock *sk;
1240
Alexander Beregalov0ee904c2009-04-11 14:50:23 +00001241 BUG_ON(tunnel == NULL);
James Chapman3557baa2007-06-27 15:49:24 -07001242
1243 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1244 "%s: closing all sessions...\n", tunnel->name);
1245
James Chapmancf3752e2008-03-05 18:39:08 -08001246 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001247 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1248again:
1249 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
Jarek Poplawskiec9b6ad2008-03-03 20:49:34 -08001250 struct sk_buff *skb;
1251
James Chapman3557baa2007-06-27 15:49:24 -07001252 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1253
1254 sk = session->sock;
1255
1256 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1257 "%s: closing session\n", session->name);
1258
1259 hlist_del_init(&session->hlist);
1260
1261 /* Since we should hold the sock lock while
1262 * doing any unbinding, we need to release the
1263 * lock we're holding before taking that lock.
1264 * Hold a reference to the sock so it doesn't
1265 * disappear as we're jumping between locks.
1266 */
1267 sock_hold(sk);
James Chapmancf3752e2008-03-05 18:39:08 -08001268 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001269 lock_sock(sk);
1270
1271 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1272 pppox_unbind_sock(sk);
1273 sk->sk_state = PPPOX_DEAD;
1274 sk->sk_state_change(sk);
1275 }
1276
1277 /* Purge any queued data */
1278 skb_queue_purge(&sk->sk_receive_queue);
1279 skb_queue_purge(&sk->sk_write_queue);
Jarek Poplawskiec9b6ad2008-03-03 20:49:34 -08001280 while ((skb = skb_dequeue(&session->reorder_q))) {
1281 kfree_skb(skb);
1282 sock_put(sk);
1283 }
James Chapman3557baa2007-06-27 15:49:24 -07001284
1285 release_sock(sk);
1286 sock_put(sk);
1287
1288 /* Now restart from the beginning of this hash
1289 * chain. We always remove a session from the
1290 * list so we are guaranteed to make forward
1291 * progress.
1292 */
James Chapmancf3752e2008-03-05 18:39:08 -08001293 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001294 goto again;
1295 }
1296 }
James Chapmancf3752e2008-03-05 18:39:08 -08001297 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001298}
1299
1300/* Really kill the tunnel.
1301 * Come here only when all sessions have been cleared from the tunnel.
1302 */
1303static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1304{
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001305 struct pppol2tp_net *pn = pppol2tp_pernet(tunnel->pppol2tp_net);
1306
James Chapman3557baa2007-06-27 15:49:24 -07001307 /* Remove from socket list */
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001308 write_lock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001309 list_del_init(&tunnel->list);
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001310 write_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001311
1312 atomic_dec(&pppol2tp_tunnel_count);
1313 kfree(tunnel);
1314}
1315
1316/* Tunnel UDP socket destruct hook.
1317 * The tunnel context is deleted only when all session sockets have been
1318 * closed.
1319 */
1320static void pppol2tp_tunnel_destruct(struct sock *sk)
1321{
1322 struct pppol2tp_tunnel *tunnel;
1323
James Chapman24b95682008-06-04 15:54:07 -07001324 tunnel = sk->sk_user_data;
James Chapman3557baa2007-06-27 15:49:24 -07001325 if (tunnel == NULL)
1326 goto end;
1327
1328 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1329 "%s: closing...\n", tunnel->name);
1330
1331 /* Close all sessions */
1332 pppol2tp_tunnel_closeall(tunnel);
1333
1334 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1335 (udp_sk(sk))->encap_type = 0;
1336 (udp_sk(sk))->encap_rcv = NULL;
1337
1338 /* Remove hooks into tunnel socket */
1339 tunnel->sock = NULL;
1340 sk->sk_destruct = tunnel->old_sk_destruct;
1341 sk->sk_user_data = NULL;
1342
1343 /* Call original (UDP) socket descructor */
1344 if (sk->sk_destruct != NULL)
1345 (*sk->sk_destruct)(sk);
1346
1347 pppol2tp_tunnel_dec_refcount(tunnel);
1348
1349end:
1350 return;
1351}
1352
1353/* Really kill the session socket. (Called from sock_put() if
1354 * refcnt == 0.)
1355 */
1356static void pppol2tp_session_destruct(struct sock *sk)
1357{
1358 struct pppol2tp_session *session = NULL;
1359
1360 if (sk->sk_user_data != NULL) {
1361 struct pppol2tp_tunnel *tunnel;
1362
James Chapman24b95682008-06-04 15:54:07 -07001363 session = sk->sk_user_data;
James Chapman3557baa2007-06-27 15:49:24 -07001364 if (session == NULL)
1365 goto out;
1366
James Chapman24b95682008-06-04 15:54:07 -07001367 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
1368
James Chapman3557baa2007-06-27 15:49:24 -07001369 /* Don't use pppol2tp_sock_to_tunnel() here to
1370 * get the tunnel context because the tunnel
1371 * socket might have already been closed (its
1372 * sk->sk_user_data will be NULL) so use the
1373 * session's private tunnel ptr instead.
1374 */
1375 tunnel = session->tunnel;
1376 if (tunnel != NULL) {
1377 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1378
1379 /* If session_id is zero, this is a null
1380 * session context, which was created for a
1381 * socket that is being used only to manage
1382 * tunnels.
1383 */
1384 if (session->tunnel_addr.s_session != 0) {
1385 /* Delete the session socket from the
1386 * hash
1387 */
James Chapmancf3752e2008-03-05 18:39:08 -08001388 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001389 hlist_del_init(&session->hlist);
James Chapmancf3752e2008-03-05 18:39:08 -08001390 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001391
1392 atomic_dec(&pppol2tp_session_count);
1393 }
1394
1395 /* This will delete the tunnel context if this
1396 * is the last session on the tunnel.
1397 */
1398 session->tunnel = NULL;
1399 session->tunnel_sock = NULL;
1400 pppol2tp_tunnel_dec_refcount(tunnel);
1401 }
1402 }
1403
1404 kfree(session);
1405out:
1406 return;
1407}
1408
1409/* Called when the PPPoX socket (session) is closed.
1410 */
1411static int pppol2tp_release(struct socket *sock)
1412{
1413 struct sock *sk = sock->sk;
James Chapman199f7d22008-06-04 15:07:32 -07001414 struct pppol2tp_session *session;
James Chapman3557baa2007-06-27 15:49:24 -07001415 int error;
1416
1417 if (!sk)
1418 return 0;
1419
1420 error = -EBADF;
1421 lock_sock(sk);
1422 if (sock_flag(sk, SOCK_DEAD) != 0)
1423 goto error;
1424
1425 pppox_unbind_sock(sk);
1426
1427 /* Signal the death of the socket. */
1428 sk->sk_state = PPPOX_DEAD;
1429 sock_orphan(sk);
1430 sock->sk = NULL;
1431
James Chapman199f7d22008-06-04 15:07:32 -07001432 session = pppol2tp_sock_to_session(sk);
1433
James Chapman3557baa2007-06-27 15:49:24 -07001434 /* Purge any queued data */
1435 skb_queue_purge(&sk->sk_receive_queue);
1436 skb_queue_purge(&sk->sk_write_queue);
James Chapman199f7d22008-06-04 15:07:32 -07001437 if (session != NULL) {
1438 struct sk_buff *skb;
1439 while ((skb = skb_dequeue(&session->reorder_q))) {
1440 kfree_skb(skb);
1441 sock_put(sk);
1442 }
Frédéric Moulinse6358132008-11-28 22:12:02 -08001443 sock_put(sk);
James Chapman199f7d22008-06-04 15:07:32 -07001444 }
James Chapman3557baa2007-06-27 15:49:24 -07001445
1446 release_sock(sk);
1447
1448 /* This will delete the session context via
1449 * pppol2tp_session_destruct() if the socket's refcnt drops to
1450 * zero.
1451 */
1452 sock_put(sk);
1453
1454 return 0;
1455
1456error:
1457 release_sock(sk);
1458 return error;
1459}
1460
1461/* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1462 * sockets attached to it.
1463 */
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001464static struct sock *pppol2tp_prepare_tunnel_socket(struct net *net,
1465 int fd, u16 tunnel_id, int *error)
James Chapman3557baa2007-06-27 15:49:24 -07001466{
1467 int err;
1468 struct socket *sock = NULL;
1469 struct sock *sk;
1470 struct pppol2tp_tunnel *tunnel;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001471 struct pppol2tp_net *pn;
James Chapman3557baa2007-06-27 15:49:24 -07001472 struct sock *ret = NULL;
1473
1474 /* Get the tunnel UDP socket from the fd, which was opened by
1475 * the userspace L2TP daemon.
1476 */
1477 err = -EBADF;
1478 sock = sockfd_lookup(fd, &err);
1479 if (!sock) {
1480 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1481 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1482 tunnel_id, fd, err);
1483 goto err;
1484 }
1485
Herbert Xua14d6ab2007-09-18 13:18:17 -07001486 sk = sock->sk;
1487
James Chapman3557baa2007-06-27 15:49:24 -07001488 /* Quick sanity checks */
Herbert Xua14d6ab2007-09-18 13:18:17 -07001489 err = -EPROTONOSUPPORT;
1490 if (sk->sk_protocol != IPPROTO_UDP) {
James Chapman3557baa2007-06-27 15:49:24 -07001491 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
Herbert Xua14d6ab2007-09-18 13:18:17 -07001492 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1493 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
James Chapman3557baa2007-06-27 15:49:24 -07001494 goto err;
1495 }
1496 err = -EAFNOSUPPORT;
1497 if (sock->ops->family != AF_INET) {
1498 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1499 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1500 tunnel_id, fd, sock->ops->family, AF_INET);
1501 goto err;
1502 }
1503
1504 err = -ENOTCONN;
James Chapman3557baa2007-06-27 15:49:24 -07001505
1506 /* Check if this socket has already been prepped */
1507 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1508 if (tunnel != NULL) {
1509 /* User-data field already set */
1510 err = -EBUSY;
1511 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1512
1513 /* This socket has already been prepped */
1514 ret = tunnel->sock;
1515 goto out;
1516 }
1517
1518 /* This socket is available and needs prepping. Create a new tunnel
1519 * context and init it.
1520 */
1521 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1522 if (sk->sk_user_data == NULL) {
1523 err = -ENOMEM;
1524 goto err;
1525 }
1526
1527 tunnel->magic = L2TP_TUNNEL_MAGIC;
1528 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1529
1530 tunnel->stats.tunnel_id = tunnel_id;
1531 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1532
1533 /* Hook on the tunnel socket destructor so that we can cleanup
1534 * if the tunnel socket goes away.
1535 */
1536 tunnel->old_sk_destruct = sk->sk_destruct;
1537 sk->sk_destruct = &pppol2tp_tunnel_destruct;
1538
1539 tunnel->sock = sk;
1540 sk->sk_allocation = GFP_ATOMIC;
1541
1542 /* Misc init */
1543 rwlock_init(&tunnel->hlist_lock);
1544
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001545 /* The net we belong to */
1546 tunnel->pppol2tp_net = net;
1547 pn = pppol2tp_pernet(net);
1548
James Chapman3557baa2007-06-27 15:49:24 -07001549 /* Add tunnel to our list */
1550 INIT_LIST_HEAD(&tunnel->list);
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001551 write_lock_bh(&pn->pppol2tp_tunnel_list_lock);
1552 list_add(&tunnel->list, &pn->pppol2tp_tunnel_list);
1553 write_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001554 atomic_inc(&pppol2tp_tunnel_count);
1555
1556 /* Bump the reference count. The tunnel context is deleted
1557 * only when this drops to zero.
1558 */
1559 pppol2tp_tunnel_inc_refcount(tunnel);
1560
1561 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1562 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1563 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1564
1565 ret = tunnel->sock;
1566
1567 *error = 0;
1568out:
1569 if (sock)
1570 sockfd_put(sock);
1571
1572 return ret;
1573
1574err:
1575 *error = err;
1576 goto out;
1577}
1578
1579static struct proto pppol2tp_sk_proto = {
1580 .name = "PPPOL2TP",
1581 .owner = THIS_MODULE,
1582 .obj_size = sizeof(struct pppox_sock),
1583};
1584
1585/* socket() handler. Initialize a new struct sock.
1586 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001587static int pppol2tp_create(struct net *net, struct socket *sock)
James Chapman3557baa2007-06-27 15:49:24 -07001588{
1589 int error = -ENOMEM;
1590 struct sock *sk;
1591
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001592 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
James Chapman3557baa2007-06-27 15:49:24 -07001593 if (!sk)
1594 goto out;
1595
1596 sock_init_data(sock, sk);
1597
1598 sock->state = SS_UNCONNECTED;
1599 sock->ops = &pppol2tp_ops;
1600
1601 sk->sk_backlog_rcv = pppol2tp_recv_core;
1602 sk->sk_protocol = PX_PROTO_OL2TP;
1603 sk->sk_family = PF_PPPOX;
1604 sk->sk_state = PPPOX_NONE;
1605 sk->sk_type = SOCK_STREAM;
1606 sk->sk_destruct = pppol2tp_session_destruct;
1607
1608 error = 0;
1609
1610out:
1611 return error;
1612}
1613
1614/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1615 */
1616static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1617 int sockaddr_len, int flags)
1618{
1619 struct sock *sk = sock->sk;
1620 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1621 struct pppox_sock *po = pppox_sk(sk);
1622 struct sock *tunnel_sock = NULL;
1623 struct pppol2tp_session *session = NULL;
1624 struct pppol2tp_tunnel *tunnel;
1625 struct dst_entry *dst;
1626 int error = 0;
1627
1628 lock_sock(sk);
1629
1630 error = -EINVAL;
1631 if (sp->sa_protocol != PX_PROTO_OL2TP)
1632 goto end;
1633
1634 /* Check for already bound sockets */
1635 error = -EBUSY;
1636 if (sk->sk_state & PPPOX_CONNECTED)
1637 goto end;
1638
1639 /* We don't supporting rebinding anyway */
1640 error = -EALREADY;
1641 if (sk->sk_user_data)
1642 goto end; /* socket is already attached */
1643
1644 /* Don't bind if s_tunnel is 0 */
1645 error = -EINVAL;
1646 if (sp->pppol2tp.s_tunnel == 0)
1647 goto end;
1648
1649 /* Special case: prepare tunnel socket if s_session and
1650 * d_session is 0. Otherwise look up tunnel using supplied
1651 * tunnel id.
1652 */
1653 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001654 tunnel_sock = pppol2tp_prepare_tunnel_socket(sock_net(sk),
1655 sp->pppol2tp.fd,
James Chapman3557baa2007-06-27 15:49:24 -07001656 sp->pppol2tp.s_tunnel,
1657 &error);
1658 if (tunnel_sock == NULL)
1659 goto end;
1660
1661 tunnel = tunnel_sock->sk_user_data;
1662 } else {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001663 tunnel = pppol2tp_tunnel_find(sock_net(sk), sp->pppol2tp.s_tunnel);
James Chapman3557baa2007-06-27 15:49:24 -07001664
1665 /* Error if we can't find the tunnel */
1666 error = -ENOENT;
1667 if (tunnel == NULL)
1668 goto end;
1669
1670 tunnel_sock = tunnel->sock;
1671 }
1672
1673 /* Check that this session doesn't already exist */
1674 error = -EEXIST;
1675 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1676 if (session != NULL)
1677 goto end;
1678
1679 /* Allocate and initialize a new session context. */
1680 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1681 if (session == NULL) {
1682 error = -ENOMEM;
1683 goto end;
1684 }
1685
1686 skb_queue_head_init(&session->reorder_q);
1687
1688 session->magic = L2TP_SESSION_MAGIC;
1689 session->owner = current->pid;
1690 session->sock = sk;
1691 session->tunnel = tunnel;
1692 session->tunnel_sock = tunnel_sock;
1693 session->tunnel_addr = sp->pppol2tp;
1694 sprintf(&session->name[0], "sess %hu/%hu",
1695 session->tunnel_addr.s_tunnel,
1696 session->tunnel_addr.s_session);
1697
1698 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1699 session->stats.session_id = session->tunnel_addr.s_session;
1700
1701 INIT_HLIST_NODE(&session->hlist);
1702
1703 /* Inherit debug options from tunnel */
1704 session->debug = tunnel->debug;
1705
1706 /* Default MTU must allow space for UDP/L2TP/PPP
1707 * headers.
1708 */
1709 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1710
1711 /* If PMTU discovery was enabled, use the MTU that was discovered */
1712 dst = sk_dst_get(sk);
1713 if (dst != NULL) {
1714 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1715 if (pmtu != 0)
1716 session->mtu = session->mru = pmtu -
1717 PPPOL2TP_HEADER_OVERHEAD;
1718 dst_release(dst);
1719 }
1720
1721 /* Special case: if source & dest session_id == 0x0000, this socket is
1722 * being created to manage the tunnel. Don't add the session to the
1723 * session hash list, just set up the internal context for use by
1724 * ioctl() and sockopt() handlers.
1725 */
1726 if ((session->tunnel_addr.s_session == 0) &&
1727 (session->tunnel_addr.d_session == 0)) {
1728 error = 0;
1729 sk->sk_user_data = session;
1730 goto out_no_ppp;
1731 }
1732
1733 /* Get tunnel context from the tunnel socket */
1734 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1735 if (tunnel == NULL) {
1736 error = -EBADF;
1737 goto end;
1738 }
1739
1740 /* Right now, because we don't have a way to push the incoming skb's
1741 * straight through the UDP layer, the only header we need to worry
1742 * about is the L2TP header. This size is different depending on
1743 * whether sequence numbers are enabled for the data channel.
1744 */
1745 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1746
1747 po->chan.private = sk;
1748 po->chan.ops = &pppol2tp_chan_ops;
1749 po->chan.mtu = session->mtu;
1750
Cyrill Gorcunovf5882c32009-01-21 15:55:40 -08001751 error = ppp_register_net_channel(sock_net(sk), &po->chan);
James Chapman3557baa2007-06-27 15:49:24 -07001752 if (error)
James Chapman24b95682008-06-04 15:54:07 -07001753 goto end_put_tun;
James Chapman3557baa2007-06-27 15:49:24 -07001754
1755 /* This is how we get the session context from the socket. */
1756 sk->sk_user_data = session;
1757
1758 /* Add session to the tunnel's hash list */
James Chapmancf3752e2008-03-05 18:39:08 -08001759 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001760 hlist_add_head(&session->hlist,
1761 pppol2tp_session_id_hash(tunnel,
1762 session->tunnel_addr.s_session));
James Chapmancf3752e2008-03-05 18:39:08 -08001763 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001764
1765 atomic_inc(&pppol2tp_session_count);
1766
1767out_no_ppp:
1768 pppol2tp_tunnel_inc_refcount(tunnel);
1769 sk->sk_state = PPPOX_CONNECTED;
1770 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1771 "%s: created\n", session->name);
1772
James Chapman24b95682008-06-04 15:54:07 -07001773end_put_tun:
1774 sock_put(tunnel_sock);
James Chapman3557baa2007-06-27 15:49:24 -07001775end:
1776 release_sock(sk);
1777
Julia Lawall5dc474d2008-05-12 15:43:46 -07001778 if (error != 0) {
1779 if (session)
1780 PRINTK(session->debug,
1781 PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1782 "%s: connect failed: %d\n",
1783 session->name, error);
1784 else
1785 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1786 "connect failed: %d\n", error);
1787 }
James Chapman3557baa2007-06-27 15:49:24 -07001788
1789 return error;
1790}
1791
1792/* getname() support.
1793 */
1794static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1795 int *usockaddr_len, int peer)
1796{
1797 int len = sizeof(struct sockaddr_pppol2tp);
1798 struct sockaddr_pppol2tp sp;
1799 int error = 0;
1800 struct pppol2tp_session *session;
1801
1802 error = -ENOTCONN;
1803 if (sock->sk->sk_state != PPPOX_CONNECTED)
1804 goto end;
1805
1806 session = pppol2tp_sock_to_session(sock->sk);
1807 if (session == NULL) {
1808 error = -EBADF;
1809 goto end;
1810 }
1811
1812 sp.sa_family = AF_PPPOX;
1813 sp.sa_protocol = PX_PROTO_OL2TP;
1814 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1815 sizeof(struct pppol2tp_addr));
1816
1817 memcpy(uaddr, &sp, len);
1818
1819 *usockaddr_len = len;
1820
1821 error = 0;
James Chapman24b95682008-06-04 15:54:07 -07001822 sock_put(sock->sk);
James Chapman3557baa2007-06-27 15:49:24 -07001823
1824end:
1825 return error;
1826}
1827
1828/****************************************************************************
1829 * ioctl() handlers.
1830 *
1831 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1832 * sockets. However, in order to control kernel tunnel features, we allow
1833 * userspace to create a special "tunnel" PPPoX socket which is used for
1834 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1835 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1836 * calls.
1837 ****************************************************************************/
1838
1839/* Session ioctl helper.
1840 */
1841static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1842 unsigned int cmd, unsigned long arg)
1843{
1844 struct ifreq ifr;
1845 int err = 0;
1846 struct sock *sk = session->sock;
1847 int val = (int) arg;
1848
1849 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1850 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1851 session->name, cmd, arg);
1852
1853 sock_hold(sk);
1854
1855 switch (cmd) {
1856 case SIOCGIFMTU:
1857 err = -ENXIO;
1858 if (!(sk->sk_state & PPPOX_CONNECTED))
1859 break;
1860
1861 err = -EFAULT;
1862 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1863 break;
1864 ifr.ifr_mtu = session->mtu;
1865 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1866 break;
1867
1868 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1869 "%s: get mtu=%d\n", session->name, session->mtu);
1870 err = 0;
1871 break;
1872
1873 case SIOCSIFMTU:
1874 err = -ENXIO;
1875 if (!(sk->sk_state & PPPOX_CONNECTED))
1876 break;
1877
1878 err = -EFAULT;
1879 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1880 break;
1881
1882 session->mtu = ifr.ifr_mtu;
1883
1884 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1885 "%s: set mtu=%d\n", session->name, session->mtu);
1886 err = 0;
1887 break;
1888
1889 case PPPIOCGMRU:
1890 err = -ENXIO;
1891 if (!(sk->sk_state & PPPOX_CONNECTED))
1892 break;
1893
1894 err = -EFAULT;
1895 if (put_user(session->mru, (int __user *) arg))
1896 break;
1897
1898 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1899 "%s: get mru=%d\n", session->name, session->mru);
1900 err = 0;
1901 break;
1902
1903 case PPPIOCSMRU:
1904 err = -ENXIO;
1905 if (!(sk->sk_state & PPPOX_CONNECTED))
1906 break;
1907
1908 err = -EFAULT;
1909 if (get_user(val,(int __user *) arg))
1910 break;
1911
1912 session->mru = val;
1913 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1914 "%s: set mru=%d\n", session->name, session->mru);
1915 err = 0;
1916 break;
1917
1918 case PPPIOCGFLAGS:
1919 err = -EFAULT;
1920 if (put_user(session->flags, (int __user *) arg))
1921 break;
1922
1923 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1924 "%s: get flags=%d\n", session->name, session->flags);
1925 err = 0;
1926 break;
1927
1928 case PPPIOCSFLAGS:
1929 err = -EFAULT;
1930 if (get_user(val, (int __user *) arg))
1931 break;
1932 session->flags = val;
1933 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1934 "%s: set flags=%d\n", session->name, session->flags);
1935 err = 0;
1936 break;
1937
1938 case PPPIOCGL2TPSTATS:
1939 err = -ENXIO;
1940 if (!(sk->sk_state & PPPOX_CONNECTED))
1941 break;
1942
1943 if (copy_to_user((void __user *) arg, &session->stats,
1944 sizeof(session->stats)))
1945 break;
1946 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1947 "%s: get L2TP stats\n", session->name);
1948 err = 0;
1949 break;
1950
1951 default:
1952 err = -ENOSYS;
1953 break;
1954 }
1955
1956 sock_put(sk);
1957
1958 return err;
1959}
1960
1961/* Tunnel ioctl helper.
1962 *
1963 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1964 * specifies a session_id, the session ioctl handler is called. This allows an
1965 * application to retrieve session stats via a tunnel socket.
1966 */
1967static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1968 unsigned int cmd, unsigned long arg)
1969{
1970 int err = 0;
1971 struct sock *sk = tunnel->sock;
1972 struct pppol2tp_ioc_stats stats_req;
1973
1974 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1975 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1976 cmd, arg);
1977
1978 sock_hold(sk);
1979
1980 switch (cmd) {
1981 case PPPIOCGL2TPSTATS:
1982 err = -ENXIO;
1983 if (!(sk->sk_state & PPPOX_CONNECTED))
1984 break;
1985
1986 if (copy_from_user(&stats_req, (void __user *) arg,
1987 sizeof(stats_req))) {
1988 err = -EFAULT;
1989 break;
1990 }
1991 if (stats_req.session_id != 0) {
1992 /* resend to session ioctl handler */
1993 struct pppol2tp_session *session =
1994 pppol2tp_session_find(tunnel, stats_req.session_id);
1995 if (session != NULL)
1996 err = pppol2tp_session_ioctl(session, cmd, arg);
1997 else
1998 err = -EBADR;
1999 break;
2000 }
2001#ifdef CONFIG_XFRM
2002 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
2003#endif
2004 if (copy_to_user((void __user *) arg, &tunnel->stats,
2005 sizeof(tunnel->stats))) {
2006 err = -EFAULT;
2007 break;
2008 }
2009 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2010 "%s: get L2TP stats\n", tunnel->name);
2011 err = 0;
2012 break;
2013
2014 default:
2015 err = -ENOSYS;
2016 break;
2017 }
2018
2019 sock_put(sk);
2020
2021 return err;
2022}
2023
2024/* Main ioctl() handler.
2025 * Dispatch to tunnel or session helpers depending on the socket.
2026 */
2027static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
2028 unsigned long arg)
2029{
2030 struct sock *sk = sock->sk;
2031 struct pppol2tp_session *session;
2032 struct pppol2tp_tunnel *tunnel;
2033 int err;
2034
2035 if (!sk)
2036 return 0;
2037
2038 err = -EBADF;
2039 if (sock_flag(sk, SOCK_DEAD) != 0)
2040 goto end;
2041
2042 err = -ENOTCONN;
2043 if ((sk->sk_user_data == NULL) ||
2044 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
2045 goto end;
2046
2047 /* Get session context from the socket */
2048 err = -EBADF;
2049 session = pppol2tp_sock_to_session(sk);
2050 if (session == NULL)
2051 goto end;
2052
2053 /* Special case: if session's session_id is zero, treat ioctl as a
2054 * tunnel ioctl
2055 */
2056 if ((session->tunnel_addr.s_session == 0) &&
2057 (session->tunnel_addr.d_session == 0)) {
2058 err = -EBADF;
2059 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2060 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07002061 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002062
2063 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
James Chapman24b95682008-06-04 15:54:07 -07002064 sock_put(session->tunnel_sock);
2065 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002066 }
2067
2068 err = pppol2tp_session_ioctl(session, cmd, arg);
2069
James Chapman24b95682008-06-04 15:54:07 -07002070end_put_sess:
2071 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07002072end:
2073 return err;
2074}
2075
2076/*****************************************************************************
2077 * setsockopt() / getsockopt() support.
2078 *
2079 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2080 * sockets. In order to control kernel tunnel features, we allow userspace to
2081 * create a special "tunnel" PPPoX socket which is used for control only.
2082 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2083 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2084 *****************************************************************************/
2085
2086/* Tunnel setsockopt() helper.
2087 */
2088static int pppol2tp_tunnel_setsockopt(struct sock *sk,
2089 struct pppol2tp_tunnel *tunnel,
2090 int optname, int val)
2091{
2092 int err = 0;
2093
2094 switch (optname) {
2095 case PPPOL2TP_SO_DEBUG:
2096 tunnel->debug = val;
2097 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2098 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
2099 break;
2100
2101 default:
2102 err = -ENOPROTOOPT;
2103 break;
2104 }
2105
2106 return err;
2107}
2108
2109/* Session setsockopt helper.
2110 */
2111static int pppol2tp_session_setsockopt(struct sock *sk,
2112 struct pppol2tp_session *session,
2113 int optname, int val)
2114{
2115 int err = 0;
2116
2117 switch (optname) {
2118 case PPPOL2TP_SO_RECVSEQ:
2119 if ((val != 0) && (val != 1)) {
2120 err = -EINVAL;
2121 break;
2122 }
2123 session->recv_seq = val ? -1 : 0;
2124 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2125 "%s: set recv_seq=%d\n", session->name,
2126 session->recv_seq);
2127 break;
2128
2129 case PPPOL2TP_SO_SENDSEQ:
2130 if ((val != 0) && (val != 1)) {
2131 err = -EINVAL;
2132 break;
2133 }
2134 session->send_seq = val ? -1 : 0;
2135 {
2136 struct sock *ssk = session->sock;
2137 struct pppox_sock *po = pppox_sk(ssk);
2138 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
2139 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
2140 }
2141 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2142 "%s: set send_seq=%d\n", session->name, session->send_seq);
2143 break;
2144
2145 case PPPOL2TP_SO_LNSMODE:
2146 if ((val != 0) && (val != 1)) {
2147 err = -EINVAL;
2148 break;
2149 }
2150 session->lns_mode = val ? -1 : 0;
2151 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2152 "%s: set lns_mode=%d\n", session->name,
2153 session->lns_mode);
2154 break;
2155
2156 case PPPOL2TP_SO_DEBUG:
2157 session->debug = val;
2158 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2159 "%s: set debug=%x\n", session->name, session->debug);
2160 break;
2161
2162 case PPPOL2TP_SO_REORDERTO:
2163 session->reorder_timeout = msecs_to_jiffies(val);
2164 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2165 "%s: set reorder_timeout=%d\n", session->name,
2166 session->reorder_timeout);
2167 break;
2168
2169 default:
2170 err = -ENOPROTOOPT;
2171 break;
2172 }
2173
2174 return err;
2175}
2176
2177/* Main setsockopt() entry point.
2178 * Does API checks, then calls either the tunnel or session setsockopt
2179 * handler, according to whether the PPPoL2TP socket is a for a regular
2180 * session or the special tunnel type.
2181 */
2182static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
2183 char __user *optval, int optlen)
2184{
2185 struct sock *sk = sock->sk;
2186 struct pppol2tp_session *session = sk->sk_user_data;
2187 struct pppol2tp_tunnel *tunnel;
2188 int val;
2189 int err;
2190
2191 if (level != SOL_PPPOL2TP)
2192 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2193
2194 if (optlen < sizeof(int))
2195 return -EINVAL;
2196
2197 if (get_user(val, (int __user *)optval))
2198 return -EFAULT;
2199
2200 err = -ENOTCONN;
2201 if (sk->sk_user_data == NULL)
2202 goto end;
2203
2204 /* Get session context from the socket */
2205 err = -EBADF;
2206 session = pppol2tp_sock_to_session(sk);
2207 if (session == NULL)
2208 goto end;
2209
2210 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2211 */
2212 if ((session->tunnel_addr.s_session == 0) &&
2213 (session->tunnel_addr.d_session == 0)) {
2214 err = -EBADF;
2215 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2216 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07002217 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002218
2219 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
James Chapman24b95682008-06-04 15:54:07 -07002220 sock_put(session->tunnel_sock);
James Chapman3557baa2007-06-27 15:49:24 -07002221 } else
2222 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2223
2224 err = 0;
2225
James Chapman24b95682008-06-04 15:54:07 -07002226end_put_sess:
2227 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07002228end:
2229 return err;
2230}
2231
2232/* Tunnel getsockopt helper. Called with sock locked.
2233 */
2234static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2235 struct pppol2tp_tunnel *tunnel,
Al Viroaf3b1622007-07-26 17:35:09 +01002236 int optname, int *val)
James Chapman3557baa2007-06-27 15:49:24 -07002237{
2238 int err = 0;
2239
2240 switch (optname) {
2241 case PPPOL2TP_SO_DEBUG:
2242 *val = tunnel->debug;
2243 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2244 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2245 break;
2246
2247 default:
2248 err = -ENOPROTOOPT;
2249 break;
2250 }
2251
2252 return err;
2253}
2254
2255/* Session getsockopt helper. Called with sock locked.
2256 */
2257static int pppol2tp_session_getsockopt(struct sock *sk,
2258 struct pppol2tp_session *session,
Al Viroaf3b1622007-07-26 17:35:09 +01002259 int optname, int *val)
James Chapman3557baa2007-06-27 15:49:24 -07002260{
2261 int err = 0;
2262
2263 switch (optname) {
2264 case PPPOL2TP_SO_RECVSEQ:
2265 *val = session->recv_seq;
2266 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2267 "%s: get recv_seq=%d\n", session->name, *val);
2268 break;
2269
2270 case PPPOL2TP_SO_SENDSEQ:
2271 *val = session->send_seq;
2272 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2273 "%s: get send_seq=%d\n", session->name, *val);
2274 break;
2275
2276 case PPPOL2TP_SO_LNSMODE:
2277 *val = session->lns_mode;
2278 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2279 "%s: get lns_mode=%d\n", session->name, *val);
2280 break;
2281
2282 case PPPOL2TP_SO_DEBUG:
2283 *val = session->debug;
2284 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2285 "%s: get debug=%d\n", session->name, *val);
2286 break;
2287
2288 case PPPOL2TP_SO_REORDERTO:
2289 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2290 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2291 "%s: get reorder_timeout=%d\n", session->name, *val);
2292 break;
2293
2294 default:
2295 err = -ENOPROTOOPT;
2296 }
2297
2298 return err;
2299}
2300
2301/* Main getsockopt() entry point.
2302 * Does API checks, then calls either the tunnel or session getsockopt
2303 * handler, according to whether the PPPoX socket is a for a regular session
2304 * or the special tunnel type.
2305 */
2306static int pppol2tp_getsockopt(struct socket *sock, int level,
2307 int optname, char __user *optval, int __user *optlen)
2308{
2309 struct sock *sk = sock->sk;
2310 struct pppol2tp_session *session = sk->sk_user_data;
2311 struct pppol2tp_tunnel *tunnel;
2312 int val, len;
2313 int err;
2314
2315 if (level != SOL_PPPOL2TP)
2316 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2317
2318 if (get_user(len, (int __user *) optlen))
2319 return -EFAULT;
2320
2321 len = min_t(unsigned int, len, sizeof(int));
2322
2323 if (len < 0)
2324 return -EINVAL;
2325
2326 err = -ENOTCONN;
2327 if (sk->sk_user_data == NULL)
2328 goto end;
2329
2330 /* Get the session context */
2331 err = -EBADF;
2332 session = pppol2tp_sock_to_session(sk);
2333 if (session == NULL)
2334 goto end;
2335
2336 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2337 if ((session->tunnel_addr.s_session == 0) &&
2338 (session->tunnel_addr.d_session == 0)) {
2339 err = -EBADF;
2340 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2341 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07002342 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002343
2344 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
James Chapman24b95682008-06-04 15:54:07 -07002345 sock_put(session->tunnel_sock);
James Chapman3557baa2007-06-27 15:49:24 -07002346 } else
2347 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2348
2349 err = -EFAULT;
2350 if (put_user(len, (int __user *) optlen))
James Chapman24b95682008-06-04 15:54:07 -07002351 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002352
2353 if (copy_to_user((void __user *) optval, &val, len))
James Chapman24b95682008-06-04 15:54:07 -07002354 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002355
2356 err = 0;
James Chapman24b95682008-06-04 15:54:07 -07002357
2358end_put_sess:
2359 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07002360end:
2361 return err;
2362}
2363
2364/*****************************************************************************
2365 * /proc filesystem for debug
2366 *****************************************************************************/
2367
2368#ifdef CONFIG_PROC_FS
2369
2370#include <linux/seq_file.h>
2371
2372struct pppol2tp_seq_data {
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002373 struct seq_net_private p;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002374 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2375 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
James Chapman3557baa2007-06-27 15:49:24 -07002376};
2377
2378static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2379{
2380 struct pppol2tp_session *session = NULL;
2381 struct hlist_node *walk;
2382 int found = 0;
2383 int next = 0;
2384 int i;
2385
James Chapmancf3752e2008-03-05 18:39:08 -08002386 read_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07002387 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2388 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2389 if (curr == NULL) {
2390 found = 1;
2391 goto out;
2392 }
2393 if (session == curr) {
2394 next = 1;
2395 continue;
2396 }
2397 if (next) {
2398 found = 1;
2399 goto out;
2400 }
2401 }
2402 }
2403out:
James Chapmancf3752e2008-03-05 18:39:08 -08002404 read_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07002405 if (!found)
2406 session = NULL;
2407
2408 return session;
2409}
2410
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002411static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_net *pn,
2412 struct pppol2tp_tunnel *curr)
James Chapman3557baa2007-06-27 15:49:24 -07002413{
2414 struct pppol2tp_tunnel *tunnel = NULL;
2415
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002416 read_lock_bh(&pn->pppol2tp_tunnel_list_lock);
2417 if (list_is_last(&curr->list, &pn->pppol2tp_tunnel_list)) {
James Chapman3557baa2007-06-27 15:49:24 -07002418 goto out;
2419 }
2420 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2421out:
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002422 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07002423
2424 return tunnel;
2425}
2426
2427static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2428{
2429 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002430 struct pppol2tp_net *pn;
James Chapman3557baa2007-06-27 15:49:24 -07002431 loff_t pos = *offs;
2432
2433 if (!pos)
2434 goto out;
2435
2436 BUG_ON(m->private == NULL);
2437 pd = m->private;
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002438 pn = pppol2tp_pernet(seq_file_net(m));
James Chapman3557baa2007-06-27 15:49:24 -07002439
2440 if (pd->tunnel == NULL) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002441 if (!list_empty(&pn->pppol2tp_tunnel_list))
2442 pd->tunnel = list_entry(pn->pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
James Chapman3557baa2007-06-27 15:49:24 -07002443 } else {
2444 pd->session = next_session(pd->tunnel, pd->session);
2445 if (pd->session == NULL) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002446 pd->tunnel = next_tunnel(pn, pd->tunnel);
James Chapman3557baa2007-06-27 15:49:24 -07002447 }
2448 }
2449
2450 /* NULL tunnel and session indicates end of list */
2451 if ((pd->tunnel == NULL) && (pd->session == NULL))
2452 pd = NULL;
2453
2454out:
2455 return pd;
2456}
2457
2458static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2459{
2460 (*pos)++;
2461 return NULL;
2462}
2463
2464static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2465{
2466 /* nothing to do */
2467}
2468
2469static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2470{
2471 struct pppol2tp_tunnel *tunnel = v;
2472
2473 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2474 tunnel->name,
2475 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2476 atomic_read(&tunnel->ref_count) - 1);
2477 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2478 tunnel->debug,
Andrew Morton0efeaa32008-02-09 23:17:51 -08002479 (unsigned long long)tunnel->stats.tx_packets,
2480 (unsigned long long)tunnel->stats.tx_bytes,
2481 (unsigned long long)tunnel->stats.tx_errors,
2482 (unsigned long long)tunnel->stats.rx_packets,
2483 (unsigned long long)tunnel->stats.rx_bytes,
2484 (unsigned long long)tunnel->stats.rx_errors);
James Chapman3557baa2007-06-27 15:49:24 -07002485}
2486
2487static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2488{
2489 struct pppol2tp_session *session = v;
2490
2491 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2492 "%04X/%04X %d %c\n",
2493 session->name,
2494 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2495 ntohs(session->tunnel_addr.addr.sin_port),
2496 session->tunnel_addr.s_tunnel,
2497 session->tunnel_addr.s_session,
2498 session->tunnel_addr.d_tunnel,
2499 session->tunnel_addr.d_session,
2500 session->sock->sk_state,
2501 (session == session->sock->sk_user_data) ?
2502 'Y' : 'N');
2503 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2504 session->mtu, session->mru,
2505 session->recv_seq ? 'R' : '-',
2506 session->send_seq ? 'S' : '-',
2507 session->lns_mode ? "LNS" : "LAC",
2508 session->debug,
2509 jiffies_to_msecs(session->reorder_timeout));
2510 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2511 session->nr, session->ns,
Andrew Morton0efeaa32008-02-09 23:17:51 -08002512 (unsigned long long)session->stats.tx_packets,
2513 (unsigned long long)session->stats.tx_bytes,
2514 (unsigned long long)session->stats.tx_errors,
2515 (unsigned long long)session->stats.rx_packets,
2516 (unsigned long long)session->stats.rx_bytes,
2517 (unsigned long long)session->stats.rx_errors);
James Chapman3557baa2007-06-27 15:49:24 -07002518}
2519
2520static int pppol2tp_seq_show(struct seq_file *m, void *v)
2521{
2522 struct pppol2tp_seq_data *pd = v;
2523
2524 /* display header on line 1 */
2525 if (v == SEQ_START_TOKEN) {
2526 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2527 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2528 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2529 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2530 "dest-tid/sid state user-data-ok\n");
2531 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2532 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2533 goto out;
2534 }
2535
2536 /* Show the tunnel or session context.
2537 */
2538 if (pd->session == NULL)
2539 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2540 else
2541 pppol2tp_seq_session_show(m, pd->session);
2542
2543out:
2544 return 0;
2545}
2546
Jan Engelhardt4101dec2009-01-14 13:52:18 -08002547static const struct seq_operations pppol2tp_seq_ops = {
James Chapman3557baa2007-06-27 15:49:24 -07002548 .start = pppol2tp_seq_start,
2549 .next = pppol2tp_seq_next,
2550 .stop = pppol2tp_seq_stop,
2551 .show = pppol2tp_seq_show,
2552};
2553
2554/* Called when our /proc file is opened. We allocate data for use when
2555 * iterating our tunnel / session contexts and store it in the private
2556 * data of the seq_file.
2557 */
2558static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2559{
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002560 return seq_open_net(inode, file, &pppol2tp_seq_ops,
2561 sizeof(struct pppol2tp_seq_data));
James Chapman3557baa2007-06-27 15:49:24 -07002562}
2563
Jan Engelhardt4101dec2009-01-14 13:52:18 -08002564static const struct file_operations pppol2tp_proc_fops = {
James Chapman3557baa2007-06-27 15:49:24 -07002565 .owner = THIS_MODULE,
2566 .open = pppol2tp_proc_open,
2567 .read = seq_read,
2568 .llseek = seq_lseek,
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002569 .release = seq_release_net,
James Chapman3557baa2007-06-27 15:49:24 -07002570};
2571
James Chapman3557baa2007-06-27 15:49:24 -07002572#endif /* CONFIG_PROC_FS */
2573
2574/*****************************************************************************
2575 * Init and cleanup
2576 *****************************************************************************/
2577
2578static struct proto_ops pppol2tp_ops = {
2579 .family = AF_PPPOX,
2580 .owner = THIS_MODULE,
2581 .release = pppol2tp_release,
2582 .bind = sock_no_bind,
2583 .connect = pppol2tp_connect,
2584 .socketpair = sock_no_socketpair,
2585 .accept = sock_no_accept,
2586 .getname = pppol2tp_getname,
2587 .poll = datagram_poll,
2588 .listen = sock_no_listen,
2589 .shutdown = sock_no_shutdown,
2590 .setsockopt = pppol2tp_setsockopt,
2591 .getsockopt = pppol2tp_getsockopt,
2592 .sendmsg = pppol2tp_sendmsg,
2593 .recvmsg = pppol2tp_recvmsg,
2594 .mmap = sock_no_mmap,
2595 .ioctl = pppox_ioctl,
2596};
2597
2598static struct pppox_proto pppol2tp_proto = {
2599 .create = pppol2tp_create,
2600 .ioctl = pppol2tp_ioctl
2601};
2602
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002603static __net_init int pppol2tp_init_net(struct net *net)
2604{
2605 struct pppol2tp_net *pn;
2606 struct proc_dir_entry *pde;
2607 int err;
2608
2609 pn = kzalloc(sizeof(*pn), GFP_KERNEL);
2610 if (!pn)
2611 return -ENOMEM;
2612
2613 INIT_LIST_HEAD(&pn->pppol2tp_tunnel_list);
2614 rwlock_init(&pn->pppol2tp_tunnel_list_lock);
2615
2616 err = net_assign_generic(net, pppol2tp_net_id, pn);
2617 if (err)
2618 goto out;
2619
2620 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
2621#ifdef CONFIG_PROC_FS
2622 if (!pde) {
2623 err = -ENOMEM;
2624 goto out;
2625 }
2626#endif
2627
2628 return 0;
2629
2630out:
2631 kfree(pn);
2632 return err;
2633}
2634
2635static __net_exit void pppol2tp_exit_net(struct net *net)
2636{
2637 struct pppoe_net *pn;
2638
2639 proc_net_remove(net, "pppol2tp");
2640 pn = net_generic(net, pppol2tp_net_id);
2641 /*
2642 * if someone has cached our net then
2643 * further net_generic call will return NULL
2644 */
2645 net_assign_generic(net, pppol2tp_net_id, NULL);
2646 kfree(pn);
2647}
2648
Alexey Dobriyan00129852009-02-09 18:05:16 -08002649static struct pernet_operations pppol2tp_net_ops = {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002650 .init = pppol2tp_init_net,
2651 .exit = pppol2tp_exit_net,
2652};
2653
James Chapman3557baa2007-06-27 15:49:24 -07002654static int __init pppol2tp_init(void)
2655{
2656 int err;
2657
2658 err = proto_register(&pppol2tp_sk_proto, 0);
2659 if (err)
2660 goto out;
2661 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2662 if (err)
2663 goto out_unregister_pppol2tp_proto;
2664
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002665 err = register_pernet_gen_device(&pppol2tp_net_id, &pppol2tp_net_ops);
2666 if (err)
James Chapman3557baa2007-06-27 15:49:24 -07002667 goto out_unregister_pppox_proto;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002668
James Chapman3557baa2007-06-27 15:49:24 -07002669 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2670 PPPOL2TP_DRV_VERSION);
2671
2672out:
2673 return err;
James Chapman3557baa2007-06-27 15:49:24 -07002674out_unregister_pppox_proto:
2675 unregister_pppox_proto(PX_PROTO_OL2TP);
2676out_unregister_pppol2tp_proto:
2677 proto_unregister(&pppol2tp_sk_proto);
2678 goto out;
2679}
2680
2681static void __exit pppol2tp_exit(void)
2682{
2683 unregister_pppox_proto(PX_PROTO_OL2TP);
James Chapman3557baa2007-06-27 15:49:24 -07002684 proto_unregister(&pppol2tp_sk_proto);
2685}
2686
2687module_init(pppol2tp_init);
2688module_exit(pppol2tp_exit);
2689
Joe Perches24500222007-11-19 17:48:28 -08002690MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
James Chapman3557baa2007-06-27 15:49:24 -07002691 "James Chapman <jchapman@katalix.com>");
2692MODULE_DESCRIPTION("PPP over L2TP over UDP");
2693MODULE_LICENSE("GPL");
2694MODULE_VERSION(PPPOL2TP_DRV_VERSION);