blob: aa1a9d44c107fd0bd446de521adbc2f2d16328a9 [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*
2 * L2TP core.
3 *
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5 *
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
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 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
20
Joe Perchesa4ca44f2012-05-16 09:55:56 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
James Chapmanfd558d12010-04-02 06:18:33 +000023#include <linux/module.h>
24#include <linux/string.h>
25#include <linux/list.h>
James Chapmane02d4942010-04-02 06:19:16 +000026#include <linux/rculist.h>
James Chapmanfd558d12010-04-02 06:18:33 +000027#include <linux/uaccess.h>
28
29#include <linux/kernel.h>
30#include <linux/spinlock.h>
31#include <linux/kthread.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/errno.h>
35#include <linux/jiffies.h>
36
37#include <linux/netdevice.h>
38#include <linux/net.h>
39#include <linux/inetdevice.h>
40#include <linux/skbuff.h>
41#include <linux/init.h>
James Chapman0d767512010-04-02 06:19:00 +000042#include <linux/in.h>
James Chapmanfd558d12010-04-02 06:18:33 +000043#include <linux/ip.h>
44#include <linux/udp.h>
James Chapman0d767512010-04-02 06:19:00 +000045#include <linux/l2tp.h>
James Chapmanfd558d12010-04-02 06:18:33 +000046#include <linux/hash.h>
47#include <linux/sort.h>
48#include <linux/file.h>
49#include <linux/nsproxy.h>
50#include <net/net_namespace.h>
51#include <net/netns/generic.h>
52#include <net/dst.h>
53#include <net/ip.h>
54#include <net/udp.h>
James Chapman309795f2010-04-02 06:19:10 +000055#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000056#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000057#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000058#include <net/inet6_connection_sock.h>
59#include <net/inet_ecn.h>
60#include <net/ip6_route.h>
David S. Millerd499bd22012-04-30 13:21:28 -040061#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000062
63#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000065
66#include "l2tp_core.h"
67
68#define L2TP_DRV_VERSION "V2.0"
69
70/* L2TP header constants */
71#define L2TP_HDRFLAG_T 0x8000
72#define L2TP_HDRFLAG_L 0x4000
73#define L2TP_HDRFLAG_S 0x0800
74#define L2TP_HDRFLAG_O 0x0200
75#define L2TP_HDRFLAG_P 0x0100
76
77#define L2TP_HDR_VER_MASK 0x000F
78#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000079#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000080
81/* L2TPv3 default L2-specific sublayer */
82#define L2TP_SLFLAG_S 0x40000000
83#define L2TP_SL_SEQ_MASK 0x00ffffff
84
85#define L2TP_HDR_SIZE_SEQ 10
86#define L2TP_HDR_SIZE_NOSEQ 6
87
88/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0
90
James Chapmanfd558d12010-04-02 06:18:33 +000091/* Private data stored for received packets in the skb.
92 */
93struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000094 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000095 u16 has_seq;
96 u16 length;
97 unsigned long expires;
98};
99
100#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101
102static atomic_t l2tp_tunnel_count;
103static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000104static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000105
106/* per-net private data for this module */
107static unsigned int l2tp_net_id;
108struct l2tp_net {
109 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000110 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000111 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000112 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000113};
114
stephen hemmingerfc130842010-10-21 07:50:46 +0000115static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000116
David S. Miller8d8a51e2013-10-08 15:44:26 -0400117static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
118{
119 return sk->sk_user_data;
120}
121
James Chapmanfd558d12010-04-02 06:18:33 +0000122static inline struct l2tp_net *l2tp_pernet(struct net *net)
123{
124 BUG_ON(!net);
125
126 return net_generic(net, l2tp_net_id);
127}
128
stephen hemmingerfc130842010-10-21 07:50:46 +0000129/* Tunnel reference counts. Incremented per session that is added to
130 * the tunnel.
131 */
132static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
133{
134 atomic_inc(&tunnel->ref_count);
135}
136
137static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
138{
139 if (atomic_dec_and_test(&tunnel->ref_count))
140 l2tp_tunnel_free(tunnel);
141}
142#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000143#define l2tp_tunnel_inc_refcount(_t) \
144do { \
145 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
146 __func__, __LINE__, (_t)->name, \
147 atomic_read(&_t->ref_count)); \
148 l2tp_tunnel_inc_refcount_1(_t); \
149} while (0)
150#define l2tp_tunnel_dec_refcount(_t)
151do { \
152 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
153 __func__, __LINE__, (_t)->name, \
154 atomic_read(&_t->ref_count)); \
155 l2tp_tunnel_dec_refcount_1(_t); \
156} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000157#else
158#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
159#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
160#endif
161
James Chapmanf7faffa2010-04-02 06:18:49 +0000162/* Session hash global list for L2TPv3.
163 * The session_id SHOULD be random according to RFC3931, but several
164 * L2TP implementations use incrementing session_ids. So we do a real
165 * hash on the session_id, rather than a simple bitmask.
166 */
167static inline struct hlist_head *
168l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
169{
170 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
171
172}
173
Tom Parkin80d84ef2013-01-22 05:13:48 +0000174/* Lookup the tunnel socket, possibly involving the fs code if the socket is
175 * owned by userspace. A struct sock returned from this function must be
176 * released using l2tp_tunnel_sock_put once you're done with it.
177 */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800178static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000179{
180 int err = 0;
181 struct socket *sock = NULL;
182 struct sock *sk = NULL;
183
184 if (!tunnel)
185 goto out;
186
187 if (tunnel->fd >= 0) {
188 /* Socket is owned by userspace, who might be in the process
189 * of closing it. Look the socket up using the fd to ensure
190 * consistency.
191 */
192 sock = sockfd_lookup(tunnel->fd, &err);
193 if (sock)
194 sk = sock->sk;
195 } else {
196 /* Socket is owned by kernelspace */
197 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000198 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000199 }
200
201out:
202 return sk;
203}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000204
205/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800206static void l2tp_tunnel_sock_put(struct sock *sk)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000207{
208 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
209 if (tunnel) {
210 if (tunnel->fd >= 0) {
211 /* Socket is owned by userspace */
212 sockfd_put(sk->sk_socket);
213 }
214 sock_put(sk);
215 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000216 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000217}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000218
James Chapmanf7faffa2010-04-02 06:18:49 +0000219/* Lookup a session by id in the global session list
220 */
221static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
222{
223 struct l2tp_net *pn = l2tp_pernet(net);
224 struct hlist_head *session_list =
225 l2tp_session_id_hash_2(pn, session_id);
226 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000227
James Chapmane02d4942010-04-02 06:19:16 +0000228 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800229 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000230 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000231 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000232 return session;
233 }
234 }
James Chapmane02d4942010-04-02 06:19:16 +0000235 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000236
237 return NULL;
238}
239
James Chapmanfd558d12010-04-02 06:18:33 +0000240/* Session hash list.
241 * The session_id SHOULD be random according to RFC2661, but several
242 * L2TP implementations (Cisco and Microsoft) use incrementing
243 * session_ids. So we do a real hash on the session_id, rather than a
244 * simple bitmask.
245 */
246static inline struct hlist_head *
247l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
248{
249 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
250}
251
252/* Lookup a session by id
253 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000254struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000255{
James Chapmanf7faffa2010-04-02 06:18:49 +0000256 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000257 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000258
James Chapmanf7faffa2010-04-02 06:18:49 +0000259 /* In L2TPv3, session_ids are unique over all tunnels and we
260 * sometimes need to look them up before we know the
261 * tunnel.
262 */
263 if (tunnel == NULL)
264 return l2tp_session_find_2(net, session_id);
265
266 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000267 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800268 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000269 if (session->session_id == session_id) {
270 read_unlock_bh(&tunnel->hlist_lock);
271 return session;
272 }
273 }
274 read_unlock_bh(&tunnel->hlist_lock);
275
276 return NULL;
277}
278EXPORT_SYMBOL_GPL(l2tp_session_find);
279
280struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
281{
282 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000283 struct l2tp_session *session;
284 int count = 0;
285
286 read_lock_bh(&tunnel->hlist_lock);
287 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800288 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000289 if (++count > nth) {
290 read_unlock_bh(&tunnel->hlist_lock);
291 return session;
292 }
293 }
294 }
295
296 read_unlock_bh(&tunnel->hlist_lock);
297
298 return NULL;
299}
300EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
301
James Chapman309795f2010-04-02 06:19:10 +0000302/* Lookup a session by interface name.
303 * This is very inefficient but is only used by management interfaces.
304 */
305struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
306{
307 struct l2tp_net *pn = l2tp_pernet(net);
308 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000309 struct l2tp_session *session;
310
James Chapmane02d4942010-04-02 06:19:16 +0000311 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000312 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800313 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000314 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000315 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000316 return session;
317 }
318 }
319 }
320
James Chapmane02d4942010-04-02 06:19:16 +0000321 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000322
323 return NULL;
324}
325EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
326
James Chapmanfd558d12010-04-02 06:18:33 +0000327/* Lookup a tunnel by id
328 */
329struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
330{
331 struct l2tp_tunnel *tunnel;
332 struct l2tp_net *pn = l2tp_pernet(net);
333
James Chapmane02d4942010-04-02 06:19:16 +0000334 rcu_read_lock_bh();
335 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000336 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000337 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000338 return tunnel;
339 }
340 }
James Chapmane02d4942010-04-02 06:19:16 +0000341 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000342
343 return NULL;
344}
345EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
346
347struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
348{
349 struct l2tp_net *pn = l2tp_pernet(net);
350 struct l2tp_tunnel *tunnel;
351 int count = 0;
352
James Chapmane02d4942010-04-02 06:19:16 +0000353 rcu_read_lock_bh();
354 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000355 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000356 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000357 return tunnel;
358 }
359 }
360
James Chapmane02d4942010-04-02 06:19:16 +0000361 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000362
363 return NULL;
364}
365EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
366
367/*****************************************************************************
368 * Receive data handling
369 *****************************************************************************/
370
371/* Queue a skb in order. We come here only if the skb has an L2TP sequence
372 * number.
373 */
374static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
375{
376 struct sk_buff *skbp;
377 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000378 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000379
380 spin_lock_bh(&session->reorder_q.lock);
381 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
382 if (L2TP_SKB_CB(skbp)->ns > ns) {
383 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000384 l2tp_dbg(session, L2TP_MSG_SEQ,
385 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
386 session->name, ns, L2TP_SKB_CB(skbp)->ns,
387 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000388 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000389 goto out;
390 }
391 }
392
393 __skb_queue_tail(&session->reorder_q, skb);
394
395out:
396 spin_unlock_bh(&session->reorder_q.lock);
397}
398
399/* Dequeue a single skb.
400 */
401static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
402{
403 struct l2tp_tunnel *tunnel = session->tunnel;
404 int length = L2TP_SKB_CB(skb)->length;
405
406 /* We're about to requeue the skb, so return resources
407 * to its current owner (a socket receive buffer).
408 */
409 skb_orphan(skb);
410
Tom Parkin7b7c0712013-03-19 06:11:22 +0000411 atomic_long_inc(&tunnel->stats.rx_packets);
412 atomic_long_add(length, &tunnel->stats.rx_bytes);
413 atomic_long_inc(&session->stats.rx_packets);
414 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000415
416 if (L2TP_SKB_CB(skb)->has_seq) {
417 /* Bump our Nr */
418 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100419 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000420
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000421 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
422 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000423 }
424
425 /* call private receive handler */
426 if (session->recv_skb != NULL)
427 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
428 else
429 kfree_skb(skb);
430
431 if (session->deref)
432 (*session->deref)(session);
433}
434
435/* Dequeue skbs from the session's reorder_q, subject to packet order.
436 * Skbs that have been in the queue for too long are simply discarded.
437 */
438static void l2tp_recv_dequeue(struct l2tp_session *session)
439{
440 struct sk_buff *skb;
441 struct sk_buff *tmp;
442
443 /* If the pkt at the head of the queue has the nr that we
444 * expect to send up next, dequeue it and any other
445 * in-sequence packets behind it.
446 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000447start:
James Chapmanfd558d12010-04-02 06:18:33 +0000448 spin_lock_bh(&session->reorder_q.lock);
449 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
450 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000451 atomic_long_inc(&session->stats.rx_seq_discards);
452 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000453 l2tp_dbg(session, L2TP_MSG_SEQ,
454 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
455 session->name, L2TP_SKB_CB(skb)->ns,
456 L2TP_SKB_CB(skb)->length, session->nr,
457 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000458 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000459 __skb_unlink(skb, &session->reorder_q);
460 kfree_skb(skb);
461 if (session->deref)
462 (*session->deref)(session);
463 continue;
464 }
465
466 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000467 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000468 l2tp_dbg(session, L2TP_MSG_SEQ,
469 "%s: advancing nr to next pkt: %u -> %u",
470 session->name, session->nr,
471 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000472 session->reorder_skip = 0;
473 session->nr = L2TP_SKB_CB(skb)->ns;
474 }
James Chapmanfd558d12010-04-02 06:18:33 +0000475 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000476 l2tp_dbg(session, L2TP_MSG_SEQ,
477 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
478 session->name, L2TP_SKB_CB(skb)->ns,
479 L2TP_SKB_CB(skb)->length, session->nr,
480 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000481 goto out;
482 }
483 }
484 __skb_unlink(skb, &session->reorder_q);
485
486 /* Process the skb. We release the queue lock while we
487 * do so to let other contexts process the queue.
488 */
489 spin_unlock_bh(&session->reorder_q.lock);
490 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000491 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000492 }
493
494out:
495 spin_unlock_bh(&session->reorder_q.lock);
496}
497
James Chapman8a1631d2013-07-02 20:28:59 +0100498static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
499{
500 u32 nws;
501
502 if (nr >= session->nr)
503 nws = nr - session->nr;
504 else
505 nws = (session->nr_max + 1) - (session->nr - nr);
506
507 return nws < session->nr_window_size;
508}
509
James Chapmanb6dc01a2013-07-02 20:28:58 +0100510/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
511 * acceptable, else non-zero.
512 */
513static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
514{
James Chapman8a1631d2013-07-02 20:28:59 +0100515 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
516 /* Packet sequence number is outside allowed window.
517 * Discard it.
518 */
519 l2tp_dbg(session, L2TP_MSG_SEQ,
520 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
521 session->name, L2TP_SKB_CB(skb)->ns,
522 L2TP_SKB_CB(skb)->length, session->nr);
523 goto discard;
524 }
525
James Chapmanb6dc01a2013-07-02 20:28:58 +0100526 if (session->reorder_timeout != 0) {
527 /* Packet reordering enabled. Add skb to session's
528 * reorder queue, in order of ns.
529 */
530 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100531 goto out;
532 }
533
534 /* Packet reordering disabled. Discard out-of-sequence packets, while
535 * tracking the number if in-sequence packets after the first OOS packet
536 * is seen. After nr_oos_count_max in-sequence packets, reset the
537 * sequence number to re-enable packet reception.
538 */
539 if (L2TP_SKB_CB(skb)->ns == session->nr) {
540 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100541 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100542 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
543 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
544
545 if (nr_oos == nr_next)
546 session->nr_oos_count++;
547 else
548 session->nr_oos_count = 0;
549
550 session->nr_oos = nr_oos;
551 if (session->nr_oos_count > session->nr_oos_count_max) {
552 session->reorder_skip = 1;
553 l2tp_dbg(session, L2TP_MSG_SEQ,
554 "%s: %d oos packets received. Resetting sequence numbers\n",
555 session->name, session->nr_oos_count);
556 }
557 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100558 atomic_long_inc(&session->stats.rx_seq_discards);
559 l2tp_dbg(session, L2TP_MSG_SEQ,
560 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
561 session->name, L2TP_SKB_CB(skb)->ns,
562 L2TP_SKB_CB(skb)->length, session->nr,
563 skb_queue_len(&session->reorder_q));
564 goto discard;
565 }
566 skb_queue_tail(&session->reorder_q, skb);
567 }
568
James Chapmana0dbd822013-07-02 20:29:00 +0100569out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100570 return 0;
571
572discard:
573 return 1;
574}
575
James Chapmanf7faffa2010-04-02 06:18:49 +0000576/* Do receive processing of L2TP data frames. We handle both L2TPv2
577 * and L2TPv3 data frames here.
578 *
579 * L2TPv2 Data Message Header
580 *
581 * 0 1 2 3
582 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * | Tunnel ID | Session ID |
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * | Ns (opt) | Nr (opt) |
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * | Offset Size (opt) | Offset pad... (opt)
591 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 *
593 * Data frames are marked by T=0. All other fields are the same as
594 * those in L2TP control frames.
595 *
596 * L2TPv3 Data Message Header
597 *
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * | L2TP Session Header |
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * | L2-Specific Sublayer |
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 * | Tunnel Payload ...
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 *
606 * L2TPv3 Session Header Over IP
607 *
608 * 0 1 2 3
609 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 * | Session ID |
612 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
613 * | Cookie (optional, maximum 64 bits)...
614 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
615 * |
616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
617 *
618 * L2TPv3 L2-Specific Sublayer Format
619 *
620 * 0 1 2 3
621 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * |x|S|x|x|x|x|x|x| Sequence Number |
624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
625 *
626 * Cookie value, sublayer format and offset (pad) are negotiated with
627 * the peer when the session is set up. Unlike L2TPv2, we do not need
628 * to parse the packet header to determine if optional fields are
629 * present.
630 *
631 * Caller must already have parsed the frame and determined that it is
632 * a data (not control) frame before coming here. Fields up to the
633 * session-id have already been parsed and ptr points to the data
634 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000635 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000636void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
637 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
638 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000639{
James Chapmanf7faffa2010-04-02 06:18:49 +0000640 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000641 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000642 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000643
644 /* The ref count is increased since we now hold a pointer to
645 * the session. Take care to decrement the refcnt when exiting
646 * this function from now on...
647 */
648 l2tp_session_inc_refcount(session);
649 if (session->ref)
650 (*session->ref)(session);
651
James Chapmanf7faffa2010-04-02 06:18:49 +0000652 /* Parse and check optional cookie */
653 if (session->peer_cookie_len > 0) {
654 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000655 l2tp_info(tunnel, L2TP_MSG_DATA,
656 "%s: cookie mismatch (%u/%u). Discarding.\n",
657 tunnel->name, tunnel->tunnel_id,
658 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000659 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000660 goto discard;
661 }
662 ptr += session->peer_cookie_len;
663 }
664
James Chapmanfd558d12010-04-02 06:18:33 +0000665 /* Handle the optional sequence numbers. Sequence numbers are
666 * in different places for L2TPv2 and L2TPv3.
667 *
668 * If we are the LAC, enable/disable sequence numbers under
669 * the control of the LNS. If no sequence numbers present but
670 * we were expecting them, discard frame.
671 */
672 ns = nr = 0;
673 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000674 if (tunnel->version == L2TP_HDR_VER_2) {
675 if (hdrflags & L2TP_HDRFLAG_S) {
676 ns = ntohs(*(__be16 *) ptr);
677 ptr += 2;
678 nr = ntohs(*(__be16 *) ptr);
679 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000680
James Chapmanf7faffa2010-04-02 06:18:49 +0000681 /* Store L2TP info in the skb */
682 L2TP_SKB_CB(skb)->ns = ns;
683 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000684
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000685 l2tp_dbg(session, L2TP_MSG_SEQ,
686 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
687 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000688 }
689 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
690 u32 l2h = ntohl(*(__be32 *) ptr);
691
692 if (l2h & 0x40000000) {
693 ns = l2h & 0x00ffffff;
694
695 /* Store L2TP info in the skb */
696 L2TP_SKB_CB(skb)->ns = ns;
697 L2TP_SKB_CB(skb)->has_seq = 1;
698
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000699 l2tp_dbg(session, L2TP_MSG_SEQ,
700 "%s: recv data ns=%u, session nr=%u\n",
701 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000702 }
James Chapmanfd558d12010-04-02 06:18:33 +0000703 }
704
James Chapmanf7faffa2010-04-02 06:18:49 +0000705 /* Advance past L2-specific header, if present */
706 ptr += session->l2specific_len;
707
James Chapmanfd558d12010-04-02 06:18:33 +0000708 if (L2TP_SKB_CB(skb)->has_seq) {
709 /* Received a packet with sequence numbers. If we're the LNS,
710 * check if we sre sending sequence numbers and if not,
711 * configure it so.
712 */
713 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000714 l2tp_info(session, L2TP_MSG_SEQ,
715 "%s: requested to enable seq numbers by LNS\n",
716 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000717 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000718 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000719 }
720 } else {
721 /* No sequence numbers.
722 * If user has configured mandatory sequence numbers, discard.
723 */
724 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000725 l2tp_warn(session, L2TP_MSG_SEQ,
726 "%s: recv data has no seq numbers when required. Discarding.\n",
727 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000728 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000729 goto discard;
730 }
731
732 /* If we're the LAC and we're sending sequence numbers, the
733 * LNS has requested that we no longer send sequence numbers.
734 * If we're the LNS and we're sending sequence numbers, the
735 * LAC is broken. Discard the frame.
736 */
737 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000738 l2tp_info(session, L2TP_MSG_SEQ,
739 "%s: requested to disable seq numbers by LNS\n",
740 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000741 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000742 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000743 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000744 l2tp_warn(session, L2TP_MSG_SEQ,
745 "%s: recv data has no seq numbers when required. Discarding.\n",
746 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000747 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000748 goto discard;
749 }
750 }
751
James Chapmanf7faffa2010-04-02 06:18:49 +0000752 /* Session data offset is handled differently for L2TPv2 and
753 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
754 * the header. For L2TPv3, the offset is negotiated using AVPs
755 * in the session setup control protocol.
756 */
757 if (tunnel->version == L2TP_HDR_VER_2) {
758 /* If offset bit set, skip it. */
759 if (hdrflags & L2TP_HDRFLAG_O) {
760 offset = ntohs(*(__be16 *)ptr);
761 ptr += 2 + offset;
762 }
763 } else
764 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000765
766 offset = ptr - optr;
767 if (!pskb_may_pull(skb, offset))
768 goto discard;
769
770 __skb_pull(skb, offset);
771
772 /* If caller wants to process the payload before we queue the
773 * packet, do so now.
774 */
775 if (payload_hook)
776 if ((*payload_hook)(skb))
777 goto discard;
778
779 /* Prepare skb for adding to the session's reorder_q. Hold
780 * packets for max reorder_timeout or 1 second if not
781 * reordering.
782 */
783 L2TP_SKB_CB(skb)->length = length;
784 L2TP_SKB_CB(skb)->expires = jiffies +
785 (session->reorder_timeout ? session->reorder_timeout : HZ);
786
787 /* Add packet to the session's receive queue. Reordering is done here, if
788 * enabled. Saved L2TP protocol info is stored in skb->sb[].
789 */
790 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100791 if (l2tp_recv_data_seq(session, skb))
792 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000793 } else {
794 /* No sequence numbers. Add the skb to the tail of the
795 * reorder queue. This ensures that it will be
796 * delivered after all previous sequenced skbs.
797 */
798 skb_queue_tail(&session->reorder_q, skb);
799 }
800
801 /* Try to dequeue as many skbs from reorder_q as we can. */
802 l2tp_recv_dequeue(session);
803
804 l2tp_session_dec_refcount(session);
805
James Chapmanf7faffa2010-04-02 06:18:49 +0000806 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000807
808discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000809 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000810 kfree_skb(skb);
811
812 if (session->deref)
813 (*session->deref)(session);
814
815 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000816}
817EXPORT_SYMBOL(l2tp_recv_common);
818
Tom Parkin48f72f92013-03-19 06:11:19 +0000819/* Drop skbs from the session's reorder_q
820 */
821int l2tp_session_queue_purge(struct l2tp_session *session)
822{
823 struct sk_buff *skb = NULL;
824 BUG_ON(!session);
825 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
826 while ((skb = skb_dequeue(&session->reorder_q))) {
827 atomic_long_inc(&session->stats.rx_errors);
828 kfree_skb(skb);
829 if (session->deref)
830 (*session->deref)(session);
831 }
832 return 0;
833}
834EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
835
James Chapmanf7faffa2010-04-02 06:18:49 +0000836/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
837 * here. The skb is not on a list when we get here.
838 * Returns 0 if the packet was a data packet and was successfully passed on.
839 * Returns 1 if the packet was not a good data packet and could not be
840 * forwarded. All such packets are passed up to userspace to deal with.
841 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000842static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
843 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000844{
845 struct l2tp_session *session = NULL;
846 unsigned char *ptr, *optr;
847 u16 hdrflags;
848 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000849 u16 version;
850 int length;
851
Tom Herbert58d60852014-05-07 16:52:48 -0700852 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000853
854 /* UDP always verifies the packet length. */
855 __skb_pull(skb, sizeof(struct udphdr));
856
857 /* Short packet? */
858 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000859 l2tp_info(tunnel, L2TP_MSG_DATA,
860 "%s: recv short packet (len=%d)\n",
861 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000862 goto error;
863 }
864
James Chapmanf7faffa2010-04-02 06:18:49 +0000865 /* Trace packet contents, if enabled */
866 if (tunnel->debug & L2TP_MSG_DATA) {
867 length = min(32u, skb->len);
868 if (!pskb_may_pull(skb, length))
869 goto error;
870
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000871 pr_debug("%s: recv\n", tunnel->name);
872 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000873 }
874
Eric Dumazete50e7052011-11-08 13:59:44 -0500875 /* Point to L2TP header */
876 optr = ptr = skb->data;
877
James Chapmanf7faffa2010-04-02 06:18:49 +0000878 /* Get L2TP header flags */
879 hdrflags = ntohs(*(__be16 *) ptr);
880
881 /* Check protocol version */
882 version = hdrflags & L2TP_HDR_VER_MASK;
883 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000884 l2tp_info(tunnel, L2TP_MSG_DATA,
885 "%s: recv protocol version mismatch: got %d expected %d\n",
886 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000887 goto error;
888 }
889
890 /* Get length of L2TP packet */
891 length = skb->len;
892
893 /* If type is control packet, it is handled by userspace. */
894 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000895 l2tp_dbg(tunnel, L2TP_MSG_DATA,
896 "%s: recv control packet, len=%d\n",
897 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000898 goto error;
899 }
900
901 /* Skip flags */
902 ptr += 2;
903
904 if (tunnel->version == L2TP_HDR_VER_2) {
905 /* If length is present, skip it */
906 if (hdrflags & L2TP_HDRFLAG_L)
907 ptr += 2;
908
909 /* Extract tunnel and session ID */
910 tunnel_id = ntohs(*(__be16 *) ptr);
911 ptr += 2;
912 session_id = ntohs(*(__be16 *) ptr);
913 ptr += 2;
914 } else {
915 ptr += 2; /* skip reserved bits */
916 tunnel_id = tunnel->tunnel_id;
917 session_id = ntohl(*(__be32 *) ptr);
918 ptr += 4;
919 }
920
921 /* Find the session context */
922 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000923 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000924 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000925 l2tp_info(tunnel, L2TP_MSG_DATA,
926 "%s: no session found (%u/%u). Passing up.\n",
927 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000928 goto error;
929 }
930
931 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000932
933 return 0;
934
James Chapmanfd558d12010-04-02 06:18:33 +0000935error:
936 /* Put UDP header back */
937 __skb_push(skb, sizeof(struct udphdr));
938
939 return 1;
940}
James Chapmanfd558d12010-04-02 06:18:33 +0000941
942/* UDP encapsulation receive handler. See net/ipv4/udp.c.
943 * Return codes:
944 * 0 : success.
945 * <0: error
946 * >0: skb should be passed up to userspace as UDP.
947 */
948int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
949{
950 struct l2tp_tunnel *tunnel;
951
952 tunnel = l2tp_sock_to_tunnel(sk);
953 if (tunnel == NULL)
954 goto pass_up;
955
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000956 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
957 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000958
959 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
960 goto pass_up_put;
961
962 sock_put(sk);
963 return 0;
964
965pass_up_put:
966 sock_put(sk);
967pass_up:
968 return 1;
969}
970EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
971
972/************************************************************************
973 * Transmit handling
974 ***********************************************************************/
975
976/* Build an L2TP header for the session into the buffer provided.
977 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000978static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000979{
James Chapmanf7faffa2010-04-02 06:18:49 +0000980 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000981 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000982 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000983 u16 flags = L2TP_HDR_VER_2;
984 u32 tunnel_id = tunnel->peer_tunnel_id;
985 u32 session_id = session->peer_session_id;
986
987 if (session->send_seq)
988 flags |= L2TP_HDRFLAG_S;
989
990 /* Setup L2TP header. */
991 *bufp++ = htons(flags);
992 *bufp++ = htons(tunnel_id);
993 *bufp++ = htons(session_id);
994 if (session->send_seq) {
995 *bufp++ = htons(session->ns);
996 *bufp++ = 0;
997 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000998 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000999 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1000 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001001 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001002
1003 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001004}
1005
James Chapmanf7faffa2010-04-02 06:18:49 +00001006static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001007{
James Chapman0d767512010-04-02 06:19:00 +00001008 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001009 char *bufp = buf;
1010 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001011
James Chapman0d767512010-04-02 06:19:00 +00001012 /* Setup L2TP header. The header differs slightly for UDP and
1013 * IP encapsulations. For UDP, there is 4 bytes of flags.
1014 */
1015 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1016 u16 flags = L2TP_HDR_VER_3;
1017 *((__be16 *) bufp) = htons(flags);
1018 bufp += 2;
1019 *((__be16 *) bufp) = 0;
1020 bufp += 2;
1021 }
1022
James Chapmanf7faffa2010-04-02 06:18:49 +00001023 *((__be32 *) bufp) = htonl(session->peer_session_id);
1024 bufp += 4;
1025 if (session->cookie_len) {
1026 memcpy(bufp, &session->cookie[0], session->cookie_len);
1027 bufp += session->cookie_len;
1028 }
1029 if (session->l2specific_len) {
1030 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1031 u32 l2h = 0;
1032 if (session->send_seq) {
1033 l2h = 0x40000000 | session->ns;
1034 session->ns++;
1035 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001036 l2tp_dbg(session, L2TP_MSG_SEQ,
1037 "%s: updated ns to %u\n",
1038 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001039 }
1040
1041 *((__be32 *) bufp) = htonl(l2h);
1042 }
1043 bufp += session->l2specific_len;
1044 }
1045 if (session->offset)
1046 bufp += session->offset;
1047
1048 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001049}
James Chapmanfd558d12010-04-02 06:18:33 +00001050
stephen hemmingerfc130842010-10-21 07:50:46 +00001051static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001052 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001053{
1054 struct l2tp_tunnel *tunnel = session->tunnel;
1055 unsigned int len = skb->len;
1056 int error;
1057
1058 /* Debug */
1059 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001060 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1061 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001062 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001063 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1064 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001065
1066 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001067 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1068 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001069
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001070 pr_debug("%s: xmit\n", session->name);
1071 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1072 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001073 }
1074
1075 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001076 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001077#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001078 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001079 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001080 else
1081#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001082 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001083
1084 /* Update stats */
1085 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001086 atomic_long_inc(&tunnel->stats.tx_packets);
1087 atomic_long_add(len, &tunnel->stats.tx_bytes);
1088 atomic_long_inc(&session->stats.tx_packets);
1089 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001090 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001091 atomic_long_inc(&tunnel->stats.tx_errors);
1092 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001093 }
1094
1095 return 0;
1096}
James Chapmanfd558d12010-04-02 06:18:33 +00001097
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001098#if IS_ENABLED(CONFIG_IPV6)
1099static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1100 int udp_len)
1101{
1102 struct ipv6_pinfo *np = inet6_sk(sk);
1103 struct udphdr *uh = udp_hdr(skb);
1104
1105 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1106 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1107 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1108 skb->ip_summed = CHECKSUM_UNNECESSARY;
Eric Dumazetefe42082013-10-03 15:42:29 -07001109 uh->check = csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr, udp_len,
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001110 IPPROTO_UDP, csum);
1111 if (uh->check == 0)
1112 uh->check = CSUM_MANGLED_0;
1113 } else {
1114 skb->ip_summed = CHECKSUM_PARTIAL;
1115 skb->csum_start = skb_transport_header(skb) - skb->head;
1116 skb->csum_offset = offsetof(struct udphdr, check);
Eric Dumazetefe42082013-10-03 15:42:29 -07001117 uh->check = ~csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr,
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001118 udp_len, IPPROTO_UDP, 0);
1119 }
1120}
1121#endif
1122
James Chapmanfd558d12010-04-02 06:18:33 +00001123/* If caller requires the skb to have a ppp header, the header must be
1124 * inserted in the skb data before calling this function.
1125 */
1126int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1127{
1128 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001129 struct l2tp_tunnel *tunnel = session->tunnel;
1130 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001131 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001132 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001133 struct inet_sock *inet;
1134 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001135 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001136 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1137 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001138 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001139
1140 /* Check that there's enough headroom in the skb to insert IP,
1141 * UDP and L2TP headers. If not enough, expand it to
1142 * make room. Adjust truesize.
1143 */
1144 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001145 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001146 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001147 kfree_skb(skb);
1148 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001149 }
James Chapmanfd558d12010-04-02 06:18:33 +00001150
James Chapmanfd558d12010-04-02 06:18:33 +00001151 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001152 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001153
James Chapman0d767512010-04-02 06:19:00 +00001154 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001155 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1156 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1157 IPSKB_REROUTED);
1158 nf_reset(skb);
1159
David S. Miller6af88da2011-05-08 13:45:20 -07001160 bh_lock_sock(sk);
1161 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001162 kfree_skb(skb);
1163 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001164 goto out_unlock;
1165 }
1166
James Chapmanfd558d12010-04-02 06:18:33 +00001167 /* Get routing info from the tunnel socket */
1168 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001169 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001170
David S. Millerd9d8da82011-05-06 22:23:20 -07001171 inet = inet_sk(sk);
1172 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001173 switch (tunnel->encap) {
1174 case L2TP_ENCAPTYPE_UDP:
1175 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001176 __skb_push(skb, sizeof(*uh));
1177 skb_reset_transport_header(skb);
1178 uh = udp_hdr(skb);
1179 uh->source = inet->inet_sport;
1180 uh->dest = inet->inet_dport;
1181 udp_len = uhlen + hdr_len + data_len;
1182 uh->len = htons(udp_len);
1183 uh->check = 0;
1184
1185 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001186#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001187 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001188 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1189 else
1190#endif
James Chapman0d767512010-04-02 06:19:00 +00001191 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1192 skb->ip_summed = CHECKSUM_NONE;
1193 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1194 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1195 skb->ip_summed = CHECKSUM_COMPLETE;
1196 csum = skb_checksum(skb, 0, udp_len, 0);
1197 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1198 inet->inet_daddr,
1199 udp_len, IPPROTO_UDP, csum);
1200 if (uh->check == 0)
1201 uh->check = CSUM_MANGLED_0;
1202 } else {
1203 skb->ip_summed = CHECKSUM_PARTIAL;
1204 skb->csum_start = skb_transport_header(skb) - skb->head;
1205 skb->csum_offset = offsetof(struct udphdr, check);
1206 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1207 inet->inet_daddr,
1208 udp_len, IPPROTO_UDP, 0);
1209 }
1210 break;
1211
1212 case L2TP_ENCAPTYPE_IP:
1213 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001214 }
1215
David S. Millerd9d8da82011-05-06 22:23:20 -07001216 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001217out_unlock:
1218 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001219
Eric Dumazetb8c84302012-06-28 20:15:13 +00001220 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001221}
1222EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1223
1224/*****************************************************************************
1225 * Tinnel and session create/destroy.
1226 *****************************************************************************/
1227
1228/* Tunnel socket destruct hook.
1229 * The tunnel context is deleted only when all session sockets have been
1230 * closed.
1231 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001232static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001233{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001234 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001235 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001236
James Chapmanfd558d12010-04-02 06:18:33 +00001237 if (tunnel == NULL)
1238 goto end;
1239
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001240 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001241
James Chapmanfd558d12010-04-02 06:18:33 +00001242
Tom Parkinf8ccac02013-01-31 23:43:00 +00001243 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001244 switch (tunnel->encap) {
1245 case L2TP_ENCAPTYPE_UDP:
1246 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1247 (udp_sk(sk))->encap_type = 0;
1248 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001249 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001250 break;
1251 case L2TP_ENCAPTYPE_IP:
1252 break;
1253 }
James Chapmanfd558d12010-04-02 06:18:33 +00001254
1255 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001256 sk->sk_destruct = tunnel->old_sk_destruct;
1257 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001258 tunnel->sock = NULL;
1259
1260 /* Remove the tunnel struct from the tunnel list */
1261 pn = l2tp_pernet(tunnel->l2tp_net);
1262 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1263 list_del_rcu(&tunnel->list);
1264 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1265 atomic_dec(&l2tp_tunnel_count);
1266
1267 l2tp_tunnel_closeall(tunnel);
1268 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001269
1270 /* Call the original destructor */
1271 if (sk->sk_destruct)
1272 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001273end:
1274 return;
1275}
James Chapmanfd558d12010-04-02 06:18:33 +00001276
1277/* When the tunnel is closed, all the attached sessions need to go too.
1278 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001279void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001280{
1281 int hash;
1282 struct hlist_node *walk;
1283 struct hlist_node *tmp;
1284 struct l2tp_session *session;
1285
1286 BUG_ON(tunnel == NULL);
1287
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001288 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1289 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001290
1291 write_lock_bh(&tunnel->hlist_lock);
1292 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1293again:
1294 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1295 session = hlist_entry(walk, struct l2tp_session, hlist);
1296
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001297 l2tp_info(session, L2TP_MSG_CONTROL,
1298 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001299
1300 hlist_del_init(&session->hlist);
1301
James Chapmanfd558d12010-04-02 06:18:33 +00001302 if (session->ref != NULL)
1303 (*session->ref)(session);
1304
1305 write_unlock_bh(&tunnel->hlist_lock);
1306
Tom Parkinf6e16b22013-03-19 06:11:23 +00001307 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001308 l2tp_session_queue_purge(session);
1309
James Chapmanfd558d12010-04-02 06:18:33 +00001310 if (session->session_close != NULL)
1311 (*session->session_close)(session);
1312
1313 if (session->deref != NULL)
1314 (*session->deref)(session);
1315
Tom Parkin9980d002013-03-19 06:11:13 +00001316 l2tp_session_dec_refcount(session);
1317
James Chapmanfd558d12010-04-02 06:18:33 +00001318 write_lock_bh(&tunnel->hlist_lock);
1319
1320 /* Now restart from the beginning of this hash
1321 * chain. We always remove a session from the
1322 * list so we are guaranteed to make forward
1323 * progress.
1324 */
1325 goto again;
1326 }
1327 }
1328 write_unlock_bh(&tunnel->hlist_lock);
1329}
Tom Parkine34f4c72013-03-19 06:11:14 +00001330EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001331
Tom Parkin9980d002013-03-19 06:11:13 +00001332/* Tunnel socket destroy hook for UDP encapsulation */
1333static void l2tp_udp_encap_destroy(struct sock *sk)
1334{
1335 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1336 if (tunnel) {
1337 l2tp_tunnel_closeall(tunnel);
1338 sock_put(sk);
1339 }
1340}
1341
James Chapmanfd558d12010-04-02 06:18:33 +00001342/* Really kill the tunnel.
1343 * Come here only when all sessions have been cleared from the tunnel.
1344 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001345static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001346{
James Chapmanfd558d12010-04-02 06:18:33 +00001347 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1348 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001349 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001350 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001351}
James Chapmanfd558d12010-04-02 06:18:33 +00001352
Tom Parkinf8ccac02013-01-31 23:43:00 +00001353/* Workqueue tunnel deletion function */
1354static void l2tp_tunnel_del_work(struct work_struct *work)
1355{
1356 struct l2tp_tunnel *tunnel = NULL;
1357 struct socket *sock = NULL;
1358 struct sock *sk = NULL;
1359
1360 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1361 sk = l2tp_tunnel_sock_lookup(tunnel);
1362 if (!sk)
1363 return;
1364
1365 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001366
Tom Parkin02d13ed2013-03-19 06:11:18 +00001367 /* If the tunnel socket was created by userspace, then go through the
1368 * inet layer to shut the socket down, and let userspace close it.
1369 * Otherwise, if we created the socket directly within the kernel, use
1370 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001371 * In either case the tunnel resources are freed in the socket
1372 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001373 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001374 if (tunnel->fd >= 0) {
1375 if (sock)
1376 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001377 } else {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001378 if (sock)
1379 kernel_sock_shutdown(sock, SHUT_RDWR);
1380 sk_release_kernel(sk);
Tom Parkin167eb172013-01-31 23:43:03 +00001381 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001382
1383 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001384}
James Chapmanfd558d12010-04-02 06:18:33 +00001385
James Chapman789a4a22010-04-02 06:19:40 +00001386/* Create a socket for the tunnel, if one isn't set up by
1387 * userspace. This is used for static tunnels where there is no
1388 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001389 *
1390 * Since we don't want these sockets to keep a namespace alive by
1391 * themselves, we drop the socket's namespace refcount after creation.
1392 * These sockets are freed when the namespace exits using the pernet
1393 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001394 */
Tom Parkin167eb172013-01-31 23:43:03 +00001395static int l2tp_tunnel_sock_create(struct net *net,
1396 u32 tunnel_id,
1397 u32 peer_tunnel_id,
1398 struct l2tp_tunnel_cfg *cfg,
1399 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001400{
1401 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001402 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001403 struct sockaddr_in udp_addr = {0};
1404 struct sockaddr_l2tpip ip_addr = {0};
1405#if IS_ENABLED(CONFIG_IPV6)
1406 struct sockaddr_in6 udp6_addr = {0};
1407 struct sockaddr_l2tpip6 ip6_addr = {0};
1408#endif
James Chapman789a4a22010-04-02 06:19:40 +00001409
1410 switch (cfg->encap) {
1411 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001412#if IS_ENABLED(CONFIG_IPV6)
1413 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001414 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001415 if (err < 0)
1416 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001417
Tom Parkin167eb172013-01-31 23:43:03 +00001418 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001419
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001420 udp6_addr.sin6_family = AF_INET6;
1421 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1422 sizeof(udp6_addr.sin6_addr));
1423 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1424 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1425 sizeof(udp6_addr));
1426 if (err < 0)
1427 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001428
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001429 udp6_addr.sin6_family = AF_INET6;
1430 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1431 sizeof(udp6_addr.sin6_addr));
1432 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1433 err = kernel_connect(sock,
1434 (struct sockaddr *) &udp6_addr,
1435 sizeof(udp6_addr), 0);
1436 if (err < 0)
1437 goto out;
1438 } else
1439#endif
1440 {
Tom Parkin167eb172013-01-31 23:43:03 +00001441 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001442 if (err < 0)
1443 goto out;
1444
Tom Parkin167eb172013-01-31 23:43:03 +00001445 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001446
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001447 udp_addr.sin_family = AF_INET;
1448 udp_addr.sin_addr = cfg->local_ip;
1449 udp_addr.sin_port = htons(cfg->local_udp_port);
1450 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1451 sizeof(udp_addr));
1452 if (err < 0)
1453 goto out;
1454
1455 udp_addr.sin_family = AF_INET;
1456 udp_addr.sin_addr = cfg->peer_ip;
1457 udp_addr.sin_port = htons(cfg->peer_udp_port);
1458 err = kernel_connect(sock,
1459 (struct sockaddr *) &udp_addr,
1460 sizeof(udp_addr), 0);
1461 if (err < 0)
1462 goto out;
1463 }
James Chapman789a4a22010-04-02 06:19:40 +00001464
1465 if (!cfg->use_udp_checksums)
1466 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1467
1468 break;
1469
1470 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001471#if IS_ENABLED(CONFIG_IPV6)
1472 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001473 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1474 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001475 if (err < 0)
1476 goto out;
1477
Tom Parkin167eb172013-01-31 23:43:03 +00001478 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001479
James Chapman5dac94e2012-04-29 21:48:55 +00001480 ip6_addr.l2tp_family = AF_INET6;
1481 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1482 sizeof(ip6_addr.l2tp_addr));
1483 ip6_addr.l2tp_conn_id = tunnel_id;
1484 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1485 sizeof(ip6_addr));
1486 if (err < 0)
1487 goto out;
1488
1489 ip6_addr.l2tp_family = AF_INET6;
1490 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1491 sizeof(ip6_addr.l2tp_addr));
1492 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1493 err = kernel_connect(sock,
1494 (struct sockaddr *) &ip6_addr,
1495 sizeof(ip6_addr), 0);
1496 if (err < 0)
1497 goto out;
1498 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001499#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001500 {
Tom Parkin167eb172013-01-31 23:43:03 +00001501 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1502 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001503 if (err < 0)
1504 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001505
Tom Parkin167eb172013-01-31 23:43:03 +00001506 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001507
James Chapman5dac94e2012-04-29 21:48:55 +00001508 ip_addr.l2tp_family = AF_INET;
1509 ip_addr.l2tp_addr = cfg->local_ip;
1510 ip_addr.l2tp_conn_id = tunnel_id;
1511 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1512 sizeof(ip_addr));
1513 if (err < 0)
1514 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001515
James Chapman5dac94e2012-04-29 21:48:55 +00001516 ip_addr.l2tp_family = AF_INET;
1517 ip_addr.l2tp_addr = cfg->peer_ip;
1518 ip_addr.l2tp_conn_id = peer_tunnel_id;
1519 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1520 sizeof(ip_addr), 0);
1521 if (err < 0)
1522 goto out;
1523 }
James Chapman789a4a22010-04-02 06:19:40 +00001524 break;
1525
1526 default:
1527 goto out;
1528 }
1529
1530out:
Tom Parkin167eb172013-01-31 23:43:03 +00001531 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001532 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001533 kernel_sock_shutdown(sock, SHUT_RDWR);
1534 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001535 *sockp = NULL;
1536 }
1537
1538 return err;
1539}
1540
Eric Dumazet37159ef2012-09-04 07:18:57 +00001541static struct lock_class_key l2tp_socket_class;
1542
James Chapmanfd558d12010-04-02 06:18:33 +00001543int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1544{
1545 struct l2tp_tunnel *tunnel = NULL;
1546 int err;
1547 struct socket *sock = NULL;
1548 struct sock *sk = NULL;
1549 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001550 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001551
1552 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001553 * the userspace L2TP daemon. If not specified, create a
1554 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001555 */
James Chapman789a4a22010-04-02 06:19:40 +00001556 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001557 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1558 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001559 if (err < 0)
1560 goto err;
1561 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001562 sock = sockfd_lookup(fd, &err);
1563 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001564 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001565 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001566 err = -EBADF;
1567 goto err;
1568 }
1569
1570 /* Reject namespace mismatches */
1571 if (!net_eq(sock_net(sock->sk), net)) {
1572 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1573 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001574 goto err;
1575 }
James Chapmanfd558d12010-04-02 06:18:33 +00001576 }
1577
1578 sk = sock->sk;
1579
James Chapman0d767512010-04-02 06:19:00 +00001580 if (cfg != NULL)
1581 encap = cfg->encap;
1582
James Chapmanfd558d12010-04-02 06:18:33 +00001583 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001584 switch (encap) {
1585 case L2TP_ENCAPTYPE_UDP:
1586 err = -EPROTONOSUPPORT;
1587 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001588 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001589 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1590 goto err;
1591 }
1592 break;
1593 case L2TP_ENCAPTYPE_IP:
1594 err = -EPROTONOSUPPORT;
1595 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001596 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001597 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1598 goto err;
1599 }
1600 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001601 }
1602
1603 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001604 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001605 if (tunnel != NULL) {
1606 /* This socket has already been prepped */
1607 err = -EBUSY;
1608 goto err;
1609 }
1610
James Chapmanfd558d12010-04-02 06:18:33 +00001611 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1612 if (tunnel == NULL) {
1613 err = -ENOMEM;
1614 goto err;
1615 }
1616
1617 tunnel->version = version;
1618 tunnel->tunnel_id = tunnel_id;
1619 tunnel->peer_tunnel_id = peer_tunnel_id;
1620 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1621
1622 tunnel->magic = L2TP_TUNNEL_MAGIC;
1623 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1624 rwlock_init(&tunnel->hlist_lock);
1625
1626 /* The net we belong to */
1627 tunnel->l2tp_net = net;
1628 pn = l2tp_pernet(net);
1629
James Chapman0d767512010-04-02 06:19:00 +00001630 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001631 tunnel->debug = cfg->debug;
1632
François Cachereule18503f2013-10-02 10:16:02 +02001633#if IS_ENABLED(CONFIG_IPV6)
1634 if (sk->sk_family == PF_INET6) {
1635 struct ipv6_pinfo *np = inet6_sk(sk);
1636
1637 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001638 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001639 struct inet_sock *inet = inet_sk(sk);
1640
1641 tunnel->v4mapped = true;
1642 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001643 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1644 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001645 } else {
1646 tunnel->v4mapped = false;
1647 }
1648 }
1649#endif
1650
James Chapmanfd558d12010-04-02 06:18:33 +00001651 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001652 tunnel->encap = encap;
1653 if (encap == L2TP_ENCAPTYPE_UDP) {
1654 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1655 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1656 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001657 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001658#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001659 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001660 udpv6_encap_enable();
1661 else
1662#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001663 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001664 }
James Chapmanfd558d12010-04-02 06:18:33 +00001665
1666 sk->sk_user_data = tunnel;
1667
1668 /* Hook on the tunnel socket destructor so that we can cleanup
1669 * if the tunnel socket goes away.
1670 */
1671 tunnel->old_sk_destruct = sk->sk_destruct;
1672 sk->sk_destruct = &l2tp_tunnel_destruct;
1673 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001674 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001675 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1676
James Chapmanfd558d12010-04-02 06:18:33 +00001677 sk->sk_allocation = GFP_ATOMIC;
1678
Tom Parkinf8ccac02013-01-31 23:43:00 +00001679 /* Init delete workqueue struct */
1680 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1681
James Chapmanfd558d12010-04-02 06:18:33 +00001682 /* Add tunnel to our list */
1683 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001684 atomic_inc(&l2tp_tunnel_count);
1685
1686 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001687 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001688 */
1689 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001690 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1691 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1692 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001693
1694 err = 0;
1695err:
1696 if (tunnelp)
1697 *tunnelp = tunnel;
1698
James Chapman789a4a22010-04-02 06:19:40 +00001699 /* If tunnel's socket was created by the kernel, it doesn't
1700 * have a file.
1701 */
1702 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001703 sockfd_put(sock);
1704
1705 return err;
1706}
1707EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1708
James Chapman309795f2010-04-02 06:19:10 +00001709/* This function is used by the netlink TUNNEL_DELETE command.
1710 */
1711int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1712{
Tom Parkin2b551c62013-03-19 06:11:16 +00001713 l2tp_tunnel_closeall(tunnel);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001714 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001715}
1716EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1717
James Chapmanfd558d12010-04-02 06:18:33 +00001718/* Really kill the session.
1719 */
1720void l2tp_session_free(struct l2tp_session *session)
1721{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001722 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001723
1724 BUG_ON(atomic_read(&session->ref_count) != 0);
1725
Tom Parkinf6e16b22013-03-19 06:11:23 +00001726 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001727 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001728 if (session->session_id != 0)
1729 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001730 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001731 session->tunnel = NULL;
1732 l2tp_tunnel_dec_refcount(tunnel);
1733 }
1734
1735 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001736}
1737EXPORT_SYMBOL_GPL(l2tp_session_free);
1738
Tom Parkinf6e16b22013-03-19 06:11:23 +00001739/* Remove an l2tp session from l2tp_core's hash lists.
1740 * Provides a tidyup interface for pseudowire code which can't just route all
1741 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1742 * callback.
1743 */
1744void __l2tp_session_unhash(struct l2tp_session *session)
1745{
1746 struct l2tp_tunnel *tunnel = session->tunnel;
1747
1748 /* Remove the session from core hashes */
1749 if (tunnel) {
1750 /* Remove from the per-tunnel hash */
1751 write_lock_bh(&tunnel->hlist_lock);
1752 hlist_del_init(&session->hlist);
1753 write_unlock_bh(&tunnel->hlist_lock);
1754
1755 /* For L2TPv3 we have a per-net hash: remove from there, too */
1756 if (tunnel->version != L2TP_HDR_VER_2) {
1757 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1758 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1759 hlist_del_init_rcu(&session->global_hlist);
1760 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1761 synchronize_rcu();
1762 }
1763 }
1764}
1765EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1766
James Chapman309795f2010-04-02 06:19:10 +00001767/* This function is used by the netlink SESSION_DELETE command and by
1768 pseudowire modules.
1769 */
1770int l2tp_session_delete(struct l2tp_session *session)
1771{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001772 if (session->ref)
1773 (*session->ref)(session);
1774 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001775 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001776 if (session->session_close != NULL)
1777 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001778 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001779 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001780 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001781 return 0;
1782}
1783EXPORT_SYMBOL_GPL(l2tp_session_delete);
1784
James Chapmanf7faffa2010-04-02 06:18:49 +00001785/* We come here whenever a session's send_seq, cookie_len or
1786 * l2specific_len parameters are set.
1787 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001788void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001789{
1790 if (version == L2TP_HDR_VER_2) {
1791 session->hdr_len = 6;
1792 if (session->send_seq)
1793 session->hdr_len += 4;
1794 } else {
James Chapman0d767512010-04-02 06:19:00 +00001795 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1796 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1797 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001798 }
1799
1800}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001801EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001802
James Chapmanfd558d12010-04-02 06:18:33 +00001803struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1804{
1805 struct l2tp_session *session;
1806
1807 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1808 if (session != NULL) {
1809 session->magic = L2TP_SESSION_MAGIC;
1810 session->tunnel = tunnel;
1811
1812 session->session_id = session_id;
1813 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001814 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001815 if (tunnel->version == L2TP_HDR_VER_2)
1816 session->nr_max = 0xffff;
1817 else
1818 session->nr_max = 0xffffff;
1819 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001820 session->nr_oos_count_max = 4;
1821
1822 /* Use NR of first received packet */
1823 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001824
1825 sprintf(&session->name[0], "sess %u/%u",
1826 tunnel->tunnel_id, session->session_id);
1827
1828 skb_queue_head_init(&session->reorder_q);
1829
1830 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001831 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001832
1833 /* Inherit debug options from tunnel */
1834 session->debug = tunnel->debug;
1835
1836 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001837 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001838 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001839 session->mtu = cfg->mtu;
1840 session->mru = cfg->mru;
1841 session->send_seq = cfg->send_seq;
1842 session->recv_seq = cfg->recv_seq;
1843 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001844 session->reorder_timeout = cfg->reorder_timeout;
1845 session->offset = cfg->offset;
1846 session->l2specific_type = cfg->l2specific_type;
1847 session->l2specific_len = cfg->l2specific_len;
1848 session->cookie_len = cfg->cookie_len;
1849 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1850 session->peer_cookie_len = cfg->peer_cookie_len;
1851 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001852 }
1853
James Chapmanf7faffa2010-04-02 06:18:49 +00001854 if (tunnel->version == L2TP_HDR_VER_2)
1855 session->build_header = l2tp_build_l2tpv2_header;
1856 else
1857 session->build_header = l2tp_build_l2tpv3_header;
1858
1859 l2tp_session_set_header_len(session, tunnel->version);
1860
James Chapmanfd558d12010-04-02 06:18:33 +00001861 /* Bump the reference count. The session context is deleted
1862 * only when this drops to zero.
1863 */
1864 l2tp_session_inc_refcount(session);
1865 l2tp_tunnel_inc_refcount(tunnel);
1866
1867 /* Ensure tunnel socket isn't deleted */
1868 sock_hold(tunnel->sock);
1869
1870 /* Add session to the tunnel's hash list */
1871 write_lock_bh(&tunnel->hlist_lock);
1872 hlist_add_head(&session->hlist,
1873 l2tp_session_id_hash(tunnel, session_id));
1874 write_unlock_bh(&tunnel->hlist_lock);
1875
James Chapmanf7faffa2010-04-02 06:18:49 +00001876 /* And to the global session list if L2TPv3 */
1877 if (tunnel->version != L2TP_HDR_VER_2) {
1878 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1879
James Chapmane02d4942010-04-02 06:19:16 +00001880 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1881 hlist_add_head_rcu(&session->global_hlist,
1882 l2tp_session_id_hash_2(pn, session_id));
1883 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001884 }
1885
James Chapmanfd558d12010-04-02 06:18:33 +00001886 /* Ignore management session in session count value */
1887 if (session->session_id != 0)
1888 atomic_inc(&l2tp_session_count);
1889 }
1890
1891 return session;
1892}
1893EXPORT_SYMBOL_GPL(l2tp_session_create);
1894
1895/*****************************************************************************
1896 * Init and cleanup
1897 *****************************************************************************/
1898
1899static __net_init int l2tp_init_net(struct net *net)
1900{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001901 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001902 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001903
James Chapmanfd558d12010-04-02 06:18:33 +00001904 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001905 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001906
James Chapmanf7faffa2010-04-02 06:18:49 +00001907 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1908 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1909
James Chapmane02d4942010-04-02 06:19:16 +00001910 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001911
James Chapmanfd558d12010-04-02 06:18:33 +00001912 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001913}
1914
Tom Parkin167eb172013-01-31 23:43:03 +00001915static __net_exit void l2tp_exit_net(struct net *net)
1916{
1917 struct l2tp_net *pn = l2tp_pernet(net);
1918 struct l2tp_tunnel *tunnel = NULL;
1919
1920 rcu_read_lock_bh();
1921 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1922 (void)l2tp_tunnel_delete(tunnel);
1923 }
1924 rcu_read_unlock_bh();
1925}
1926
James Chapmanfd558d12010-04-02 06:18:33 +00001927static struct pernet_operations l2tp_net_ops = {
1928 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001929 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001930 .id = &l2tp_net_id,
1931 .size = sizeof(struct l2tp_net),
1932};
1933
1934static int __init l2tp_init(void)
1935{
1936 int rc = 0;
1937
1938 rc = register_pernet_device(&l2tp_net_ops);
1939 if (rc)
1940 goto out;
1941
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001942 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001943 if (!l2tp_wq) {
1944 pr_err("alloc_workqueue failed\n");
1945 rc = -ENOMEM;
1946 goto out;
1947 }
1948
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001949 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001950
1951out:
1952 return rc;
1953}
1954
1955static void __exit l2tp_exit(void)
1956{
1957 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001958 if (l2tp_wq) {
1959 destroy_workqueue(l2tp_wq);
1960 l2tp_wq = NULL;
1961 }
James Chapmanfd558d12010-04-02 06:18:33 +00001962}
1963
1964module_init(l2tp_init);
1965module_exit(l2tp_exit);
1966
1967MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1968MODULE_DESCRIPTION("L2TP core");
1969MODULE_LICENSE("GPL");
1970MODULE_VERSION(L2TP_DRV_VERSION);
1971