blob: 5861ee9599a2ff9a34ee0ed65590e1f538b6601d [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 };
Alexey Dobriyan5708e862009-09-14 12:23:23 +0000232static const struct proto_ops pppol2tp_ops;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -0800233
234/* per-net private data for this module */
Eric Dumazetf99189b2009-11-17 10:42:49 +0000235static int pppol2tp_net_id __read_mostly;
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);
Eric Dumazetadf30902009-06-02 05:19:30 +0000436 skb_dst_drop(skb);
James Chapman3557baa2007-06-27 15:49:24 -0700437 nf_reset(skb);
438
439 po = pppox_sk(session_sock);
440 ppp_input(&po->chan, skb);
441 } else {
442 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
443 "%s: socket not bound\n", session->name);
444
445 /* Not bound. Nothing we can do, so discard. */
446 session->stats.rx_errors++;
447 kfree_skb(skb);
448 }
449
450 sock_put(session->sock);
451}
452
453/* Dequeue skbs from the session's reorder_q, subject to packet order.
454 * Skbs that have been in the queue for too long are simply discarded.
455 */
456static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
457{
458 struct sk_buff *skb;
459 struct sk_buff *tmp;
460
461 /* If the pkt at the head of the queue has the nr that we
462 * expect to send up next, dequeue it and any other
463 * in-sequence packets behind it.
464 */
James Chapmancf3752e2008-03-05 18:39:08 -0800465 spin_lock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700466 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
467 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
468 session->stats.rx_seq_discards++;
469 session->stats.rx_errors++;
470 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
471 "%s: oos pkt %hu len %d discarded (too old), "
472 "waiting for %hu, reorder_q_len=%d\n",
473 session->name, PPPOL2TP_SKB_CB(skb)->ns,
474 PPPOL2TP_SKB_CB(skb)->length, session->nr,
475 skb_queue_len(&session->reorder_q));
476 __skb_unlink(skb, &session->reorder_q);
477 kfree_skb(skb);
Jarek Poplawskic8fff1c2008-03-03 20:48:53 -0800478 sock_put(session->sock);
James Chapman3557baa2007-06-27 15:49:24 -0700479 continue;
480 }
481
482 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
483 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
484 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
485 "%s: holding oos pkt %hu len %d, "
486 "waiting for %hu, reorder_q_len=%d\n",
487 session->name, PPPOL2TP_SKB_CB(skb)->ns,
488 PPPOL2TP_SKB_CB(skb)->length, session->nr,
489 skb_queue_len(&session->reorder_q));
490 goto out;
491 }
492 }
James Chapmane6531812008-03-05 18:40:01 -0800493 __skb_unlink(skb, &session->reorder_q);
494
495 /* Process the skb. We release the queue lock while we
496 * do so to let other contexts process the queue.
497 */
James Chapmancf3752e2008-03-05 18:39:08 -0800498 spin_unlock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700499 pppol2tp_recv_dequeue_skb(session, skb);
James Chapmancf3752e2008-03-05 18:39:08 -0800500 spin_lock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700501 }
502
503out:
James Chapmancf3752e2008-03-05 18:39:08 -0800504 spin_unlock_bh(&session->reorder_q.lock);
James Chapman3557baa2007-06-27 15:49:24 -0700505}
506
James Chapmanffcebb12008-12-16 01:23:49 -0800507static inline int pppol2tp_verify_udp_checksum(struct sock *sk,
508 struct sk_buff *skb)
509{
510 struct udphdr *uh = udp_hdr(skb);
511 u16 ulen = ntohs(uh->len);
512 struct inet_sock *inet;
513 __wsum psum;
514
515 if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
516 return 0;
517
518 inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000519 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
James Chapmanffcebb12008-12-16 01:23:49 -0800520 IPPROTO_UDP, 0);
521
522 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
523 !csum_fold(csum_add(psum, skb->csum)))
524 return 0;
525
526 skb->csum = psum;
527
528 return __skb_checksum_complete(skb);
529}
530
James Chapman3557baa2007-06-27 15:49:24 -0700531/* Internal receive frame. Do the real work of receiving an L2TP data frame
532 * here. The skb is not on a list when we get here.
533 * Returns 0 if the packet was a data packet and was successfully passed on.
534 * Returns 1 if the packet was not a good data packet and could not be
535 * forwarded. All such packets are passed up to userspace to deal with.
536 */
537static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
538{
539 struct pppol2tp_session *session = NULL;
540 struct pppol2tp_tunnel *tunnel;
James Chapman91781002007-11-05 23:32:37 -0800541 unsigned char *ptr, *optr;
James Chapman3557baa2007-06-27 15:49:24 -0700542 u16 hdrflags;
543 u16 tunnel_id, session_id;
544 int length;
Herbert Xu7a70e392007-09-18 13:18:42 -0700545 int offset;
James Chapman3557baa2007-06-27 15:49:24 -0700546
547 tunnel = pppol2tp_sock_to_tunnel(sock);
548 if (tunnel == NULL)
James Chapman91781002007-11-05 23:32:37 -0800549 goto no_tunnel;
James Chapman3557baa2007-06-27 15:49:24 -0700550
James Chapmanffcebb12008-12-16 01:23:49 -0800551 if (tunnel->sock && pppol2tp_verify_udp_checksum(tunnel->sock, skb))
552 goto discard_bad_csum;
553
Herbert Xu7a70e392007-09-18 13:18:42 -0700554 /* UDP always verifies the packet length. */
555 __skb_pull(skb, sizeof(struct udphdr));
556
James Chapman3557baa2007-06-27 15:49:24 -0700557 /* Short packet? */
Herbert Xu7a70e392007-09-18 13:18:42 -0700558 if (!pskb_may_pull(skb, 12)) {
James Chapman3557baa2007-06-27 15:49:24 -0700559 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
560 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
561 goto error;
562 }
563
564 /* Point to L2TP header */
James Chapman91781002007-11-05 23:32:37 -0800565 optr = ptr = skb->data;
James Chapman3557baa2007-06-27 15:49:24 -0700566
567 /* Get L2TP header flags */
568 hdrflags = ntohs(*(__be16*)ptr);
569
570 /* Trace packet contents, if enabled */
571 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
Herbert Xu7a70e392007-09-18 13:18:42 -0700572 length = min(16u, skb->len);
573 if (!pskb_may_pull(skb, length))
574 goto error;
575
James Chapman3557baa2007-06-27 15:49:24 -0700576 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
577
Herbert Xu7a70e392007-09-18 13:18:42 -0700578 offset = 0;
579 do {
580 printk(" %02X", ptr[offset]);
581 } while (++offset < length);
582
James Chapman3557baa2007-06-27 15:49:24 -0700583 printk("\n");
584 }
585
586 /* Get length of L2TP packet */
Herbert Xu7a70e392007-09-18 13:18:42 -0700587 length = skb->len;
James Chapman3557baa2007-06-27 15:49:24 -0700588
589 /* If type is control packet, it is handled by userspace. */
590 if (hdrflags & L2TP_HDRFLAG_T) {
591 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
592 "%s: recv control packet, len=%d\n", tunnel->name, length);
593 goto error;
594 }
595
596 /* Skip flags */
597 ptr += 2;
598
599 /* If length is present, skip it */
600 if (hdrflags & L2TP_HDRFLAG_L)
601 ptr += 2;
602
603 /* Extract tunnel and session ID */
604 tunnel_id = ntohs(*(__be16 *) ptr);
605 ptr += 2;
606 session_id = ntohs(*(__be16 *) ptr);
607 ptr += 2;
608
609 /* Find the session context */
610 session = pppol2tp_session_find(tunnel, session_id);
611 if (!session) {
612 /* Not found? Pass to userspace to deal with */
613 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
614 "%s: no socket found (%hu/%hu). Passing up.\n",
615 tunnel->name, tunnel_id, session_id);
616 goto error;
617 }
618 sock_hold(session->sock);
619
620 /* The ref count on the socket was increased by the above call since
621 * we now hold a pointer to the session. Take care to do sock_put()
622 * when exiting this function from now on...
623 */
624
625 /* Handle the optional sequence numbers. If we are the LAC,
626 * enable/disable sequence numbers under the control of the LNS. If
627 * no sequence numbers present but we were expecting them, discard
628 * frame.
629 */
630 if (hdrflags & L2TP_HDRFLAG_S) {
631 u16 ns, nr;
632 ns = ntohs(*(__be16 *) ptr);
633 ptr += 2;
634 nr = ntohs(*(__be16 *) ptr);
635 ptr += 2;
636
637 /* Received a packet with sequence numbers. If we're the LNS,
638 * check if we sre sending sequence numbers and if not,
639 * configure it so.
640 */
641 if ((!session->lns_mode) && (!session->send_seq)) {
642 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
643 "%s: requested to enable seq numbers by LNS\n",
644 session->name);
645 session->send_seq = -1;
646 }
647
648 /* Store L2TP info in the skb */
649 PPPOL2TP_SKB_CB(skb)->ns = ns;
650 PPPOL2TP_SKB_CB(skb)->nr = nr;
651 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
652
653 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
654 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
655 session->name, ns, nr, session->nr);
656 } else {
657 /* No sequence numbers.
658 * If user has configured mandatory sequence numbers, discard.
659 */
660 if (session->recv_seq) {
661 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
662 "%s: recv data has no seq numbers when required. "
663 "Discarding\n", session->name);
664 session->stats.rx_seq_discards++;
James Chapman3557baa2007-06-27 15:49:24 -0700665 goto discard;
666 }
667
668 /* If we're the LAC and we're sending sequence numbers, the
669 * LNS has requested that we no longer send sequence numbers.
670 * If we're the LNS and we're sending sequence numbers, the
671 * LAC is broken. Discard the frame.
672 */
673 if ((!session->lns_mode) && (session->send_seq)) {
674 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
675 "%s: requested to disable seq numbers by LNS\n",
676 session->name);
677 session->send_seq = 0;
678 } else if (session->send_seq) {
679 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
680 "%s: recv data has no seq numbers when required. "
681 "Discarding\n", session->name);
682 session->stats.rx_seq_discards++;
James Chapman3557baa2007-06-27 15:49:24 -0700683 goto discard;
684 }
685
686 /* Store L2TP info in the skb */
687 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
688 }
689
690 /* If offset bit set, skip it. */
Herbert Xu7a70e392007-09-18 13:18:42 -0700691 if (hdrflags & L2TP_HDRFLAG_O) {
692 offset = ntohs(*(__be16 *)ptr);
James Chapman91781002007-11-05 23:32:37 -0800693 ptr += 2 + offset;
Herbert Xu7a70e392007-09-18 13:18:42 -0700694 }
James Chapman3557baa2007-06-27 15:49:24 -0700695
James Chapman91781002007-11-05 23:32:37 -0800696 offset = ptr - optr;
697 if (!pskb_may_pull(skb, offset))
698 goto discard;
699
700 __skb_pull(skb, offset);
James Chapman3557baa2007-06-27 15:49:24 -0700701
702 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
703 * don't send the PPP header (PPP header compression enabled), but
704 * other clients can include the header. So we cope with both cases
705 * here. The PPP header is always FF03 when using L2TP.
706 *
707 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
708 * the field may be unaligned.
709 */
James Chapman91781002007-11-05 23:32:37 -0800710 if (!pskb_may_pull(skb, 2))
711 goto discard;
712
James Chapman3557baa2007-06-27 15:49:24 -0700713 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
714 skb_pull(skb, 2);
715
716 /* Prepare skb for adding to the session's reorder_q. Hold
717 * packets for max reorder_timeout or 1 second if not
718 * reordering.
719 */
720 PPPOL2TP_SKB_CB(skb)->length = length;
721 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
722 (session->reorder_timeout ? session->reorder_timeout : HZ);
723
724 /* Add packet to the session's receive queue. Reordering is done here, if
725 * enabled. Saved L2TP protocol info is stored in skb->sb[].
726 */
727 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
728 if (session->reorder_timeout != 0) {
729 /* Packet reordering enabled. Add skb to session's
730 * reorder queue, in order of ns.
731 */
732 pppol2tp_recv_queue_skb(session, skb);
733 } else {
734 /* Packet reordering disabled. Discard out-of-sequence
735 * packets
736 */
737 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
738 session->stats.rx_seq_discards++;
James Chapman3557baa2007-06-27 15:49:24 -0700739 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
740 "%s: oos pkt %hu len %d discarded, "
741 "waiting for %hu, reorder_q_len=%d\n",
742 session->name, PPPOL2TP_SKB_CB(skb)->ns,
743 PPPOL2TP_SKB_CB(skb)->length, session->nr,
744 skb_queue_len(&session->reorder_q));
745 goto discard;
746 }
747 skb_queue_tail(&session->reorder_q, skb);
748 }
749 } else {
750 /* No sequence numbers. Add the skb to the tail of the
751 * reorder queue. This ensures that it will be
752 * delivered after all previous sequenced skbs.
753 */
754 skb_queue_tail(&session->reorder_q, skb);
755 }
756
757 /* Try to dequeue as many skbs from reorder_q as we can. */
758 pppol2tp_recv_dequeue(session);
759
760 return 0;
761
762discard:
Herbert Xu7a70e392007-09-18 13:18:42 -0700763 session->stats.rx_errors++;
James Chapman3557baa2007-06-27 15:49:24 -0700764 kfree_skb(skb);
765 sock_put(session->sock);
James Chapman24b95682008-06-04 15:54:07 -0700766 sock_put(sock);
James Chapman3557baa2007-06-27 15:49:24 -0700767
768 return 0;
769
James Chapmanffcebb12008-12-16 01:23:49 -0800770discard_bad_csum:
771 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
772 UDP_INC_STATS_USER(&init_net, UDP_MIB_INERRORS, 0);
773 tunnel->stats.rx_errors++;
774 kfree_skb(skb);
775
776 return 0;
777
James Chapman3557baa2007-06-27 15:49:24 -0700778error:
James Chapman91781002007-11-05 23:32:37 -0800779 /* Put UDP header back */
780 __skb_push(skb, sizeof(struct udphdr));
James Chapman24b95682008-06-04 15:54:07 -0700781 sock_put(sock);
James Chapman91781002007-11-05 23:32:37 -0800782
783no_tunnel:
James Chapman3557baa2007-06-27 15:49:24 -0700784 return 1;
785}
786
787/* UDP encapsulation receive handler. See net/ipv4/udp.c.
788 * Return codes:
789 * 0 : success.
790 * <0: error
791 * >0: skb should be passed up to userspace as UDP.
792 */
793static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
794{
795 struct pppol2tp_tunnel *tunnel;
796
797 tunnel = pppol2tp_sock_to_tunnel(sk);
798 if (tunnel == NULL)
799 goto pass_up;
800
801 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
802 "%s: received %d bytes\n", tunnel->name, skb->len);
803
804 if (pppol2tp_recv_core(sk, skb))
James Chapman24b95682008-06-04 15:54:07 -0700805 goto pass_up_put;
James Chapman3557baa2007-06-27 15:49:24 -0700806
James Chapman24b95682008-06-04 15:54:07 -0700807 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -0700808 return 0;
809
James Chapman24b95682008-06-04 15:54:07 -0700810pass_up_put:
811 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -0700812pass_up:
813 return 1;
814}
815
816/* Receive message. This is the recvmsg for the PPPoL2TP socket.
817 */
818static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
819 struct msghdr *msg, size_t len,
820 int flags)
821{
822 int err;
823 struct sk_buff *skb;
824 struct sock *sk = sock->sk;
825
826 err = -EIO;
827 if (sk->sk_state & PPPOX_BOUND)
828 goto end;
829
830 msg->msg_namelen = 0;
831
832 err = 0;
833 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
834 flags & MSG_DONTWAIT, &err);
James Chapman6b6707a2008-06-10 12:35:00 -0700835 if (!skb)
836 goto end;
837
838 if (len > skb->len)
839 len = skb->len;
840 else if (len < skb->len)
841 msg->msg_flags |= MSG_TRUNC;
842
843 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
844 if (likely(err == 0))
845 err = len;
846
James Chapman3557baa2007-06-27 15:49:24 -0700847 kfree_skb(skb);
848end:
849 return err;
850}
851
852/************************************************************************
853 * Transmit handling
854 ***********************************************************************/
855
856/* Tell how big L2TP headers are for a particular session. This
857 * depends on whether sequence numbers are being used.
858 */
859static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
860{
861 if (session->send_seq)
862 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
863
864 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
865}
866
867/* Build an L2TP header for the session into the buffer provided.
868 */
869static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
870 void *buf)
871{
872 __be16 *bufp = buf;
873 u16 flags = L2TP_HDR_VER;
874
875 if (session->send_seq)
876 flags |= L2TP_HDRFLAG_S;
877
878 /* Setup L2TP header.
879 * FIXME: Can this ever be unaligned? Is direct dereferencing of
880 * 16-bit header fields safe here for all architectures?
881 */
882 *bufp++ = htons(flags);
883 *bufp++ = htons(session->tunnel_addr.d_tunnel);
884 *bufp++ = htons(session->tunnel_addr.d_session);
885 if (session->send_seq) {
886 *bufp++ = htons(session->ns);
887 *bufp++ = 0;
888 session->ns++;
889 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
890 "%s: updated ns to %hu\n", session->name, session->ns);
891 }
892}
893
894/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
895 * when a user application does a sendmsg() on the session socket. L2TP and
896 * PPP headers must be inserted into the user's data.
897 */
898static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
899 size_t total_len)
900{
901 static const unsigned char ppph[2] = { 0xff, 0x03 };
902 struct sock *sk = sock->sk;
903 struct inet_sock *inet;
James Chapmanffcebb12008-12-16 01:23:49 -0800904 __wsum csum;
James Chapman3557baa2007-06-27 15:49:24 -0700905 struct sk_buff *skb;
906 int error;
907 int hdr_len;
908 struct pppol2tp_session *session;
909 struct pppol2tp_tunnel *tunnel;
910 struct udphdr *uh;
Patrick McHardy7d4372b2007-07-18 02:04:09 -0700911 unsigned int len;
James Chapmanffcebb12008-12-16 01:23:49 -0800912 struct sock *sk_tun;
913 u16 udp_len;
James Chapman3557baa2007-06-27 15:49:24 -0700914
915 error = -ENOTCONN;
916 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
917 goto error;
918
919 /* Get session and tunnel contexts */
920 error = -EBADF;
921 session = pppol2tp_sock_to_session(sk);
922 if (session == NULL)
923 goto error;
924
James Chapmanffcebb12008-12-16 01:23:49 -0800925 sk_tun = session->tunnel_sock;
926 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
James Chapman3557baa2007-06-27 15:49:24 -0700927 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -0700928 goto error_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -0700929
930 /* What header length is configured for this session? */
931 hdr_len = pppol2tp_l2tp_header_len(session);
932
933 /* Allocate a socket buffer */
934 error = -ENOMEM;
935 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
936 sizeof(struct udphdr) + hdr_len +
937 sizeof(ppph) + total_len,
938 0, GFP_KERNEL);
939 if (!skb)
James Chapman24b95682008-06-04 15:54:07 -0700940 goto error_put_sess_tun;
James Chapman3557baa2007-06-27 15:49:24 -0700941
942 /* Reserve space for headers. */
943 skb_reserve(skb, NET_SKB_PAD);
944 skb_reset_network_header(skb);
945 skb_reserve(skb, sizeof(struct iphdr));
946 skb_reset_transport_header(skb);
947
948 /* Build UDP header */
James Chapmanffcebb12008-12-16 01:23:49 -0800949 inet = inet_sk(sk_tun);
950 udp_len = hdr_len + sizeof(ppph) + total_len;
James Chapman3557baa2007-06-27 15:49:24 -0700951 uh = (struct udphdr *) skb->data;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000952 uh->source = inet->inet_sport;
953 uh->dest = inet->inet_dport;
James Chapmanffcebb12008-12-16 01:23:49 -0800954 uh->len = htons(udp_len);
James Chapman3557baa2007-06-27 15:49:24 -0700955 uh->check = 0;
956 skb_put(skb, sizeof(struct udphdr));
957
958 /* Build L2TP header */
959 pppol2tp_build_l2tp_header(session, skb->data);
960 skb_put(skb, hdr_len);
961
962 /* Add PPP header */
963 skb->data[0] = ppph[0];
964 skb->data[1] = ppph[1];
965 skb_put(skb, 2);
966
967 /* Copy user data into skb */
968 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
969 if (error < 0) {
970 kfree_skb(skb);
James Chapman24b95682008-06-04 15:54:07 -0700971 goto error_put_sess_tun;
James Chapman3557baa2007-06-27 15:49:24 -0700972 }
973 skb_put(skb, total_len);
974
975 /* Calculate UDP checksum if configured to do so */
James Chapmanffcebb12008-12-16 01:23:49 -0800976 if (sk_tun->sk_no_check == UDP_CSUM_NOXMIT)
977 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetadf30902009-06-02 05:19:30 +0000978 else if (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM)) {
James Chapmanffcebb12008-12-16 01:23:49 -0800979 skb->ip_summed = CHECKSUM_COMPLETE;
980 csum = skb_checksum(skb, 0, udp_len, 0);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000981 uh->check = csum_tcpudp_magic(inet->inet_saddr,
982 inet->inet_daddr,
James Chapmanffcebb12008-12-16 01:23:49 -0800983 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);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000990 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
991 inet->inet_daddr,
James Chapmanffcebb12008-12-16 01:23:49 -0800992 udp_len, IPPROTO_UDP, 0);
993 }
James Chapman3557baa2007-06-27 15:49:24 -0700994
995 /* Debug */
996 if (session->send_seq)
997 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
David S. Miller38d15b62007-06-27 15:52:25 -0700998 "%s: send %Zd bytes, ns=%hu\n", session->name,
James Chapman3557baa2007-06-27 15:49:24 -0700999 total_len, session->ns - 1);
1000 else
1001 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
David S. Miller38d15b62007-06-27 15:52:25 -07001002 "%s: send %Zd bytes\n", session->name, total_len);
James Chapman3557baa2007-06-27 15:49:24 -07001003
1004 if (session->debug & PPPOL2TP_MSG_DATA) {
1005 int i;
1006 unsigned char *datap = skb->data;
1007
1008 printk(KERN_DEBUG "%s: xmit:", session->name);
1009 for (i = 0; i < total_len; i++) {
1010 printk(" %02X", *datap++);
1011 if (i == 15) {
1012 printk(" ...");
1013 break;
1014 }
1015 }
1016 printk("\n");
1017 }
1018
1019 /* Queue the packet to IP for output */
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001020 len = skb->len;
James Chapman3557baa2007-06-27 15:49:24 -07001021 error = ip_queue_xmit(skb, 1);
1022
1023 /* Update stats */
1024 if (error >= 0) {
1025 tunnel->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001026 tunnel->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001027 session->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001028 session->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001029 } else {
1030 tunnel->stats.tx_errors++;
1031 session->stats.tx_errors++;
1032 }
1033
James Chapman24b95682008-06-04 15:54:07 -07001034 return error;
1035
1036error_put_sess_tun:
1037 sock_put(session->tunnel_sock);
1038error_put_sess:
1039 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07001040error:
1041 return error;
1042}
1043
James Chapman24b95682008-06-04 15:54:07 -07001044/* Automatically called when the skb is freed.
1045 */
1046static void pppol2tp_sock_wfree(struct sk_buff *skb)
1047{
1048 sock_put(skb->sk);
1049}
1050
1051/* For data skbs that we transmit, we associate with the tunnel socket
1052 * but don't do accounting.
1053 */
1054static inline void pppol2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1055{
1056 sock_hold(sk);
1057 skb->sk = sk;
1058 skb->destructor = pppol2tp_sock_wfree;
1059}
1060
James Chapman3557baa2007-06-27 15:49:24 -07001061/* Transmit function called by generic PPP driver. Sends PPP frame
1062 * over PPPoL2TP socket.
1063 *
1064 * This is almost the same as pppol2tp_sendmsg(), but rather than
1065 * being called with a msghdr from userspace, it is called with a skb
1066 * from the kernel.
1067 *
1068 * The supplied skb from ppp doesn't have enough headroom for the
1069 * insertion of L2TP, UDP and IP headers so we need to allocate more
1070 * headroom in the skb. This will create a cloned skb. But we must be
1071 * careful in the error case because the caller will expect to free
1072 * the skb it supplied, not our cloned skb. So we take care to always
1073 * leave the original skb unfreed if we return an error.
1074 */
1075static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
1076{
1077 static const u8 ppph[2] = { 0xff, 0x03 };
1078 struct sock *sk = (struct sock *) chan->private;
1079 struct sock *sk_tun;
1080 int hdr_len;
James Chapmanffcebb12008-12-16 01:23:49 -08001081 u16 udp_len;
James Chapman3557baa2007-06-27 15:49:24 -07001082 struct pppol2tp_session *session;
1083 struct pppol2tp_tunnel *tunnel;
1084 int rc;
1085 int headroom;
1086 int data_len = skb->len;
1087 struct inet_sock *inet;
James Chapmanffcebb12008-12-16 01:23:49 -08001088 __wsum csum;
James Chapman3557baa2007-06-27 15:49:24 -07001089 struct udphdr *uh;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001090 unsigned int len;
James Chapman090c48d2008-05-19 14:10:01 -07001091 int old_headroom;
1092 int new_headroom;
James Chapman3557baa2007-06-27 15:49:24 -07001093
1094 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
1095 goto abort;
1096
1097 /* Get session and tunnel contexts from the socket */
1098 session = pppol2tp_sock_to_session(sk);
1099 if (session == NULL)
1100 goto abort;
1101
1102 sk_tun = session->tunnel_sock;
1103 if (sk_tun == NULL)
James Chapman24b95682008-06-04 15:54:07 -07001104 goto abort_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07001105 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
1106 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07001107 goto abort_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07001108
1109 /* What header length is configured for this session? */
1110 hdr_len = pppol2tp_l2tp_header_len(session);
1111
1112 /* Check that there's enough headroom in the skb to insert IP,
1113 * UDP and L2TP and PPP headers. If not enough, expand it to
James Chapman090c48d2008-05-19 14:10:01 -07001114 * make room. Adjust truesize.
James Chapman3557baa2007-06-27 15:49:24 -07001115 */
1116 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1117 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
James Chapman090c48d2008-05-19 14:10:01 -07001118 old_headroom = skb_headroom(skb);
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001119 if (skb_cow_head(skb, headroom))
James Chapman24b95682008-06-04 15:54:07 -07001120 goto abort_put_sess_tun;
James Chapman3557baa2007-06-27 15:49:24 -07001121
James Chapman090c48d2008-05-19 14:10:01 -07001122 new_headroom = skb_headroom(skb);
1123 skb_orphan(skb);
1124 skb->truesize += new_headroom - old_headroom;
1125
James Chapman3557baa2007-06-27 15:49:24 -07001126 /* Setup PPP header */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001127 __skb_push(skb, sizeof(ppph));
1128 skb->data[0] = ppph[0];
1129 skb->data[1] = ppph[1];
James Chapman3557baa2007-06-27 15:49:24 -07001130
1131 /* Setup L2TP header */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001132 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
James Chapman3557baa2007-06-27 15:49:24 -07001133
James Chapmanffcebb12008-12-16 01:23:49 -08001134 udp_len = sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len;
1135
James Chapman3557baa2007-06-27 15:49:24 -07001136 /* Setup UDP header */
1137 inet = inet_sk(sk_tun);
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001138 __skb_push(skb, sizeof(*uh));
1139 skb_reset_transport_header(skb);
1140 uh = udp_hdr(skb);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001141 uh->source = inet->inet_sport;
1142 uh->dest = inet->inet_dport;
James Chapmanffcebb12008-12-16 01:23:49 -08001143 uh->len = htons(udp_len);
James Chapman3557baa2007-06-27 15:49:24 -07001144 uh->check = 0;
1145
James Chapman3557baa2007-06-27 15:49:24 -07001146 /* Debug */
1147 if (session->send_seq)
1148 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1149 "%s: send %d bytes, ns=%hu\n", session->name,
1150 data_len, session->ns - 1);
1151 else
1152 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1153 "%s: send %d bytes\n", session->name, data_len);
1154
1155 if (session->debug & PPPOL2TP_MSG_DATA) {
1156 int i;
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001157 unsigned char *datap = skb->data;
James Chapman3557baa2007-06-27 15:49:24 -07001158
1159 printk(KERN_DEBUG "%s: xmit:", session->name);
1160 for (i = 0; i < data_len; i++) {
1161 printk(" %02X", *datap++);
1162 if (i == 31) {
1163 printk(" ...");
1164 break;
1165 }
1166 }
1167 printk("\n");
1168 }
1169
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001170 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1171 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1172 IPSKB_REROUTED);
1173 nf_reset(skb);
Patrick McHardyf77ae932007-07-18 02:04:39 -07001174
James Chapman3557baa2007-06-27 15:49:24 -07001175 /* Get routing info from the tunnel socket */
Eric Dumazetadf30902009-06-02 05:19:30 +00001176 skb_dst_drop(skb);
1177 skb_dst_set(skb, dst_clone(__sk_dst_get(sk_tun)));
James Chapman24b95682008-06-04 15:54:07 -07001178 pppol2tp_skb_set_owner_w(skb, sk_tun);
James Chapman3557baa2007-06-27 15:49:24 -07001179
James Chapmanffcebb12008-12-16 01:23:49 -08001180 /* Calculate UDP checksum if configured to do so */
1181 if (sk_tun->sk_no_check == UDP_CSUM_NOXMIT)
1182 skb->ip_summed = CHECKSUM_NONE;
James Chapman3feec902010-03-16 06:46:31 +00001183 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1184 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
James Chapmanffcebb12008-12-16 01:23:49 -08001185 skb->ip_summed = CHECKSUM_COMPLETE;
1186 csum = skb_checksum(skb, 0, udp_len, 0);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001187 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1188 inet->inet_daddr,
James Chapmanffcebb12008-12-16 01:23:49 -08001189 udp_len, IPPROTO_UDP, csum);
1190 if (uh->check == 0)
1191 uh->check = CSUM_MANGLED_0;
1192 } else {
1193 skb->ip_summed = CHECKSUM_PARTIAL;
1194 skb->csum_start = skb_transport_header(skb) - skb->head;
1195 skb->csum_offset = offsetof(struct udphdr, check);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001196 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1197 inet->inet_daddr,
James Chapmanffcebb12008-12-16 01:23:49 -08001198 udp_len, IPPROTO_UDP, 0);
1199 }
1200
James Chapman3557baa2007-06-27 15:49:24 -07001201 /* Queue the packet to IP for output */
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001202 len = skb->len;
1203 rc = ip_queue_xmit(skb, 1);
James Chapman3557baa2007-06-27 15:49:24 -07001204
1205 /* Update stats */
1206 if (rc >= 0) {
1207 tunnel->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001208 tunnel->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001209 session->stats.tx_packets++;
Patrick McHardy7d4372b2007-07-18 02:04:09 -07001210 session->stats.tx_bytes += len;
James Chapman3557baa2007-06-27 15:49:24 -07001211 } else {
1212 tunnel->stats.tx_errors++;
1213 session->stats.tx_errors++;
1214 }
1215
James Chapman24b95682008-06-04 15:54:07 -07001216 sock_put(sk_tun);
1217 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07001218 return 1;
1219
James Chapman24b95682008-06-04 15:54:07 -07001220abort_put_sess_tun:
1221 sock_put(sk_tun);
1222abort_put_sess:
1223 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07001224abort:
Herbert Xuf3d5e3a2007-09-19 10:46:28 -07001225 /* Free the original skb */
1226 kfree_skb(skb);
1227 return 1;
James Chapman3557baa2007-06-27 15:49:24 -07001228}
1229
1230/*****************************************************************************
1231 * Session (and tunnel control) socket create/destroy.
1232 *****************************************************************************/
1233
1234/* When the tunnel UDP socket is closed, all the attached sockets need to go
1235 * too.
1236 */
1237static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1238{
1239 int hash;
1240 struct hlist_node *walk;
1241 struct hlist_node *tmp;
1242 struct pppol2tp_session *session;
1243 struct sock *sk;
1244
Alexander Beregalov0ee904c2009-04-11 14:50:23 +00001245 BUG_ON(tunnel == NULL);
James Chapman3557baa2007-06-27 15:49:24 -07001246
1247 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1248 "%s: closing all sessions...\n", tunnel->name);
1249
James Chapmancf3752e2008-03-05 18:39:08 -08001250 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001251 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1252again:
1253 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
Jarek Poplawskiec9b6ad2008-03-03 20:49:34 -08001254 struct sk_buff *skb;
1255
James Chapman3557baa2007-06-27 15:49:24 -07001256 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1257
1258 sk = session->sock;
1259
1260 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1261 "%s: closing session\n", session->name);
1262
1263 hlist_del_init(&session->hlist);
1264
1265 /* Since we should hold the sock lock while
1266 * doing any unbinding, we need to release the
1267 * lock we're holding before taking that lock.
1268 * Hold a reference to the sock so it doesn't
1269 * disappear as we're jumping between locks.
1270 */
1271 sock_hold(sk);
James Chapmancf3752e2008-03-05 18:39:08 -08001272 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001273 lock_sock(sk);
1274
1275 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1276 pppox_unbind_sock(sk);
1277 sk->sk_state = PPPOX_DEAD;
1278 sk->sk_state_change(sk);
1279 }
1280
1281 /* Purge any queued data */
1282 skb_queue_purge(&sk->sk_receive_queue);
1283 skb_queue_purge(&sk->sk_write_queue);
Jarek Poplawskiec9b6ad2008-03-03 20:49:34 -08001284 while ((skb = skb_dequeue(&session->reorder_q))) {
1285 kfree_skb(skb);
1286 sock_put(sk);
1287 }
James Chapman3557baa2007-06-27 15:49:24 -07001288
1289 release_sock(sk);
1290 sock_put(sk);
1291
1292 /* Now restart from the beginning of this hash
1293 * chain. We always remove a session from the
1294 * list so we are guaranteed to make forward
1295 * progress.
1296 */
James Chapmancf3752e2008-03-05 18:39:08 -08001297 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001298 goto again;
1299 }
1300 }
James Chapmancf3752e2008-03-05 18:39:08 -08001301 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001302}
1303
1304/* Really kill the tunnel.
1305 * Come here only when all sessions have been cleared from the tunnel.
1306 */
1307static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1308{
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001309 struct pppol2tp_net *pn = pppol2tp_pernet(tunnel->pppol2tp_net);
1310
James Chapman3557baa2007-06-27 15:49:24 -07001311 /* Remove from socket list */
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001312 write_lock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001313 list_del_init(&tunnel->list);
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001314 write_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001315
1316 atomic_dec(&pppol2tp_tunnel_count);
1317 kfree(tunnel);
1318}
1319
1320/* Tunnel UDP socket destruct hook.
1321 * The tunnel context is deleted only when all session sockets have been
1322 * closed.
1323 */
1324static void pppol2tp_tunnel_destruct(struct sock *sk)
1325{
1326 struct pppol2tp_tunnel *tunnel;
1327
James Chapman24b95682008-06-04 15:54:07 -07001328 tunnel = sk->sk_user_data;
James Chapman3557baa2007-06-27 15:49:24 -07001329 if (tunnel == NULL)
1330 goto end;
1331
1332 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1333 "%s: closing...\n", tunnel->name);
1334
1335 /* Close all sessions */
1336 pppol2tp_tunnel_closeall(tunnel);
1337
1338 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1339 (udp_sk(sk))->encap_type = 0;
1340 (udp_sk(sk))->encap_rcv = NULL;
1341
1342 /* Remove hooks into tunnel socket */
1343 tunnel->sock = NULL;
1344 sk->sk_destruct = tunnel->old_sk_destruct;
1345 sk->sk_user_data = NULL;
1346
1347 /* Call original (UDP) socket descructor */
1348 if (sk->sk_destruct != NULL)
1349 (*sk->sk_destruct)(sk);
1350
1351 pppol2tp_tunnel_dec_refcount(tunnel);
1352
1353end:
1354 return;
1355}
1356
1357/* Really kill the session socket. (Called from sock_put() if
1358 * refcnt == 0.)
1359 */
1360static void pppol2tp_session_destruct(struct sock *sk)
1361{
1362 struct pppol2tp_session *session = NULL;
1363
1364 if (sk->sk_user_data != NULL) {
1365 struct pppol2tp_tunnel *tunnel;
1366
James Chapman24b95682008-06-04 15:54:07 -07001367 session = sk->sk_user_data;
James Chapman3557baa2007-06-27 15:49:24 -07001368 if (session == NULL)
1369 goto out;
1370
James Chapman24b95682008-06-04 15:54:07 -07001371 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
1372
James Chapman3557baa2007-06-27 15:49:24 -07001373 /* Don't use pppol2tp_sock_to_tunnel() here to
1374 * get the tunnel context because the tunnel
1375 * socket might have already been closed (its
1376 * sk->sk_user_data will be NULL) so use the
1377 * session's private tunnel ptr instead.
1378 */
1379 tunnel = session->tunnel;
1380 if (tunnel != NULL) {
1381 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1382
1383 /* If session_id is zero, this is a null
1384 * session context, which was created for a
1385 * socket that is being used only to manage
1386 * tunnels.
1387 */
1388 if (session->tunnel_addr.s_session != 0) {
1389 /* Delete the session socket from the
1390 * hash
1391 */
James Chapmancf3752e2008-03-05 18:39:08 -08001392 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001393 hlist_del_init(&session->hlist);
James Chapmancf3752e2008-03-05 18:39:08 -08001394 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001395
1396 atomic_dec(&pppol2tp_session_count);
1397 }
1398
1399 /* This will delete the tunnel context if this
1400 * is the last session on the tunnel.
1401 */
1402 session->tunnel = NULL;
1403 session->tunnel_sock = NULL;
1404 pppol2tp_tunnel_dec_refcount(tunnel);
1405 }
1406 }
1407
1408 kfree(session);
1409out:
1410 return;
1411}
1412
1413/* Called when the PPPoX socket (session) is closed.
1414 */
1415static int pppol2tp_release(struct socket *sock)
1416{
1417 struct sock *sk = sock->sk;
James Chapman199f7d22008-06-04 15:07:32 -07001418 struct pppol2tp_session *session;
James Chapman3557baa2007-06-27 15:49:24 -07001419 int error;
1420
1421 if (!sk)
1422 return 0;
1423
1424 error = -EBADF;
1425 lock_sock(sk);
1426 if (sock_flag(sk, SOCK_DEAD) != 0)
1427 goto error;
1428
1429 pppox_unbind_sock(sk);
1430
1431 /* Signal the death of the socket. */
1432 sk->sk_state = PPPOX_DEAD;
1433 sock_orphan(sk);
1434 sock->sk = NULL;
1435
James Chapman199f7d22008-06-04 15:07:32 -07001436 session = pppol2tp_sock_to_session(sk);
1437
James Chapman3557baa2007-06-27 15:49:24 -07001438 /* Purge any queued data */
1439 skb_queue_purge(&sk->sk_receive_queue);
1440 skb_queue_purge(&sk->sk_write_queue);
James Chapman199f7d22008-06-04 15:07:32 -07001441 if (session != NULL) {
1442 struct sk_buff *skb;
1443 while ((skb = skb_dequeue(&session->reorder_q))) {
1444 kfree_skb(skb);
1445 sock_put(sk);
1446 }
Frédéric Moulinse6358132008-11-28 22:12:02 -08001447 sock_put(sk);
James Chapman199f7d22008-06-04 15:07:32 -07001448 }
James Chapman3557baa2007-06-27 15:49:24 -07001449
1450 release_sock(sk);
1451
1452 /* This will delete the session context via
1453 * pppol2tp_session_destruct() if the socket's refcnt drops to
1454 * zero.
1455 */
1456 sock_put(sk);
1457
1458 return 0;
1459
1460error:
1461 release_sock(sk);
1462 return error;
1463}
1464
1465/* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1466 * sockets attached to it.
1467 */
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001468static struct sock *pppol2tp_prepare_tunnel_socket(struct net *net,
1469 int fd, u16 tunnel_id, int *error)
James Chapman3557baa2007-06-27 15:49:24 -07001470{
1471 int err;
1472 struct socket *sock = NULL;
1473 struct sock *sk;
1474 struct pppol2tp_tunnel *tunnel;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001475 struct pppol2tp_net *pn;
James Chapman3557baa2007-06-27 15:49:24 -07001476 struct sock *ret = NULL;
1477
1478 /* Get the tunnel UDP socket from the fd, which was opened by
1479 * the userspace L2TP daemon.
1480 */
1481 err = -EBADF;
1482 sock = sockfd_lookup(fd, &err);
1483 if (!sock) {
1484 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1485 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1486 tunnel_id, fd, err);
1487 goto err;
1488 }
1489
Herbert Xua14d6ab2007-09-18 13:18:17 -07001490 sk = sock->sk;
1491
James Chapman3557baa2007-06-27 15:49:24 -07001492 /* Quick sanity checks */
Herbert Xua14d6ab2007-09-18 13:18:17 -07001493 err = -EPROTONOSUPPORT;
1494 if (sk->sk_protocol != IPPROTO_UDP) {
James Chapman3557baa2007-06-27 15:49:24 -07001495 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
Herbert Xua14d6ab2007-09-18 13:18:17 -07001496 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1497 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
James Chapman3557baa2007-06-27 15:49:24 -07001498 goto err;
1499 }
1500 err = -EAFNOSUPPORT;
1501 if (sock->ops->family != AF_INET) {
1502 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1503 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1504 tunnel_id, fd, sock->ops->family, AF_INET);
1505 goto err;
1506 }
1507
1508 err = -ENOTCONN;
James Chapman3557baa2007-06-27 15:49:24 -07001509
1510 /* Check if this socket has already been prepped */
1511 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1512 if (tunnel != NULL) {
1513 /* User-data field already set */
1514 err = -EBUSY;
1515 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1516
1517 /* This socket has already been prepped */
1518 ret = tunnel->sock;
1519 goto out;
1520 }
1521
1522 /* This socket is available and needs prepping. Create a new tunnel
1523 * context and init it.
1524 */
1525 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1526 if (sk->sk_user_data == NULL) {
1527 err = -ENOMEM;
1528 goto err;
1529 }
1530
1531 tunnel->magic = L2TP_TUNNEL_MAGIC;
1532 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1533
1534 tunnel->stats.tunnel_id = tunnel_id;
1535 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1536
1537 /* Hook on the tunnel socket destructor so that we can cleanup
1538 * if the tunnel socket goes away.
1539 */
1540 tunnel->old_sk_destruct = sk->sk_destruct;
Julia Lawallc2f379b2009-11-18 08:22:44 +00001541 sk->sk_destruct = pppol2tp_tunnel_destruct;
James Chapman3557baa2007-06-27 15:49:24 -07001542
1543 tunnel->sock = sk;
1544 sk->sk_allocation = GFP_ATOMIC;
1545
1546 /* Misc init */
1547 rwlock_init(&tunnel->hlist_lock);
1548
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001549 /* The net we belong to */
1550 tunnel->pppol2tp_net = net;
1551 pn = pppol2tp_pernet(net);
1552
James Chapman3557baa2007-06-27 15:49:24 -07001553 /* Add tunnel to our list */
1554 INIT_LIST_HEAD(&tunnel->list);
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001555 write_lock_bh(&pn->pppol2tp_tunnel_list_lock);
1556 list_add(&tunnel->list, &pn->pppol2tp_tunnel_list);
1557 write_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001558 atomic_inc(&pppol2tp_tunnel_count);
1559
1560 /* Bump the reference count. The tunnel context is deleted
1561 * only when this drops to zero.
1562 */
1563 pppol2tp_tunnel_inc_refcount(tunnel);
1564
1565 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1566 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1567 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1568
1569 ret = tunnel->sock;
1570
1571 *error = 0;
1572out:
1573 if (sock)
1574 sockfd_put(sock);
1575
1576 return ret;
1577
1578err:
1579 *error = err;
1580 goto out;
1581}
1582
1583static struct proto pppol2tp_sk_proto = {
1584 .name = "PPPOL2TP",
1585 .owner = THIS_MODULE,
1586 .obj_size = sizeof(struct pppox_sock),
1587};
1588
1589/* socket() handler. Initialize a new struct sock.
1590 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001591static int pppol2tp_create(struct net *net, struct socket *sock)
James Chapman3557baa2007-06-27 15:49:24 -07001592{
1593 int error = -ENOMEM;
1594 struct sock *sk;
1595
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001596 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
James Chapman3557baa2007-06-27 15:49:24 -07001597 if (!sk)
1598 goto out;
1599
1600 sock_init_data(sock, sk);
1601
1602 sock->state = SS_UNCONNECTED;
1603 sock->ops = &pppol2tp_ops;
1604
1605 sk->sk_backlog_rcv = pppol2tp_recv_core;
1606 sk->sk_protocol = PX_PROTO_OL2TP;
1607 sk->sk_family = PF_PPPOX;
1608 sk->sk_state = PPPOX_NONE;
1609 sk->sk_type = SOCK_STREAM;
1610 sk->sk_destruct = pppol2tp_session_destruct;
1611
1612 error = 0;
1613
1614out:
1615 return error;
1616}
1617
1618/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1619 */
1620static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1621 int sockaddr_len, int flags)
1622{
1623 struct sock *sk = sock->sk;
1624 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1625 struct pppox_sock *po = pppox_sk(sk);
1626 struct sock *tunnel_sock = NULL;
1627 struct pppol2tp_session *session = NULL;
1628 struct pppol2tp_tunnel *tunnel;
1629 struct dst_entry *dst;
1630 int error = 0;
1631
1632 lock_sock(sk);
1633
1634 error = -EINVAL;
1635 if (sp->sa_protocol != PX_PROTO_OL2TP)
1636 goto end;
1637
1638 /* Check for already bound sockets */
1639 error = -EBUSY;
1640 if (sk->sk_state & PPPOX_CONNECTED)
1641 goto end;
1642
1643 /* We don't supporting rebinding anyway */
1644 error = -EALREADY;
1645 if (sk->sk_user_data)
1646 goto end; /* socket is already attached */
1647
1648 /* Don't bind if s_tunnel is 0 */
1649 error = -EINVAL;
1650 if (sp->pppol2tp.s_tunnel == 0)
1651 goto end;
1652
1653 /* Special case: prepare tunnel socket if s_session and
1654 * d_session is 0. Otherwise look up tunnel using supplied
1655 * tunnel id.
1656 */
1657 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001658 tunnel_sock = pppol2tp_prepare_tunnel_socket(sock_net(sk),
1659 sp->pppol2tp.fd,
James Chapman3557baa2007-06-27 15:49:24 -07001660 sp->pppol2tp.s_tunnel,
1661 &error);
1662 if (tunnel_sock == NULL)
1663 goto end;
1664
1665 tunnel = tunnel_sock->sk_user_data;
1666 } else {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08001667 tunnel = pppol2tp_tunnel_find(sock_net(sk), sp->pppol2tp.s_tunnel);
James Chapman3557baa2007-06-27 15:49:24 -07001668
1669 /* Error if we can't find the tunnel */
1670 error = -ENOENT;
1671 if (tunnel == NULL)
1672 goto end;
1673
1674 tunnel_sock = tunnel->sock;
1675 }
1676
1677 /* Check that this session doesn't already exist */
1678 error = -EEXIST;
1679 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1680 if (session != NULL)
1681 goto end;
1682
1683 /* Allocate and initialize a new session context. */
1684 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1685 if (session == NULL) {
1686 error = -ENOMEM;
1687 goto end;
1688 }
1689
1690 skb_queue_head_init(&session->reorder_q);
1691
1692 session->magic = L2TP_SESSION_MAGIC;
1693 session->owner = current->pid;
1694 session->sock = sk;
1695 session->tunnel = tunnel;
1696 session->tunnel_sock = tunnel_sock;
1697 session->tunnel_addr = sp->pppol2tp;
1698 sprintf(&session->name[0], "sess %hu/%hu",
1699 session->tunnel_addr.s_tunnel,
1700 session->tunnel_addr.s_session);
1701
1702 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1703 session->stats.session_id = session->tunnel_addr.s_session;
1704
1705 INIT_HLIST_NODE(&session->hlist);
1706
1707 /* Inherit debug options from tunnel */
1708 session->debug = tunnel->debug;
1709
1710 /* Default MTU must allow space for UDP/L2TP/PPP
1711 * headers.
1712 */
1713 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1714
1715 /* If PMTU discovery was enabled, use the MTU that was discovered */
1716 dst = sk_dst_get(sk);
1717 if (dst != NULL) {
1718 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1719 if (pmtu != 0)
1720 session->mtu = session->mru = pmtu -
1721 PPPOL2TP_HEADER_OVERHEAD;
1722 dst_release(dst);
1723 }
1724
1725 /* Special case: if source & dest session_id == 0x0000, this socket is
1726 * being created to manage the tunnel. Don't add the session to the
1727 * session hash list, just set up the internal context for use by
1728 * ioctl() and sockopt() handlers.
1729 */
1730 if ((session->tunnel_addr.s_session == 0) &&
1731 (session->tunnel_addr.d_session == 0)) {
1732 error = 0;
1733 sk->sk_user_data = session;
1734 goto out_no_ppp;
1735 }
1736
1737 /* Get tunnel context from the tunnel socket */
1738 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1739 if (tunnel == NULL) {
1740 error = -EBADF;
1741 goto end;
1742 }
1743
1744 /* Right now, because we don't have a way to push the incoming skb's
1745 * straight through the UDP layer, the only header we need to worry
1746 * about is the L2TP header. This size is different depending on
1747 * whether sequence numbers are enabled for the data channel.
1748 */
1749 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1750
1751 po->chan.private = sk;
1752 po->chan.ops = &pppol2tp_chan_ops;
1753 po->chan.mtu = session->mtu;
1754
Cyrill Gorcunovf5882c32009-01-21 15:55:40 -08001755 error = ppp_register_net_channel(sock_net(sk), &po->chan);
James Chapman3557baa2007-06-27 15:49:24 -07001756 if (error)
James Chapman24b95682008-06-04 15:54:07 -07001757 goto end_put_tun;
James Chapman3557baa2007-06-27 15:49:24 -07001758
1759 /* This is how we get the session context from the socket. */
1760 sk->sk_user_data = session;
1761
1762 /* Add session to the tunnel's hash list */
James Chapmancf3752e2008-03-05 18:39:08 -08001763 write_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001764 hlist_add_head(&session->hlist,
1765 pppol2tp_session_id_hash(tunnel,
1766 session->tunnel_addr.s_session));
James Chapmancf3752e2008-03-05 18:39:08 -08001767 write_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07001768
1769 atomic_inc(&pppol2tp_session_count);
1770
1771out_no_ppp:
1772 pppol2tp_tunnel_inc_refcount(tunnel);
1773 sk->sk_state = PPPOX_CONNECTED;
1774 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1775 "%s: created\n", session->name);
1776
James Chapman24b95682008-06-04 15:54:07 -07001777end_put_tun:
1778 sock_put(tunnel_sock);
James Chapman3557baa2007-06-27 15:49:24 -07001779end:
1780 release_sock(sk);
1781
Julia Lawall5dc474d2008-05-12 15:43:46 -07001782 if (error != 0) {
1783 if (session)
1784 PRINTK(session->debug,
1785 PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1786 "%s: connect failed: %d\n",
1787 session->name, error);
1788 else
1789 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1790 "connect failed: %d\n", error);
1791 }
James Chapman3557baa2007-06-27 15:49:24 -07001792
1793 return error;
1794}
1795
1796/* getname() support.
1797 */
1798static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1799 int *usockaddr_len, int peer)
1800{
1801 int len = sizeof(struct sockaddr_pppol2tp);
1802 struct sockaddr_pppol2tp sp;
1803 int error = 0;
1804 struct pppol2tp_session *session;
1805
1806 error = -ENOTCONN;
1807 if (sock->sk->sk_state != PPPOX_CONNECTED)
1808 goto end;
1809
1810 session = pppol2tp_sock_to_session(sock->sk);
1811 if (session == NULL) {
1812 error = -EBADF;
1813 goto end;
1814 }
1815
1816 sp.sa_family = AF_PPPOX;
1817 sp.sa_protocol = PX_PROTO_OL2TP;
1818 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1819 sizeof(struct pppol2tp_addr));
1820
1821 memcpy(uaddr, &sp, len);
1822
1823 *usockaddr_len = len;
1824
1825 error = 0;
James Chapman24b95682008-06-04 15:54:07 -07001826 sock_put(sock->sk);
James Chapman3557baa2007-06-27 15:49:24 -07001827
1828end:
1829 return error;
1830}
1831
1832/****************************************************************************
1833 * ioctl() handlers.
1834 *
1835 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1836 * sockets. However, in order to control kernel tunnel features, we allow
1837 * userspace to create a special "tunnel" PPPoX socket which is used for
1838 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1839 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1840 * calls.
1841 ****************************************************************************/
1842
1843/* Session ioctl helper.
1844 */
1845static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1846 unsigned int cmd, unsigned long arg)
1847{
1848 struct ifreq ifr;
1849 int err = 0;
1850 struct sock *sk = session->sock;
1851 int val = (int) arg;
1852
1853 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1854 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1855 session->name, cmd, arg);
1856
1857 sock_hold(sk);
1858
1859 switch (cmd) {
1860 case SIOCGIFMTU:
1861 err = -ENXIO;
1862 if (!(sk->sk_state & PPPOX_CONNECTED))
1863 break;
1864
1865 err = -EFAULT;
1866 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1867 break;
1868 ifr.ifr_mtu = session->mtu;
1869 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1870 break;
1871
1872 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1873 "%s: get mtu=%d\n", session->name, session->mtu);
1874 err = 0;
1875 break;
1876
1877 case SIOCSIFMTU:
1878 err = -ENXIO;
1879 if (!(sk->sk_state & PPPOX_CONNECTED))
1880 break;
1881
1882 err = -EFAULT;
1883 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1884 break;
1885
1886 session->mtu = ifr.ifr_mtu;
1887
1888 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1889 "%s: set mtu=%d\n", session->name, session->mtu);
1890 err = 0;
1891 break;
1892
1893 case PPPIOCGMRU:
1894 err = -ENXIO;
1895 if (!(sk->sk_state & PPPOX_CONNECTED))
1896 break;
1897
1898 err = -EFAULT;
1899 if (put_user(session->mru, (int __user *) arg))
1900 break;
1901
1902 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1903 "%s: get mru=%d\n", session->name, session->mru);
1904 err = 0;
1905 break;
1906
1907 case PPPIOCSMRU:
1908 err = -ENXIO;
1909 if (!(sk->sk_state & PPPOX_CONNECTED))
1910 break;
1911
1912 err = -EFAULT;
1913 if (get_user(val,(int __user *) arg))
1914 break;
1915
1916 session->mru = val;
1917 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1918 "%s: set mru=%d\n", session->name, session->mru);
1919 err = 0;
1920 break;
1921
1922 case PPPIOCGFLAGS:
1923 err = -EFAULT;
1924 if (put_user(session->flags, (int __user *) arg))
1925 break;
1926
1927 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1928 "%s: get flags=%d\n", session->name, session->flags);
1929 err = 0;
1930 break;
1931
1932 case PPPIOCSFLAGS:
1933 err = -EFAULT;
1934 if (get_user(val, (int __user *) arg))
1935 break;
1936 session->flags = val;
1937 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1938 "%s: set flags=%d\n", session->name, session->flags);
1939 err = 0;
1940 break;
1941
1942 case PPPIOCGL2TPSTATS:
1943 err = -ENXIO;
1944 if (!(sk->sk_state & PPPOX_CONNECTED))
1945 break;
1946
1947 if (copy_to_user((void __user *) arg, &session->stats,
1948 sizeof(session->stats)))
1949 break;
1950 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1951 "%s: get L2TP stats\n", session->name);
1952 err = 0;
1953 break;
1954
1955 default:
1956 err = -ENOSYS;
1957 break;
1958 }
1959
1960 sock_put(sk);
1961
1962 return err;
1963}
1964
1965/* Tunnel ioctl helper.
1966 *
1967 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1968 * specifies a session_id, the session ioctl handler is called. This allows an
1969 * application to retrieve session stats via a tunnel socket.
1970 */
1971static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1972 unsigned int cmd, unsigned long arg)
1973{
1974 int err = 0;
1975 struct sock *sk = tunnel->sock;
1976 struct pppol2tp_ioc_stats stats_req;
1977
1978 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1979 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1980 cmd, arg);
1981
1982 sock_hold(sk);
1983
1984 switch (cmd) {
1985 case PPPIOCGL2TPSTATS:
1986 err = -ENXIO;
1987 if (!(sk->sk_state & PPPOX_CONNECTED))
1988 break;
1989
1990 if (copy_from_user(&stats_req, (void __user *) arg,
1991 sizeof(stats_req))) {
1992 err = -EFAULT;
1993 break;
1994 }
1995 if (stats_req.session_id != 0) {
1996 /* resend to session ioctl handler */
1997 struct pppol2tp_session *session =
1998 pppol2tp_session_find(tunnel, stats_req.session_id);
1999 if (session != NULL)
2000 err = pppol2tp_session_ioctl(session, cmd, arg);
2001 else
2002 err = -EBADR;
2003 break;
2004 }
2005#ifdef CONFIG_XFRM
2006 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
2007#endif
2008 if (copy_to_user((void __user *) arg, &tunnel->stats,
2009 sizeof(tunnel->stats))) {
2010 err = -EFAULT;
2011 break;
2012 }
2013 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2014 "%s: get L2TP stats\n", tunnel->name);
2015 err = 0;
2016 break;
2017
2018 default:
2019 err = -ENOSYS;
2020 break;
2021 }
2022
2023 sock_put(sk);
2024
2025 return err;
2026}
2027
2028/* Main ioctl() handler.
2029 * Dispatch to tunnel or session helpers depending on the socket.
2030 */
2031static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
2032 unsigned long arg)
2033{
2034 struct sock *sk = sock->sk;
2035 struct pppol2tp_session *session;
2036 struct pppol2tp_tunnel *tunnel;
2037 int err;
2038
2039 if (!sk)
2040 return 0;
2041
2042 err = -EBADF;
2043 if (sock_flag(sk, SOCK_DEAD) != 0)
2044 goto end;
2045
2046 err = -ENOTCONN;
2047 if ((sk->sk_user_data == NULL) ||
2048 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
2049 goto end;
2050
2051 /* Get session context from the socket */
2052 err = -EBADF;
2053 session = pppol2tp_sock_to_session(sk);
2054 if (session == NULL)
2055 goto end;
2056
2057 /* Special case: if session's session_id is zero, treat ioctl as a
2058 * tunnel ioctl
2059 */
2060 if ((session->tunnel_addr.s_session == 0) &&
2061 (session->tunnel_addr.d_session == 0)) {
2062 err = -EBADF;
2063 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2064 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07002065 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002066
2067 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
James Chapman24b95682008-06-04 15:54:07 -07002068 sock_put(session->tunnel_sock);
2069 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002070 }
2071
2072 err = pppol2tp_session_ioctl(session, cmd, arg);
2073
James Chapman24b95682008-06-04 15:54:07 -07002074end_put_sess:
2075 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07002076end:
2077 return err;
2078}
2079
2080/*****************************************************************************
2081 * setsockopt() / getsockopt() support.
2082 *
2083 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2084 * sockets. In order to control kernel tunnel features, we allow userspace to
2085 * create a special "tunnel" PPPoX socket which is used for control only.
2086 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2087 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2088 *****************************************************************************/
2089
2090/* Tunnel setsockopt() helper.
2091 */
2092static int pppol2tp_tunnel_setsockopt(struct sock *sk,
2093 struct pppol2tp_tunnel *tunnel,
2094 int optname, int val)
2095{
2096 int err = 0;
2097
2098 switch (optname) {
2099 case PPPOL2TP_SO_DEBUG:
2100 tunnel->debug = val;
2101 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2102 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
2103 break;
2104
2105 default:
2106 err = -ENOPROTOOPT;
2107 break;
2108 }
2109
2110 return err;
2111}
2112
2113/* Session setsockopt helper.
2114 */
2115static int pppol2tp_session_setsockopt(struct sock *sk,
2116 struct pppol2tp_session *session,
2117 int optname, int val)
2118{
2119 int err = 0;
2120
2121 switch (optname) {
2122 case PPPOL2TP_SO_RECVSEQ:
2123 if ((val != 0) && (val != 1)) {
2124 err = -EINVAL;
2125 break;
2126 }
2127 session->recv_seq = val ? -1 : 0;
2128 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2129 "%s: set recv_seq=%d\n", session->name,
2130 session->recv_seq);
2131 break;
2132
2133 case PPPOL2TP_SO_SENDSEQ:
2134 if ((val != 0) && (val != 1)) {
2135 err = -EINVAL;
2136 break;
2137 }
2138 session->send_seq = val ? -1 : 0;
2139 {
2140 struct sock *ssk = session->sock;
2141 struct pppox_sock *po = pppox_sk(ssk);
2142 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
2143 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
2144 }
2145 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2146 "%s: set send_seq=%d\n", session->name, session->send_seq);
2147 break;
2148
2149 case PPPOL2TP_SO_LNSMODE:
2150 if ((val != 0) && (val != 1)) {
2151 err = -EINVAL;
2152 break;
2153 }
2154 session->lns_mode = val ? -1 : 0;
2155 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2156 "%s: set lns_mode=%d\n", session->name,
2157 session->lns_mode);
2158 break;
2159
2160 case PPPOL2TP_SO_DEBUG:
2161 session->debug = val;
2162 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2163 "%s: set debug=%x\n", session->name, session->debug);
2164 break;
2165
2166 case PPPOL2TP_SO_REORDERTO:
2167 session->reorder_timeout = msecs_to_jiffies(val);
2168 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2169 "%s: set reorder_timeout=%d\n", session->name,
2170 session->reorder_timeout);
2171 break;
2172
2173 default:
2174 err = -ENOPROTOOPT;
2175 break;
2176 }
2177
2178 return err;
2179}
2180
2181/* Main setsockopt() entry point.
2182 * Does API checks, then calls either the tunnel or session setsockopt
2183 * handler, according to whether the PPPoL2TP socket is a for a regular
2184 * session or the special tunnel type.
2185 */
2186static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002187 char __user *optval, unsigned int optlen)
James Chapman3557baa2007-06-27 15:49:24 -07002188{
2189 struct sock *sk = sock->sk;
2190 struct pppol2tp_session *session = sk->sk_user_data;
2191 struct pppol2tp_tunnel *tunnel;
2192 int val;
2193 int err;
2194
2195 if (level != SOL_PPPOL2TP)
2196 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2197
2198 if (optlen < sizeof(int))
2199 return -EINVAL;
2200
2201 if (get_user(val, (int __user *)optval))
2202 return -EFAULT;
2203
2204 err = -ENOTCONN;
2205 if (sk->sk_user_data == NULL)
2206 goto end;
2207
2208 /* Get session context from the socket */
2209 err = -EBADF;
2210 session = pppol2tp_sock_to_session(sk);
2211 if (session == NULL)
2212 goto end;
2213
2214 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2215 */
2216 if ((session->tunnel_addr.s_session == 0) &&
2217 (session->tunnel_addr.d_session == 0)) {
2218 err = -EBADF;
2219 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2220 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07002221 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002222
2223 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
James Chapman24b95682008-06-04 15:54:07 -07002224 sock_put(session->tunnel_sock);
James Chapman3557baa2007-06-27 15:49:24 -07002225 } else
2226 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2227
2228 err = 0;
2229
James Chapman24b95682008-06-04 15:54:07 -07002230end_put_sess:
2231 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07002232end:
2233 return err;
2234}
2235
2236/* Tunnel getsockopt helper. Called with sock locked.
2237 */
2238static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2239 struct pppol2tp_tunnel *tunnel,
Al Viroaf3b1622007-07-26 17:35:09 +01002240 int optname, int *val)
James Chapman3557baa2007-06-27 15:49:24 -07002241{
2242 int err = 0;
2243
2244 switch (optname) {
2245 case PPPOL2TP_SO_DEBUG:
2246 *val = tunnel->debug;
2247 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2248 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2249 break;
2250
2251 default:
2252 err = -ENOPROTOOPT;
2253 break;
2254 }
2255
2256 return err;
2257}
2258
2259/* Session getsockopt helper. Called with sock locked.
2260 */
2261static int pppol2tp_session_getsockopt(struct sock *sk,
2262 struct pppol2tp_session *session,
Al Viroaf3b1622007-07-26 17:35:09 +01002263 int optname, int *val)
James Chapman3557baa2007-06-27 15:49:24 -07002264{
2265 int err = 0;
2266
2267 switch (optname) {
2268 case PPPOL2TP_SO_RECVSEQ:
2269 *val = session->recv_seq;
2270 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2271 "%s: get recv_seq=%d\n", session->name, *val);
2272 break;
2273
2274 case PPPOL2TP_SO_SENDSEQ:
2275 *val = session->send_seq;
2276 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2277 "%s: get send_seq=%d\n", session->name, *val);
2278 break;
2279
2280 case PPPOL2TP_SO_LNSMODE:
2281 *val = session->lns_mode;
2282 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2283 "%s: get lns_mode=%d\n", session->name, *val);
2284 break;
2285
2286 case PPPOL2TP_SO_DEBUG:
2287 *val = session->debug;
2288 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2289 "%s: get debug=%d\n", session->name, *val);
2290 break;
2291
2292 case PPPOL2TP_SO_REORDERTO:
2293 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2294 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2295 "%s: get reorder_timeout=%d\n", session->name, *val);
2296 break;
2297
2298 default:
2299 err = -ENOPROTOOPT;
2300 }
2301
2302 return err;
2303}
2304
2305/* Main getsockopt() entry point.
2306 * Does API checks, then calls either the tunnel or session getsockopt
2307 * handler, according to whether the PPPoX socket is a for a regular session
2308 * or the special tunnel type.
2309 */
2310static int pppol2tp_getsockopt(struct socket *sock, int level,
2311 int optname, char __user *optval, int __user *optlen)
2312{
2313 struct sock *sk = sock->sk;
2314 struct pppol2tp_session *session = sk->sk_user_data;
2315 struct pppol2tp_tunnel *tunnel;
2316 int val, len;
2317 int err;
2318
2319 if (level != SOL_PPPOL2TP)
2320 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2321
2322 if (get_user(len, (int __user *) optlen))
2323 return -EFAULT;
2324
2325 len = min_t(unsigned int, len, sizeof(int));
2326
2327 if (len < 0)
2328 return -EINVAL;
2329
2330 err = -ENOTCONN;
2331 if (sk->sk_user_data == NULL)
2332 goto end;
2333
2334 /* Get the session context */
2335 err = -EBADF;
2336 session = pppol2tp_sock_to_session(sk);
2337 if (session == NULL)
2338 goto end;
2339
2340 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2341 if ((session->tunnel_addr.s_session == 0) &&
2342 (session->tunnel_addr.d_session == 0)) {
2343 err = -EBADF;
2344 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2345 if (tunnel == NULL)
James Chapman24b95682008-06-04 15:54:07 -07002346 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002347
2348 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
James Chapman24b95682008-06-04 15:54:07 -07002349 sock_put(session->tunnel_sock);
James Chapman3557baa2007-06-27 15:49:24 -07002350 } else
2351 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2352
2353 err = -EFAULT;
2354 if (put_user(len, (int __user *) optlen))
James Chapman24b95682008-06-04 15:54:07 -07002355 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002356
2357 if (copy_to_user((void __user *) optval, &val, len))
James Chapman24b95682008-06-04 15:54:07 -07002358 goto end_put_sess;
James Chapman3557baa2007-06-27 15:49:24 -07002359
2360 err = 0;
James Chapman24b95682008-06-04 15:54:07 -07002361
2362end_put_sess:
2363 sock_put(sk);
James Chapman3557baa2007-06-27 15:49:24 -07002364end:
2365 return err;
2366}
2367
2368/*****************************************************************************
2369 * /proc filesystem for debug
2370 *****************************************************************************/
2371
2372#ifdef CONFIG_PROC_FS
2373
2374#include <linux/seq_file.h>
2375
2376struct pppol2tp_seq_data {
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002377 struct seq_net_private p;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002378 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2379 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
James Chapman3557baa2007-06-27 15:49:24 -07002380};
2381
2382static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2383{
2384 struct pppol2tp_session *session = NULL;
2385 struct hlist_node *walk;
2386 int found = 0;
2387 int next = 0;
2388 int i;
2389
James Chapmancf3752e2008-03-05 18:39:08 -08002390 read_lock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07002391 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2392 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2393 if (curr == NULL) {
2394 found = 1;
2395 goto out;
2396 }
2397 if (session == curr) {
2398 next = 1;
2399 continue;
2400 }
2401 if (next) {
2402 found = 1;
2403 goto out;
2404 }
2405 }
2406 }
2407out:
James Chapmancf3752e2008-03-05 18:39:08 -08002408 read_unlock_bh(&tunnel->hlist_lock);
James Chapman3557baa2007-06-27 15:49:24 -07002409 if (!found)
2410 session = NULL;
2411
2412 return session;
2413}
2414
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002415static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_net *pn,
2416 struct pppol2tp_tunnel *curr)
James Chapman3557baa2007-06-27 15:49:24 -07002417{
2418 struct pppol2tp_tunnel *tunnel = NULL;
2419
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002420 read_lock_bh(&pn->pppol2tp_tunnel_list_lock);
2421 if (list_is_last(&curr->list, &pn->pppol2tp_tunnel_list)) {
James Chapman3557baa2007-06-27 15:49:24 -07002422 goto out;
2423 }
2424 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2425out:
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002426 read_unlock_bh(&pn->pppol2tp_tunnel_list_lock);
James Chapman3557baa2007-06-27 15:49:24 -07002427
2428 return tunnel;
2429}
2430
2431static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2432{
2433 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002434 struct pppol2tp_net *pn;
James Chapman3557baa2007-06-27 15:49:24 -07002435 loff_t pos = *offs;
2436
2437 if (!pos)
2438 goto out;
2439
2440 BUG_ON(m->private == NULL);
2441 pd = m->private;
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002442 pn = pppol2tp_pernet(seq_file_net(m));
James Chapman3557baa2007-06-27 15:49:24 -07002443
2444 if (pd->tunnel == NULL) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002445 if (!list_empty(&pn->pppol2tp_tunnel_list))
2446 pd->tunnel = list_entry(pn->pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
James Chapman3557baa2007-06-27 15:49:24 -07002447 } else {
2448 pd->session = next_session(pd->tunnel, pd->session);
2449 if (pd->session == NULL) {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002450 pd->tunnel = next_tunnel(pn, pd->tunnel);
James Chapman3557baa2007-06-27 15:49:24 -07002451 }
2452 }
2453
2454 /* NULL tunnel and session indicates end of list */
2455 if ((pd->tunnel == NULL) && (pd->session == NULL))
2456 pd = NULL;
2457
2458out:
2459 return pd;
2460}
2461
2462static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2463{
2464 (*pos)++;
2465 return NULL;
2466}
2467
2468static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2469{
2470 /* nothing to do */
2471}
2472
2473static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2474{
2475 struct pppol2tp_tunnel *tunnel = v;
2476
2477 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2478 tunnel->name,
2479 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2480 atomic_read(&tunnel->ref_count) - 1);
2481 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2482 tunnel->debug,
Andrew Morton0efeaa32008-02-09 23:17:51 -08002483 (unsigned long long)tunnel->stats.tx_packets,
2484 (unsigned long long)tunnel->stats.tx_bytes,
2485 (unsigned long long)tunnel->stats.tx_errors,
2486 (unsigned long long)tunnel->stats.rx_packets,
2487 (unsigned long long)tunnel->stats.rx_bytes,
2488 (unsigned long long)tunnel->stats.rx_errors);
James Chapman3557baa2007-06-27 15:49:24 -07002489}
2490
2491static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2492{
2493 struct pppol2tp_session *session = v;
2494
2495 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2496 "%04X/%04X %d %c\n",
2497 session->name,
2498 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2499 ntohs(session->tunnel_addr.addr.sin_port),
2500 session->tunnel_addr.s_tunnel,
2501 session->tunnel_addr.s_session,
2502 session->tunnel_addr.d_tunnel,
2503 session->tunnel_addr.d_session,
2504 session->sock->sk_state,
2505 (session == session->sock->sk_user_data) ?
2506 'Y' : 'N');
2507 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2508 session->mtu, session->mru,
2509 session->recv_seq ? 'R' : '-',
2510 session->send_seq ? 'S' : '-',
2511 session->lns_mode ? "LNS" : "LAC",
2512 session->debug,
2513 jiffies_to_msecs(session->reorder_timeout));
2514 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2515 session->nr, session->ns,
Andrew Morton0efeaa32008-02-09 23:17:51 -08002516 (unsigned long long)session->stats.tx_packets,
2517 (unsigned long long)session->stats.tx_bytes,
2518 (unsigned long long)session->stats.tx_errors,
2519 (unsigned long long)session->stats.rx_packets,
2520 (unsigned long long)session->stats.rx_bytes,
2521 (unsigned long long)session->stats.rx_errors);
James Chapman3557baa2007-06-27 15:49:24 -07002522}
2523
2524static int pppol2tp_seq_show(struct seq_file *m, void *v)
2525{
2526 struct pppol2tp_seq_data *pd = v;
2527
2528 /* display header on line 1 */
2529 if (v == SEQ_START_TOKEN) {
2530 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2531 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2532 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2533 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2534 "dest-tid/sid state user-data-ok\n");
2535 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2536 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2537 goto out;
2538 }
2539
2540 /* Show the tunnel or session context.
2541 */
2542 if (pd->session == NULL)
2543 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2544 else
2545 pppol2tp_seq_session_show(m, pd->session);
2546
2547out:
2548 return 0;
2549}
2550
Jan Engelhardt4101dec2009-01-14 13:52:18 -08002551static const struct seq_operations pppol2tp_seq_ops = {
James Chapman3557baa2007-06-27 15:49:24 -07002552 .start = pppol2tp_seq_start,
2553 .next = pppol2tp_seq_next,
2554 .stop = pppol2tp_seq_stop,
2555 .show = pppol2tp_seq_show,
2556};
2557
2558/* Called when our /proc file is opened. We allocate data for use when
2559 * iterating our tunnel / session contexts and store it in the private
2560 * data of the seq_file.
2561 */
2562static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2563{
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002564 return seq_open_net(inode, file, &pppol2tp_seq_ops,
2565 sizeof(struct pppol2tp_seq_data));
James Chapman3557baa2007-06-27 15:49:24 -07002566}
2567
Jan Engelhardt4101dec2009-01-14 13:52:18 -08002568static const struct file_operations pppol2tp_proc_fops = {
James Chapman3557baa2007-06-27 15:49:24 -07002569 .owner = THIS_MODULE,
2570 .open = pppol2tp_proc_open,
2571 .read = seq_read,
2572 .llseek = seq_lseek,
Alexey Dobriyancbec6602009-01-26 21:10:08 -08002573 .release = seq_release_net,
James Chapman3557baa2007-06-27 15:49:24 -07002574};
2575
James Chapman3557baa2007-06-27 15:49:24 -07002576#endif /* CONFIG_PROC_FS */
2577
2578/*****************************************************************************
2579 * Init and cleanup
2580 *****************************************************************************/
2581
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002582static const struct proto_ops pppol2tp_ops = {
James Chapman3557baa2007-06-27 15:49:24 -07002583 .family = AF_PPPOX,
2584 .owner = THIS_MODULE,
2585 .release = pppol2tp_release,
2586 .bind = sock_no_bind,
2587 .connect = pppol2tp_connect,
2588 .socketpair = sock_no_socketpair,
2589 .accept = sock_no_accept,
2590 .getname = pppol2tp_getname,
2591 .poll = datagram_poll,
2592 .listen = sock_no_listen,
2593 .shutdown = sock_no_shutdown,
2594 .setsockopt = pppol2tp_setsockopt,
2595 .getsockopt = pppol2tp_getsockopt,
2596 .sendmsg = pppol2tp_sendmsg,
2597 .recvmsg = pppol2tp_recvmsg,
2598 .mmap = sock_no_mmap,
2599 .ioctl = pppox_ioctl,
2600};
2601
2602static struct pppox_proto pppol2tp_proto = {
2603 .create = pppol2tp_create,
2604 .ioctl = pppol2tp_ioctl
2605};
2606
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002607static __net_init int pppol2tp_init_net(struct net *net)
2608{
Eric W. Biedermanaaab3402009-11-29 15:46:11 +00002609 struct pppol2tp_net *pn = pppol2tp_pernet(net);
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002610 struct proc_dir_entry *pde;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002611
2612 INIT_LIST_HEAD(&pn->pppol2tp_tunnel_list);
2613 rwlock_init(&pn->pppol2tp_tunnel_list_lock);
2614
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002615 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
2616#ifdef CONFIG_PROC_FS
Eric W. Biedermanaaab3402009-11-29 15:46:11 +00002617 if (!pde)
2618 return -ENOMEM;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002619#endif
2620
2621 return 0;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002622}
2623
2624static __net_exit void pppol2tp_exit_net(struct net *net)
2625{
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002626 proc_net_remove(net, "pppol2tp");
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002627}
2628
Alexey Dobriyan00129852009-02-09 18:05:16 -08002629static struct pernet_operations pppol2tp_net_ops = {
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002630 .init = pppol2tp_init_net,
2631 .exit = pppol2tp_exit_net,
Eric W. Biedermanaaab3402009-11-29 15:46:11 +00002632 .id = &pppol2tp_net_id,
2633 .size = sizeof(struct pppol2tp_net),
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002634};
2635
James Chapman3557baa2007-06-27 15:49:24 -07002636static int __init pppol2tp_init(void)
2637{
2638 int err;
2639
2640 err = proto_register(&pppol2tp_sk_proto, 0);
2641 if (err)
2642 goto out;
2643 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2644 if (err)
2645 goto out_unregister_pppol2tp_proto;
2646
Eric W. Biedermanaaab3402009-11-29 15:46:11 +00002647 err = register_pernet_device(&pppol2tp_net_ops);
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002648 if (err)
James Chapman3557baa2007-06-27 15:49:24 -07002649 goto out_unregister_pppox_proto;
Cyrill Gorcunov4e9fb802009-01-21 15:55:15 -08002650
James Chapman3557baa2007-06-27 15:49:24 -07002651 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2652 PPPOL2TP_DRV_VERSION);
2653
2654out:
2655 return err;
James Chapman3557baa2007-06-27 15:49:24 -07002656out_unregister_pppox_proto:
2657 unregister_pppox_proto(PX_PROTO_OL2TP);
2658out_unregister_pppol2tp_proto:
2659 proto_unregister(&pppol2tp_sk_proto);
2660 goto out;
2661}
2662
2663static void __exit pppol2tp_exit(void)
2664{
2665 unregister_pppox_proto(PX_PROTO_OL2TP);
Eric W. Biedermanaaab3402009-11-29 15:46:11 +00002666 unregister_pernet_device(&pppol2tp_net_ops);
James Chapman3557baa2007-06-27 15:49:24 -07002667 proto_unregister(&pppol2tp_sk_proto);
2668}
2669
2670module_init(pppol2tp_init);
2671module_exit(pppol2tp_exit);
2672
Joe Perches24500222007-11-19 17:48:28 -08002673MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
James Chapman3557baa2007-06-27 15:49:24 -07002674 "James Chapman <jchapman@katalix.com>");
2675MODULE_DESCRIPTION("PPP over L2TP over UDP");
2676MODULE_LICENSE("GPL");
2677MODULE_VERSION(PPPOL2TP_DRV_VERSION);