blob: aedaa2cd423705a7bcb6238721c0487ce0c58488 [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_session_set_header_len(struct l2tp_session *session, int version);
116static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000117
James Chapmanfd558d12010-04-02 06:18:33 +0000118static inline struct l2tp_net *l2tp_pernet(struct net *net)
119{
120 BUG_ON(!net);
121
122 return net_generic(net, l2tp_net_id);
123}
124
stephen hemmingerfc130842010-10-21 07:50:46 +0000125/* Tunnel reference counts. Incremented per session that is added to
126 * the tunnel.
127 */
128static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
129{
130 atomic_inc(&tunnel->ref_count);
131}
132
133static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 if (atomic_dec_and_test(&tunnel->ref_count))
136 l2tp_tunnel_free(tunnel);
137}
138#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000139#define l2tp_tunnel_inc_refcount(_t) \
140do { \
141 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
142 __func__, __LINE__, (_t)->name, \
143 atomic_read(&_t->ref_count)); \
144 l2tp_tunnel_inc_refcount_1(_t); \
145} while (0)
146#define l2tp_tunnel_dec_refcount(_t)
147do { \
148 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
149 __func__, __LINE__, (_t)->name, \
150 atomic_read(&_t->ref_count)); \
151 l2tp_tunnel_dec_refcount_1(_t); \
152} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000153#else
154#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
155#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
156#endif
157
James Chapmanf7faffa2010-04-02 06:18:49 +0000158/* Session hash global list for L2TPv3.
159 * The session_id SHOULD be random according to RFC3931, but several
160 * L2TP implementations use incrementing session_ids. So we do a real
161 * hash on the session_id, rather than a simple bitmask.
162 */
163static inline struct hlist_head *
164l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
165{
166 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
167
168}
169
Tom Parkin80d84ef2013-01-22 05:13:48 +0000170/* Lookup the tunnel socket, possibly involving the fs code if the socket is
171 * owned by userspace. A struct sock returned from this function must be
172 * released using l2tp_tunnel_sock_put once you're done with it.
173 */
174struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
175{
176 int err = 0;
177 struct socket *sock = NULL;
178 struct sock *sk = NULL;
179
180 if (!tunnel)
181 goto out;
182
183 if (tunnel->fd >= 0) {
184 /* Socket is owned by userspace, who might be in the process
185 * of closing it. Look the socket up using the fd to ensure
186 * consistency.
187 */
188 sock = sockfd_lookup(tunnel->fd, &err);
189 if (sock)
190 sk = sock->sk;
191 } else {
192 /* Socket is owned by kernelspace */
193 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000194 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000195 }
196
197out:
198 return sk;
199}
200EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
201
202/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
203void l2tp_tunnel_sock_put(struct sock *sk)
204{
205 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
206 if (tunnel) {
207 if (tunnel->fd >= 0) {
208 /* Socket is owned by userspace */
209 sockfd_put(sk->sk_socket);
210 }
211 sock_put(sk);
212 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000213 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000214}
215EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
216
James Chapmanf7faffa2010-04-02 06:18:49 +0000217/* Lookup a session by id in the global session list
218 */
219static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
220{
221 struct l2tp_net *pn = l2tp_pernet(net);
222 struct hlist_head *session_list =
223 l2tp_session_id_hash_2(pn, session_id);
224 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000225
James Chapmane02d4942010-04-02 06:19:16 +0000226 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800227 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000228 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000229 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000230 return session;
231 }
232 }
James Chapmane02d4942010-04-02 06:19:16 +0000233 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000234
235 return NULL;
236}
237
James Chapmanfd558d12010-04-02 06:18:33 +0000238/* Session hash list.
239 * The session_id SHOULD be random according to RFC2661, but several
240 * L2TP implementations (Cisco and Microsoft) use incrementing
241 * session_ids. So we do a real hash on the session_id, rather than a
242 * simple bitmask.
243 */
244static inline struct hlist_head *
245l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
246{
247 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
248}
249
250/* Lookup a session by id
251 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000252struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000253{
James Chapmanf7faffa2010-04-02 06:18:49 +0000254 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000255 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000256
James Chapmanf7faffa2010-04-02 06:18:49 +0000257 /* In L2TPv3, session_ids are unique over all tunnels and we
258 * sometimes need to look them up before we know the
259 * tunnel.
260 */
261 if (tunnel == NULL)
262 return l2tp_session_find_2(net, session_id);
263
264 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000265 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800266 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000267 if (session->session_id == session_id) {
268 read_unlock_bh(&tunnel->hlist_lock);
269 return session;
270 }
271 }
272 read_unlock_bh(&tunnel->hlist_lock);
273
274 return NULL;
275}
276EXPORT_SYMBOL_GPL(l2tp_session_find);
277
278struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
279{
280 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000281 struct l2tp_session *session;
282 int count = 0;
283
284 read_lock_bh(&tunnel->hlist_lock);
285 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800286 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000287 if (++count > nth) {
288 read_unlock_bh(&tunnel->hlist_lock);
289 return session;
290 }
291 }
292 }
293
294 read_unlock_bh(&tunnel->hlist_lock);
295
296 return NULL;
297}
298EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
299
James Chapman309795f2010-04-02 06:19:10 +0000300/* Lookup a session by interface name.
301 * This is very inefficient but is only used by management interfaces.
302 */
303struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
304{
305 struct l2tp_net *pn = l2tp_pernet(net);
306 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000307 struct l2tp_session *session;
308
James Chapmane02d4942010-04-02 06:19:16 +0000309 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000310 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800311 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000312 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000313 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000314 return session;
315 }
316 }
317 }
318
James Chapmane02d4942010-04-02 06:19:16 +0000319 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000320
321 return NULL;
322}
323EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
324
James Chapmanfd558d12010-04-02 06:18:33 +0000325/* Lookup a tunnel by id
326 */
327struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
328{
329 struct l2tp_tunnel *tunnel;
330 struct l2tp_net *pn = l2tp_pernet(net);
331
James Chapmane02d4942010-04-02 06:19:16 +0000332 rcu_read_lock_bh();
333 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000334 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000335 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000336 return tunnel;
337 }
338 }
James Chapmane02d4942010-04-02 06:19:16 +0000339 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000340
341 return NULL;
342}
343EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
344
345struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
346{
347 struct l2tp_net *pn = l2tp_pernet(net);
348 struct l2tp_tunnel *tunnel;
349 int count = 0;
350
James Chapmane02d4942010-04-02 06:19:16 +0000351 rcu_read_lock_bh();
352 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000353 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000354 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000355 return tunnel;
356 }
357 }
358
James Chapmane02d4942010-04-02 06:19:16 +0000359 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000360
361 return NULL;
362}
363EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
364
365/*****************************************************************************
366 * Receive data handling
367 *****************************************************************************/
368
369/* Queue a skb in order. We come here only if the skb has an L2TP sequence
370 * number.
371 */
372static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
373{
374 struct sk_buff *skbp;
375 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000376 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000377
378 spin_lock_bh(&session->reorder_q.lock);
379 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
380 if (L2TP_SKB_CB(skbp)->ns > ns) {
381 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000382 l2tp_dbg(session, L2TP_MSG_SEQ,
383 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
384 session->name, ns, L2TP_SKB_CB(skbp)->ns,
385 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000386 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000387 goto out;
388 }
389 }
390
391 __skb_queue_tail(&session->reorder_q, skb);
392
393out:
394 spin_unlock_bh(&session->reorder_q.lock);
395}
396
397/* Dequeue a single skb.
398 */
399static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
400{
401 struct l2tp_tunnel *tunnel = session->tunnel;
402 int length = L2TP_SKB_CB(skb)->length;
403
404 /* We're about to requeue the skb, so return resources
405 * to its current owner (a socket receive buffer).
406 */
407 skb_orphan(skb);
408
Tom Parkin7b7c0712013-03-19 06:11:22 +0000409 atomic_long_inc(&tunnel->stats.rx_packets);
410 atomic_long_add(length, &tunnel->stats.rx_bytes);
411 atomic_long_inc(&session->stats.rx_packets);
412 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000413
414 if (L2TP_SKB_CB(skb)->has_seq) {
415 /* Bump our Nr */
416 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100417 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000418
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000419 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
420 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000421 }
422
423 /* call private receive handler */
424 if (session->recv_skb != NULL)
425 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
426 else
427 kfree_skb(skb);
428
429 if (session->deref)
430 (*session->deref)(session);
431}
432
433/* Dequeue skbs from the session's reorder_q, subject to packet order.
434 * Skbs that have been in the queue for too long are simply discarded.
435 */
436static void l2tp_recv_dequeue(struct l2tp_session *session)
437{
438 struct sk_buff *skb;
439 struct sk_buff *tmp;
440
441 /* If the pkt at the head of the queue has the nr that we
442 * expect to send up next, dequeue it and any other
443 * in-sequence packets behind it.
444 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000445start:
James Chapmanfd558d12010-04-02 06:18:33 +0000446 spin_lock_bh(&session->reorder_q.lock);
447 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
448 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000449 atomic_long_inc(&session->stats.rx_seq_discards);
450 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000451 l2tp_dbg(session, L2TP_MSG_SEQ,
452 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
453 session->name, L2TP_SKB_CB(skb)->ns,
454 L2TP_SKB_CB(skb)->length, session->nr,
455 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000456 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000457 __skb_unlink(skb, &session->reorder_q);
458 kfree_skb(skb);
459 if (session->deref)
460 (*session->deref)(session);
461 continue;
462 }
463
464 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000465 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000466 l2tp_dbg(session, L2TP_MSG_SEQ,
467 "%s: advancing nr to next pkt: %u -> %u",
468 session->name, session->nr,
469 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000470 session->reorder_skip = 0;
471 session->nr = L2TP_SKB_CB(skb)->ns;
472 }
James Chapmanfd558d12010-04-02 06:18:33 +0000473 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000474 l2tp_dbg(session, L2TP_MSG_SEQ,
475 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
476 session->name, L2TP_SKB_CB(skb)->ns,
477 L2TP_SKB_CB(skb)->length, session->nr,
478 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000479 goto out;
480 }
481 }
482 __skb_unlink(skb, &session->reorder_q);
483
484 /* Process the skb. We release the queue lock while we
485 * do so to let other contexts process the queue.
486 */
487 spin_unlock_bh(&session->reorder_q.lock);
488 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000489 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000490 }
491
492out:
493 spin_unlock_bh(&session->reorder_q.lock);
494}
495
496static inline int l2tp_verify_udp_checksum(struct sock *sk,
497 struct sk_buff *skb)
498{
François Cachereule18503f2013-10-02 10:16:02 +0200499 struct l2tp_tunnel *tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
James Chapmanfd558d12010-04-02 06:18:33 +0000500 struct udphdr *uh = udp_hdr(skb);
501 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000502 __wsum psum;
503
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000504 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000505 return 0;
506
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000507#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +0200508 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000509 if (!uh->check) {
510 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
511 return 1;
512 }
513 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
514 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
515 &ipv6_hdr(skb)->daddr, ulen,
516 IPPROTO_UDP, skb->csum)) {
517 skb->ip_summed = CHECKSUM_UNNECESSARY;
518 return 0;
519 }
520 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
521 &ipv6_hdr(skb)->daddr,
522 skb->len, IPPROTO_UDP,
523 0));
524 } else
525#endif
526 {
527 struct inet_sock *inet;
528 if (!uh->check)
529 return 0;
530 inet = inet_sk(sk);
531 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
532 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000533
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000534 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
535 !csum_fold(csum_add(psum, skb->csum)))
536 return 0;
537 skb->csum = psum;
538 }
James Chapmanfd558d12010-04-02 06:18:33 +0000539
540 return __skb_checksum_complete(skb);
541}
542
James Chapman8a1631d2013-07-02 20:28:59 +0100543static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
544{
545 u32 nws;
546
547 if (nr >= session->nr)
548 nws = nr - session->nr;
549 else
550 nws = (session->nr_max + 1) - (session->nr - nr);
551
552 return nws < session->nr_window_size;
553}
554
James Chapmanb6dc01a2013-07-02 20:28:58 +0100555/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
556 * acceptable, else non-zero.
557 */
558static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
559{
James Chapman8a1631d2013-07-02 20:28:59 +0100560 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
561 /* Packet sequence number is outside allowed window.
562 * Discard it.
563 */
564 l2tp_dbg(session, L2TP_MSG_SEQ,
565 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
566 session->name, L2TP_SKB_CB(skb)->ns,
567 L2TP_SKB_CB(skb)->length, session->nr);
568 goto discard;
569 }
570
James Chapmanb6dc01a2013-07-02 20:28:58 +0100571 if (session->reorder_timeout != 0) {
572 /* Packet reordering enabled. Add skb to session's
573 * reorder queue, in order of ns.
574 */
575 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100576 goto out;
577 }
578
579 /* Packet reordering disabled. Discard out-of-sequence packets, while
580 * tracking the number if in-sequence packets after the first OOS packet
581 * is seen. After nr_oos_count_max in-sequence packets, reset the
582 * sequence number to re-enable packet reception.
583 */
584 if (L2TP_SKB_CB(skb)->ns == session->nr) {
585 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100586 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100587 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
588 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
589
590 if (nr_oos == nr_next)
591 session->nr_oos_count++;
592 else
593 session->nr_oos_count = 0;
594
595 session->nr_oos = nr_oos;
596 if (session->nr_oos_count > session->nr_oos_count_max) {
597 session->reorder_skip = 1;
598 l2tp_dbg(session, L2TP_MSG_SEQ,
599 "%s: %d oos packets received. Resetting sequence numbers\n",
600 session->name, session->nr_oos_count);
601 }
602 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100603 atomic_long_inc(&session->stats.rx_seq_discards);
604 l2tp_dbg(session, L2TP_MSG_SEQ,
605 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
606 session->name, L2TP_SKB_CB(skb)->ns,
607 L2TP_SKB_CB(skb)->length, session->nr,
608 skb_queue_len(&session->reorder_q));
609 goto discard;
610 }
611 skb_queue_tail(&session->reorder_q, skb);
612 }
613
James Chapmana0dbd822013-07-02 20:29:00 +0100614out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100615 return 0;
616
617discard:
618 return 1;
619}
620
James Chapmanf7faffa2010-04-02 06:18:49 +0000621/* Do receive processing of L2TP data frames. We handle both L2TPv2
622 * and L2TPv3 data frames here.
623 *
624 * L2TPv2 Data Message Header
625 *
626 * 0 1 2 3
627 * 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
628 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
629 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
630 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 * | Tunnel ID | Session ID |
632 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
633 * | Ns (opt) | Nr (opt) |
634 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
635 * | Offset Size (opt) | Offset pad... (opt)
636 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
637 *
638 * Data frames are marked by T=0. All other fields are the same as
639 * those in L2TP control frames.
640 *
641 * L2TPv3 Data Message Header
642 *
643 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
644 * | L2TP Session Header |
645 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
646 * | L2-Specific Sublayer |
647 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648 * | Tunnel Payload ...
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 *
651 * L2TPv3 Session Header Over IP
652 *
653 * 0 1 2 3
654 * 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
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 * | Session ID |
657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
658 * | Cookie (optional, maximum 64 bits)...
659 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
660 * |
661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
662 *
663 * L2TPv3 L2-Specific Sublayer Format
664 *
665 * 0 1 2 3
666 * 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
667 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
668 * |x|S|x|x|x|x|x|x| Sequence Number |
669 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670 *
671 * Cookie value, sublayer format and offset (pad) are negotiated with
672 * the peer when the session is set up. Unlike L2TPv2, we do not need
673 * to parse the packet header to determine if optional fields are
674 * present.
675 *
676 * Caller must already have parsed the frame and determined that it is
677 * a data (not control) frame before coming here. Fields up to the
678 * session-id have already been parsed and ptr points to the data
679 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000680 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000681void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
682 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
683 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000684{
James Chapmanf7faffa2010-04-02 06:18:49 +0000685 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000686 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000687 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000688
689 /* The ref count is increased since we now hold a pointer to
690 * the session. Take care to decrement the refcnt when exiting
691 * this function from now on...
692 */
693 l2tp_session_inc_refcount(session);
694 if (session->ref)
695 (*session->ref)(session);
696
James Chapmanf7faffa2010-04-02 06:18:49 +0000697 /* Parse and check optional cookie */
698 if (session->peer_cookie_len > 0) {
699 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000700 l2tp_info(tunnel, L2TP_MSG_DATA,
701 "%s: cookie mismatch (%u/%u). Discarding.\n",
702 tunnel->name, tunnel->tunnel_id,
703 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000704 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000705 goto discard;
706 }
707 ptr += session->peer_cookie_len;
708 }
709
James Chapmanfd558d12010-04-02 06:18:33 +0000710 /* Handle the optional sequence numbers. Sequence numbers are
711 * in different places for L2TPv2 and L2TPv3.
712 *
713 * If we are the LAC, enable/disable sequence numbers under
714 * the control of the LNS. If no sequence numbers present but
715 * we were expecting them, discard frame.
716 */
717 ns = nr = 0;
718 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000719 if (tunnel->version == L2TP_HDR_VER_2) {
720 if (hdrflags & L2TP_HDRFLAG_S) {
721 ns = ntohs(*(__be16 *) ptr);
722 ptr += 2;
723 nr = ntohs(*(__be16 *) ptr);
724 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000725
James Chapmanf7faffa2010-04-02 06:18:49 +0000726 /* Store L2TP info in the skb */
727 L2TP_SKB_CB(skb)->ns = ns;
728 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000729
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000730 l2tp_dbg(session, L2TP_MSG_SEQ,
731 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
732 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000733 }
734 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
735 u32 l2h = ntohl(*(__be32 *) ptr);
736
737 if (l2h & 0x40000000) {
738 ns = l2h & 0x00ffffff;
739
740 /* Store L2TP info in the skb */
741 L2TP_SKB_CB(skb)->ns = ns;
742 L2TP_SKB_CB(skb)->has_seq = 1;
743
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000744 l2tp_dbg(session, L2TP_MSG_SEQ,
745 "%s: recv data ns=%u, session nr=%u\n",
746 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000747 }
James Chapmanfd558d12010-04-02 06:18:33 +0000748 }
749
James Chapmanf7faffa2010-04-02 06:18:49 +0000750 /* Advance past L2-specific header, if present */
751 ptr += session->l2specific_len;
752
James Chapmanfd558d12010-04-02 06:18:33 +0000753 if (L2TP_SKB_CB(skb)->has_seq) {
754 /* Received a packet with sequence numbers. If we're the LNS,
755 * check if we sre sending sequence numbers and if not,
756 * configure it so.
757 */
758 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000759 l2tp_info(session, L2TP_MSG_SEQ,
760 "%s: requested to enable seq numbers by LNS\n",
761 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000762 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000763 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000764 }
765 } else {
766 /* No sequence numbers.
767 * If user has configured mandatory sequence numbers, discard.
768 */
769 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000770 l2tp_warn(session, L2TP_MSG_SEQ,
771 "%s: recv data has no seq numbers when required. Discarding.\n",
772 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000773 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000774 goto discard;
775 }
776
777 /* If we're the LAC and we're sending sequence numbers, the
778 * LNS has requested that we no longer send sequence numbers.
779 * If we're the LNS and we're sending sequence numbers, the
780 * LAC is broken. Discard the frame.
781 */
782 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000783 l2tp_info(session, L2TP_MSG_SEQ,
784 "%s: requested to disable seq numbers by LNS\n",
785 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000786 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000787 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000788 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000789 l2tp_warn(session, L2TP_MSG_SEQ,
790 "%s: recv data has no seq numbers when required. Discarding.\n",
791 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000792 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000793 goto discard;
794 }
795 }
796
James Chapmanf7faffa2010-04-02 06:18:49 +0000797 /* Session data offset is handled differently for L2TPv2 and
798 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
799 * the header. For L2TPv3, the offset is negotiated using AVPs
800 * in the session setup control protocol.
801 */
802 if (tunnel->version == L2TP_HDR_VER_2) {
803 /* If offset bit set, skip it. */
804 if (hdrflags & L2TP_HDRFLAG_O) {
805 offset = ntohs(*(__be16 *)ptr);
806 ptr += 2 + offset;
807 }
808 } else
809 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000810
811 offset = ptr - optr;
812 if (!pskb_may_pull(skb, offset))
813 goto discard;
814
815 __skb_pull(skb, offset);
816
817 /* If caller wants to process the payload before we queue the
818 * packet, do so now.
819 */
820 if (payload_hook)
821 if ((*payload_hook)(skb))
822 goto discard;
823
824 /* Prepare skb for adding to the session's reorder_q. Hold
825 * packets for max reorder_timeout or 1 second if not
826 * reordering.
827 */
828 L2TP_SKB_CB(skb)->length = length;
829 L2TP_SKB_CB(skb)->expires = jiffies +
830 (session->reorder_timeout ? session->reorder_timeout : HZ);
831
832 /* Add packet to the session's receive queue. Reordering is done here, if
833 * enabled. Saved L2TP protocol info is stored in skb->sb[].
834 */
835 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100836 if (l2tp_recv_data_seq(session, skb))
837 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000838 } else {
839 /* No sequence numbers. Add the skb to the tail of the
840 * reorder queue. This ensures that it will be
841 * delivered after all previous sequenced skbs.
842 */
843 skb_queue_tail(&session->reorder_q, skb);
844 }
845
846 /* Try to dequeue as many skbs from reorder_q as we can. */
847 l2tp_recv_dequeue(session);
848
849 l2tp_session_dec_refcount(session);
850
James Chapmanf7faffa2010-04-02 06:18:49 +0000851 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000852
853discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000854 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000855 kfree_skb(skb);
856
857 if (session->deref)
858 (*session->deref)(session);
859
860 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000861}
862EXPORT_SYMBOL(l2tp_recv_common);
863
Tom Parkin48f72f92013-03-19 06:11:19 +0000864/* Drop skbs from the session's reorder_q
865 */
866int l2tp_session_queue_purge(struct l2tp_session *session)
867{
868 struct sk_buff *skb = NULL;
869 BUG_ON(!session);
870 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
871 while ((skb = skb_dequeue(&session->reorder_q))) {
872 atomic_long_inc(&session->stats.rx_errors);
873 kfree_skb(skb);
874 if (session->deref)
875 (*session->deref)(session);
876 }
877 return 0;
878}
879EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
880
James Chapmanf7faffa2010-04-02 06:18:49 +0000881/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
882 * here. The skb is not on a list when we get here.
883 * Returns 0 if the packet was a data packet and was successfully passed on.
884 * Returns 1 if the packet was not a good data packet and could not be
885 * forwarded. All such packets are passed up to userspace to deal with.
886 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000887static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
888 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000889{
890 struct l2tp_session *session = NULL;
891 unsigned char *ptr, *optr;
892 u16 hdrflags;
893 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000894 u16 version;
895 int length;
896
897 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
898 goto discard_bad_csum;
899
900 /* UDP always verifies the packet length. */
901 __skb_pull(skb, sizeof(struct udphdr));
902
903 /* Short packet? */
904 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000905 l2tp_info(tunnel, L2TP_MSG_DATA,
906 "%s: recv short packet (len=%d)\n",
907 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000908 goto error;
909 }
910
James Chapmanf7faffa2010-04-02 06:18:49 +0000911 /* Trace packet contents, if enabled */
912 if (tunnel->debug & L2TP_MSG_DATA) {
913 length = min(32u, skb->len);
914 if (!pskb_may_pull(skb, length))
915 goto error;
916
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000917 pr_debug("%s: recv\n", tunnel->name);
918 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000919 }
920
Eric Dumazete50e7052011-11-08 13:59:44 -0500921 /* Point to L2TP header */
922 optr = ptr = skb->data;
923
James Chapmanf7faffa2010-04-02 06:18:49 +0000924 /* Get L2TP header flags */
925 hdrflags = ntohs(*(__be16 *) ptr);
926
927 /* Check protocol version */
928 version = hdrflags & L2TP_HDR_VER_MASK;
929 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000930 l2tp_info(tunnel, L2TP_MSG_DATA,
931 "%s: recv protocol version mismatch: got %d expected %d\n",
932 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000933 goto error;
934 }
935
936 /* Get length of L2TP packet */
937 length = skb->len;
938
939 /* If type is control packet, it is handled by userspace. */
940 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000941 l2tp_dbg(tunnel, L2TP_MSG_DATA,
942 "%s: recv control packet, len=%d\n",
943 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000944 goto error;
945 }
946
947 /* Skip flags */
948 ptr += 2;
949
950 if (tunnel->version == L2TP_HDR_VER_2) {
951 /* If length is present, skip it */
952 if (hdrflags & L2TP_HDRFLAG_L)
953 ptr += 2;
954
955 /* Extract tunnel and session ID */
956 tunnel_id = ntohs(*(__be16 *) ptr);
957 ptr += 2;
958 session_id = ntohs(*(__be16 *) ptr);
959 ptr += 2;
960 } else {
961 ptr += 2; /* skip reserved bits */
962 tunnel_id = tunnel->tunnel_id;
963 session_id = ntohl(*(__be32 *) ptr);
964 ptr += 4;
965 }
966
967 /* Find the session context */
968 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000969 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000970 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000971 l2tp_info(tunnel, L2TP_MSG_DATA,
972 "%s: no session found (%u/%u). Passing up.\n",
973 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000974 goto error;
975 }
976
977 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000978
979 return 0;
980
981discard_bad_csum:
982 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
983 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000984 atomic_long_inc(&tunnel->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000985 kfree_skb(skb);
986
987 return 0;
988
989error:
990 /* Put UDP header back */
991 __skb_push(skb, sizeof(struct udphdr));
992
993 return 1;
994}
James Chapmanfd558d12010-04-02 06:18:33 +0000995
996/* UDP encapsulation receive handler. See net/ipv4/udp.c.
997 * Return codes:
998 * 0 : success.
999 * <0: error
1000 * >0: skb should be passed up to userspace as UDP.
1001 */
1002int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1003{
1004 struct l2tp_tunnel *tunnel;
1005
1006 tunnel = l2tp_sock_to_tunnel(sk);
1007 if (tunnel == NULL)
1008 goto pass_up;
1009
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001010 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1011 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001012
1013 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1014 goto pass_up_put;
1015
1016 sock_put(sk);
1017 return 0;
1018
1019pass_up_put:
1020 sock_put(sk);
1021pass_up:
1022 return 1;
1023}
1024EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1025
1026/************************************************************************
1027 * Transmit handling
1028 ***********************************************************************/
1029
1030/* Build an L2TP header for the session into the buffer provided.
1031 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001032static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001033{
James Chapmanf7faffa2010-04-02 06:18:49 +00001034 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001035 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001036 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001037 u16 flags = L2TP_HDR_VER_2;
1038 u32 tunnel_id = tunnel->peer_tunnel_id;
1039 u32 session_id = session->peer_session_id;
1040
1041 if (session->send_seq)
1042 flags |= L2TP_HDRFLAG_S;
1043
1044 /* Setup L2TP header. */
1045 *bufp++ = htons(flags);
1046 *bufp++ = htons(tunnel_id);
1047 *bufp++ = htons(session_id);
1048 if (session->send_seq) {
1049 *bufp++ = htons(session->ns);
1050 *bufp++ = 0;
1051 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001052 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001053 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1054 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001055 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001056
1057 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001058}
1059
James Chapmanf7faffa2010-04-02 06:18:49 +00001060static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001061{
James Chapman0d767512010-04-02 06:19:00 +00001062 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001063 char *bufp = buf;
1064 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001065
James Chapman0d767512010-04-02 06:19:00 +00001066 /* Setup L2TP header. The header differs slightly for UDP and
1067 * IP encapsulations. For UDP, there is 4 bytes of flags.
1068 */
1069 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1070 u16 flags = L2TP_HDR_VER_3;
1071 *((__be16 *) bufp) = htons(flags);
1072 bufp += 2;
1073 *((__be16 *) bufp) = 0;
1074 bufp += 2;
1075 }
1076
James Chapmanf7faffa2010-04-02 06:18:49 +00001077 *((__be32 *) bufp) = htonl(session->peer_session_id);
1078 bufp += 4;
1079 if (session->cookie_len) {
1080 memcpy(bufp, &session->cookie[0], session->cookie_len);
1081 bufp += session->cookie_len;
1082 }
1083 if (session->l2specific_len) {
1084 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1085 u32 l2h = 0;
1086 if (session->send_seq) {
1087 l2h = 0x40000000 | session->ns;
1088 session->ns++;
1089 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001090 l2tp_dbg(session, L2TP_MSG_SEQ,
1091 "%s: updated ns to %u\n",
1092 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001093 }
1094
1095 *((__be32 *) bufp) = htonl(l2h);
1096 }
1097 bufp += session->l2specific_len;
1098 }
1099 if (session->offset)
1100 bufp += session->offset;
1101
1102 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001103}
James Chapmanfd558d12010-04-02 06:18:33 +00001104
stephen hemmingerfc130842010-10-21 07:50:46 +00001105static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001106 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001107{
1108 struct l2tp_tunnel *tunnel = session->tunnel;
1109 unsigned int len = skb->len;
1110 int error;
1111
1112 /* Debug */
1113 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001114 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1115 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001116 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001117 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1118 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001119
1120 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001121 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1122 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001123
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001124 pr_debug("%s: xmit\n", session->name);
1125 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1126 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001127 }
1128
1129 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001130 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001131#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001132 if (skb->sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001133 error = inet6_csk_xmit(skb, NULL);
1134 else
1135#endif
1136 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001137
1138 /* Update stats */
1139 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001140 atomic_long_inc(&tunnel->stats.tx_packets);
1141 atomic_long_add(len, &tunnel->stats.tx_bytes);
1142 atomic_long_inc(&session->stats.tx_packets);
1143 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001144 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001145 atomic_long_inc(&tunnel->stats.tx_errors);
1146 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001147 }
1148
1149 return 0;
1150}
James Chapmanfd558d12010-04-02 06:18:33 +00001151
1152/* Automatically called when the skb is freed.
1153 */
1154static void l2tp_sock_wfree(struct sk_buff *skb)
1155{
1156 sock_put(skb->sk);
1157}
1158
1159/* For data skbs that we transmit, we associate with the tunnel socket
1160 * but don't do accounting.
1161 */
1162static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1163{
1164 sock_hold(sk);
1165 skb->sk = sk;
1166 skb->destructor = l2tp_sock_wfree;
1167}
1168
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001169#if IS_ENABLED(CONFIG_IPV6)
1170static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1171 int udp_len)
1172{
1173 struct ipv6_pinfo *np = inet6_sk(sk);
1174 struct udphdr *uh = udp_hdr(skb);
1175
1176 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1177 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1178 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1179 skb->ip_summed = CHECKSUM_UNNECESSARY;
1180 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1181 IPPROTO_UDP, csum);
1182 if (uh->check == 0)
1183 uh->check = CSUM_MANGLED_0;
1184 } else {
1185 skb->ip_summed = CHECKSUM_PARTIAL;
1186 skb->csum_start = skb_transport_header(skb) - skb->head;
1187 skb->csum_offset = offsetof(struct udphdr, check);
1188 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1189 udp_len, IPPROTO_UDP, 0);
1190 }
1191}
1192#endif
1193
James Chapmanfd558d12010-04-02 06:18:33 +00001194/* If caller requires the skb to have a ppp header, the header must be
1195 * inserted in the skb data before calling this function.
1196 */
1197int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1198{
1199 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001200 struct l2tp_tunnel *tunnel = session->tunnel;
1201 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001202 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001203 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001204 struct inet_sock *inet;
1205 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001206 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001207 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1208 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001209 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001210
1211 /* Check that there's enough headroom in the skb to insert IP,
1212 * UDP and L2TP headers. If not enough, expand it to
1213 * make room. Adjust truesize.
1214 */
1215 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001216 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001217 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001218 kfree_skb(skb);
1219 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001220 }
James Chapmanfd558d12010-04-02 06:18:33 +00001221
James Chapmanfd558d12010-04-02 06:18:33 +00001222 skb_orphan(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001223 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001224 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001225
James Chapman0d767512010-04-02 06:19:00 +00001226 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001227 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1228 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1229 IPSKB_REROUTED);
1230 nf_reset(skb);
1231
David S. Miller6af88da2011-05-08 13:45:20 -07001232 bh_lock_sock(sk);
1233 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001234 kfree_skb(skb);
1235 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001236 goto out_unlock;
1237 }
1238
James Chapmanfd558d12010-04-02 06:18:33 +00001239 /* Get routing info from the tunnel socket */
1240 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001241 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001242
David S. Millerd9d8da82011-05-06 22:23:20 -07001243 inet = inet_sk(sk);
1244 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001245 switch (tunnel->encap) {
1246 case L2TP_ENCAPTYPE_UDP:
1247 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001248 __skb_push(skb, sizeof(*uh));
1249 skb_reset_transport_header(skb);
1250 uh = udp_hdr(skb);
1251 uh->source = inet->inet_sport;
1252 uh->dest = inet->inet_dport;
1253 udp_len = uhlen + hdr_len + data_len;
1254 uh->len = htons(udp_len);
1255 uh->check = 0;
1256
1257 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001258#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001259 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001260 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1261 else
1262#endif
James Chapman0d767512010-04-02 06:19:00 +00001263 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1264 skb->ip_summed = CHECKSUM_NONE;
1265 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1266 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1267 skb->ip_summed = CHECKSUM_COMPLETE;
1268 csum = skb_checksum(skb, 0, udp_len, 0);
1269 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1270 inet->inet_daddr,
1271 udp_len, IPPROTO_UDP, csum);
1272 if (uh->check == 0)
1273 uh->check = CSUM_MANGLED_0;
1274 } else {
1275 skb->ip_summed = CHECKSUM_PARTIAL;
1276 skb->csum_start = skb_transport_header(skb) - skb->head;
1277 skb->csum_offset = offsetof(struct udphdr, check);
1278 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1279 inet->inet_daddr,
1280 udp_len, IPPROTO_UDP, 0);
1281 }
1282 break;
1283
1284 case L2TP_ENCAPTYPE_IP:
1285 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001286 }
1287
James Chapman0d767512010-04-02 06:19:00 +00001288 l2tp_skb_set_owner_w(skb, sk);
1289
David S. Millerd9d8da82011-05-06 22:23:20 -07001290 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001291out_unlock:
1292 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001293
Eric Dumazetb8c84302012-06-28 20:15:13 +00001294 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001295}
1296EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1297
1298/*****************************************************************************
1299 * Tinnel and session create/destroy.
1300 *****************************************************************************/
1301
1302/* Tunnel socket destruct hook.
1303 * The tunnel context is deleted only when all session sockets have been
1304 * closed.
1305 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001306static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001307{
1308 struct l2tp_tunnel *tunnel;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001309 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001310
1311 tunnel = sk->sk_user_data;
1312 if (tunnel == NULL)
1313 goto end;
1314
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001315 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001316
James Chapmanfd558d12010-04-02 06:18:33 +00001317
Tom Parkinf8ccac02013-01-31 23:43:00 +00001318 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001319 switch (tunnel->encap) {
1320 case L2TP_ENCAPTYPE_UDP:
1321 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1322 (udp_sk(sk))->encap_type = 0;
1323 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001324 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001325 break;
1326 case L2TP_ENCAPTYPE_IP:
1327 break;
1328 }
James Chapmanfd558d12010-04-02 06:18:33 +00001329
1330 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001331 sk->sk_destruct = tunnel->old_sk_destruct;
1332 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001333 tunnel->sock = NULL;
1334
1335 /* Remove the tunnel struct from the tunnel list */
1336 pn = l2tp_pernet(tunnel->l2tp_net);
1337 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1338 list_del_rcu(&tunnel->list);
1339 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1340 atomic_dec(&l2tp_tunnel_count);
1341
1342 l2tp_tunnel_closeall(tunnel);
1343 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001344
1345 /* Call the original destructor */
1346 if (sk->sk_destruct)
1347 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001348end:
1349 return;
1350}
James Chapmanfd558d12010-04-02 06:18:33 +00001351
1352/* When the tunnel is closed, all the attached sessions need to go too.
1353 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001354void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001355{
1356 int hash;
1357 struct hlist_node *walk;
1358 struct hlist_node *tmp;
1359 struct l2tp_session *session;
1360
1361 BUG_ON(tunnel == NULL);
1362
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001363 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1364 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001365
1366 write_lock_bh(&tunnel->hlist_lock);
1367 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1368again:
1369 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1370 session = hlist_entry(walk, struct l2tp_session, hlist);
1371
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001372 l2tp_info(session, L2TP_MSG_CONTROL,
1373 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001374
1375 hlist_del_init(&session->hlist);
1376
James Chapmanfd558d12010-04-02 06:18:33 +00001377 if (session->ref != NULL)
1378 (*session->ref)(session);
1379
1380 write_unlock_bh(&tunnel->hlist_lock);
1381
Tom Parkinf6e16b22013-03-19 06:11:23 +00001382 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001383 l2tp_session_queue_purge(session);
1384
James Chapmanfd558d12010-04-02 06:18:33 +00001385 if (session->session_close != NULL)
1386 (*session->session_close)(session);
1387
1388 if (session->deref != NULL)
1389 (*session->deref)(session);
1390
Tom Parkin9980d002013-03-19 06:11:13 +00001391 l2tp_session_dec_refcount(session);
1392
James Chapmanfd558d12010-04-02 06:18:33 +00001393 write_lock_bh(&tunnel->hlist_lock);
1394
1395 /* Now restart from the beginning of this hash
1396 * chain. We always remove a session from the
1397 * list so we are guaranteed to make forward
1398 * progress.
1399 */
1400 goto again;
1401 }
1402 }
1403 write_unlock_bh(&tunnel->hlist_lock);
1404}
Tom Parkine34f4c72013-03-19 06:11:14 +00001405EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001406
Tom Parkin9980d002013-03-19 06:11:13 +00001407/* Tunnel socket destroy hook for UDP encapsulation */
1408static void l2tp_udp_encap_destroy(struct sock *sk)
1409{
1410 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1411 if (tunnel) {
1412 l2tp_tunnel_closeall(tunnel);
1413 sock_put(sk);
1414 }
1415}
1416
James Chapmanfd558d12010-04-02 06:18:33 +00001417/* Really kill the tunnel.
1418 * Come here only when all sessions have been cleared from the tunnel.
1419 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001420static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001421{
James Chapmanfd558d12010-04-02 06:18:33 +00001422 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1423 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001424 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001425 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001426}
James Chapmanfd558d12010-04-02 06:18:33 +00001427
Tom Parkinf8ccac02013-01-31 23:43:00 +00001428/* Workqueue tunnel deletion function */
1429static void l2tp_tunnel_del_work(struct work_struct *work)
1430{
1431 struct l2tp_tunnel *tunnel = NULL;
1432 struct socket *sock = NULL;
1433 struct sock *sk = NULL;
1434
1435 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1436 sk = l2tp_tunnel_sock_lookup(tunnel);
1437 if (!sk)
1438 return;
1439
1440 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001441
Tom Parkin02d13ed2013-03-19 06:11:18 +00001442 /* If the tunnel socket was created by userspace, then go through the
1443 * inet layer to shut the socket down, and let userspace close it.
1444 * Otherwise, if we created the socket directly within the kernel, use
1445 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001446 * In either case the tunnel resources are freed in the socket
1447 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001448 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001449 if (tunnel->fd >= 0) {
1450 if (sock)
1451 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001452 } else {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001453 if (sock)
1454 kernel_sock_shutdown(sock, SHUT_RDWR);
1455 sk_release_kernel(sk);
Tom Parkin167eb172013-01-31 23:43:03 +00001456 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001457
1458 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001459}
James Chapmanfd558d12010-04-02 06:18:33 +00001460
James Chapman789a4a22010-04-02 06:19:40 +00001461/* Create a socket for the tunnel, if one isn't set up by
1462 * userspace. This is used for static tunnels where there is no
1463 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001464 *
1465 * Since we don't want these sockets to keep a namespace alive by
1466 * themselves, we drop the socket's namespace refcount after creation.
1467 * These sockets are freed when the namespace exits using the pernet
1468 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001469 */
Tom Parkin167eb172013-01-31 23:43:03 +00001470static int l2tp_tunnel_sock_create(struct net *net,
1471 u32 tunnel_id,
1472 u32 peer_tunnel_id,
1473 struct l2tp_tunnel_cfg *cfg,
1474 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001475{
1476 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001477 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001478 struct sockaddr_in udp_addr = {0};
1479 struct sockaddr_l2tpip ip_addr = {0};
1480#if IS_ENABLED(CONFIG_IPV6)
1481 struct sockaddr_in6 udp6_addr = {0};
1482 struct sockaddr_l2tpip6 ip6_addr = {0};
1483#endif
James Chapman789a4a22010-04-02 06:19:40 +00001484
1485 switch (cfg->encap) {
1486 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001487#if IS_ENABLED(CONFIG_IPV6)
1488 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001489 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001490 if (err < 0)
1491 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001492
Tom Parkin167eb172013-01-31 23:43:03 +00001493 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001494
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001495 udp6_addr.sin6_family = AF_INET6;
1496 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1497 sizeof(udp6_addr.sin6_addr));
1498 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1499 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1500 sizeof(udp6_addr));
1501 if (err < 0)
1502 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001503
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001504 udp6_addr.sin6_family = AF_INET6;
1505 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1506 sizeof(udp6_addr.sin6_addr));
1507 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1508 err = kernel_connect(sock,
1509 (struct sockaddr *) &udp6_addr,
1510 sizeof(udp6_addr), 0);
1511 if (err < 0)
1512 goto out;
1513 } else
1514#endif
1515 {
Tom Parkin167eb172013-01-31 23:43:03 +00001516 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001517 if (err < 0)
1518 goto out;
1519
Tom Parkin167eb172013-01-31 23:43:03 +00001520 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001521
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001522 udp_addr.sin_family = AF_INET;
1523 udp_addr.sin_addr = cfg->local_ip;
1524 udp_addr.sin_port = htons(cfg->local_udp_port);
1525 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1526 sizeof(udp_addr));
1527 if (err < 0)
1528 goto out;
1529
1530 udp_addr.sin_family = AF_INET;
1531 udp_addr.sin_addr = cfg->peer_ip;
1532 udp_addr.sin_port = htons(cfg->peer_udp_port);
1533 err = kernel_connect(sock,
1534 (struct sockaddr *) &udp_addr,
1535 sizeof(udp_addr), 0);
1536 if (err < 0)
1537 goto out;
1538 }
James Chapman789a4a22010-04-02 06:19:40 +00001539
1540 if (!cfg->use_udp_checksums)
1541 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1542
1543 break;
1544
1545 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001546#if IS_ENABLED(CONFIG_IPV6)
1547 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001548 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1549 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001550 if (err < 0)
1551 goto out;
1552
Tom Parkin167eb172013-01-31 23:43:03 +00001553 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001554
James Chapman5dac94e2012-04-29 21:48:55 +00001555 ip6_addr.l2tp_family = AF_INET6;
1556 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1557 sizeof(ip6_addr.l2tp_addr));
1558 ip6_addr.l2tp_conn_id = tunnel_id;
1559 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1560 sizeof(ip6_addr));
1561 if (err < 0)
1562 goto out;
1563
1564 ip6_addr.l2tp_family = AF_INET6;
1565 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1566 sizeof(ip6_addr.l2tp_addr));
1567 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1568 err = kernel_connect(sock,
1569 (struct sockaddr *) &ip6_addr,
1570 sizeof(ip6_addr), 0);
1571 if (err < 0)
1572 goto out;
1573 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001574#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001575 {
Tom Parkin167eb172013-01-31 23:43:03 +00001576 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1577 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001578 if (err < 0)
1579 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001580
Tom Parkin167eb172013-01-31 23:43:03 +00001581 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001582
James Chapman5dac94e2012-04-29 21:48:55 +00001583 ip_addr.l2tp_family = AF_INET;
1584 ip_addr.l2tp_addr = cfg->local_ip;
1585 ip_addr.l2tp_conn_id = tunnel_id;
1586 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1587 sizeof(ip_addr));
1588 if (err < 0)
1589 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001590
James Chapman5dac94e2012-04-29 21:48:55 +00001591 ip_addr.l2tp_family = AF_INET;
1592 ip_addr.l2tp_addr = cfg->peer_ip;
1593 ip_addr.l2tp_conn_id = peer_tunnel_id;
1594 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1595 sizeof(ip_addr), 0);
1596 if (err < 0)
1597 goto out;
1598 }
James Chapman789a4a22010-04-02 06:19:40 +00001599 break;
1600
1601 default:
1602 goto out;
1603 }
1604
1605out:
Tom Parkin167eb172013-01-31 23:43:03 +00001606 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001607 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001608 kernel_sock_shutdown(sock, SHUT_RDWR);
1609 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001610 *sockp = NULL;
1611 }
1612
1613 return err;
1614}
1615
Eric Dumazet37159ef2012-09-04 07:18:57 +00001616static struct lock_class_key l2tp_socket_class;
1617
James Chapmanfd558d12010-04-02 06:18:33 +00001618int 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)
1619{
1620 struct l2tp_tunnel *tunnel = NULL;
1621 int err;
1622 struct socket *sock = NULL;
1623 struct sock *sk = NULL;
1624 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001625 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001626
1627 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001628 * the userspace L2TP daemon. If not specified, create a
1629 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001630 */
James Chapman789a4a22010-04-02 06:19:40 +00001631 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001632 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1633 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001634 if (err < 0)
1635 goto err;
1636 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001637 sock = sockfd_lookup(fd, &err);
1638 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001639 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001640 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001641 err = -EBADF;
1642 goto err;
1643 }
1644
1645 /* Reject namespace mismatches */
1646 if (!net_eq(sock_net(sock->sk), net)) {
1647 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1648 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001649 goto err;
1650 }
James Chapmanfd558d12010-04-02 06:18:33 +00001651 }
1652
1653 sk = sock->sk;
1654
James Chapman0d767512010-04-02 06:19:00 +00001655 if (cfg != NULL)
1656 encap = cfg->encap;
1657
James Chapmanfd558d12010-04-02 06:18:33 +00001658 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001659 switch (encap) {
1660 case L2TP_ENCAPTYPE_UDP:
1661 err = -EPROTONOSUPPORT;
1662 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001663 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001664 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1665 goto err;
1666 }
1667 break;
1668 case L2TP_ENCAPTYPE_IP:
1669 err = -EPROTONOSUPPORT;
1670 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001671 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001672 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1673 goto err;
1674 }
1675 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001676 }
1677
1678 /* Check if this socket has already been prepped */
1679 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1680 if (tunnel != NULL) {
1681 /* This socket has already been prepped */
1682 err = -EBUSY;
1683 goto err;
1684 }
1685
James Chapmanfd558d12010-04-02 06:18:33 +00001686 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1687 if (tunnel == NULL) {
1688 err = -ENOMEM;
1689 goto err;
1690 }
1691
1692 tunnel->version = version;
1693 tunnel->tunnel_id = tunnel_id;
1694 tunnel->peer_tunnel_id = peer_tunnel_id;
1695 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1696
1697 tunnel->magic = L2TP_TUNNEL_MAGIC;
1698 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1699 rwlock_init(&tunnel->hlist_lock);
1700
1701 /* The net we belong to */
1702 tunnel->l2tp_net = net;
1703 pn = l2tp_pernet(net);
1704
James Chapman0d767512010-04-02 06:19:00 +00001705 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001706 tunnel->debug = cfg->debug;
1707
François Cachereule18503f2013-10-02 10:16:02 +02001708#if IS_ENABLED(CONFIG_IPV6)
1709 if (sk->sk_family == PF_INET6) {
1710 struct ipv6_pinfo *np = inet6_sk(sk);
1711
1712 if (ipv6_addr_v4mapped(&np->saddr) &&
1713 ipv6_addr_v4mapped(&np->daddr)) {
1714 struct inet_sock *inet = inet_sk(sk);
1715
1716 tunnel->v4mapped = true;
1717 inet->inet_saddr = np->saddr.s6_addr32[3];
1718 inet->inet_rcv_saddr = np->rcv_saddr.s6_addr32[3];
1719 inet->inet_daddr = np->daddr.s6_addr32[3];
1720 } else {
1721 tunnel->v4mapped = false;
1722 }
1723 }
1724#endif
1725
James Chapmanfd558d12010-04-02 06:18:33 +00001726 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001727 tunnel->encap = encap;
1728 if (encap == L2TP_ENCAPTYPE_UDP) {
1729 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1730 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1731 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001732 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001733#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001734 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001735 udpv6_encap_enable();
1736 else
1737#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001738 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001739 }
James Chapmanfd558d12010-04-02 06:18:33 +00001740
1741 sk->sk_user_data = tunnel;
1742
1743 /* Hook on the tunnel socket destructor so that we can cleanup
1744 * if the tunnel socket goes away.
1745 */
1746 tunnel->old_sk_destruct = sk->sk_destruct;
1747 sk->sk_destruct = &l2tp_tunnel_destruct;
1748 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001749 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001750 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1751
James Chapmanfd558d12010-04-02 06:18:33 +00001752 sk->sk_allocation = GFP_ATOMIC;
1753
Tom Parkinf8ccac02013-01-31 23:43:00 +00001754 /* Init delete workqueue struct */
1755 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1756
James Chapmanfd558d12010-04-02 06:18:33 +00001757 /* Add tunnel to our list */
1758 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001759 atomic_inc(&l2tp_tunnel_count);
1760
1761 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001762 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001763 */
1764 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001765 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1766 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1767 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001768
1769 err = 0;
1770err:
1771 if (tunnelp)
1772 *tunnelp = tunnel;
1773
James Chapman789a4a22010-04-02 06:19:40 +00001774 /* If tunnel's socket was created by the kernel, it doesn't
1775 * have a file.
1776 */
1777 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001778 sockfd_put(sock);
1779
1780 return err;
1781}
1782EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1783
James Chapman309795f2010-04-02 06:19:10 +00001784/* This function is used by the netlink TUNNEL_DELETE command.
1785 */
1786int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1787{
Tom Parkin2b551c62013-03-19 06:11:16 +00001788 l2tp_tunnel_closeall(tunnel);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001789 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001790}
1791EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1792
James Chapmanfd558d12010-04-02 06:18:33 +00001793/* Really kill the session.
1794 */
1795void l2tp_session_free(struct l2tp_session *session)
1796{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001797 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001798
1799 BUG_ON(atomic_read(&session->ref_count) != 0);
1800
Tom Parkinf6e16b22013-03-19 06:11:23 +00001801 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001802 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001803 if (session->session_id != 0)
1804 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001805 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001806 session->tunnel = NULL;
1807 l2tp_tunnel_dec_refcount(tunnel);
1808 }
1809
1810 kfree(session);
1811
1812 return;
1813}
1814EXPORT_SYMBOL_GPL(l2tp_session_free);
1815
Tom Parkinf6e16b22013-03-19 06:11:23 +00001816/* Remove an l2tp session from l2tp_core's hash lists.
1817 * Provides a tidyup interface for pseudowire code which can't just route all
1818 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1819 * callback.
1820 */
1821void __l2tp_session_unhash(struct l2tp_session *session)
1822{
1823 struct l2tp_tunnel *tunnel = session->tunnel;
1824
1825 /* Remove the session from core hashes */
1826 if (tunnel) {
1827 /* Remove from the per-tunnel hash */
1828 write_lock_bh(&tunnel->hlist_lock);
1829 hlist_del_init(&session->hlist);
1830 write_unlock_bh(&tunnel->hlist_lock);
1831
1832 /* For L2TPv3 we have a per-net hash: remove from there, too */
1833 if (tunnel->version != L2TP_HDR_VER_2) {
1834 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1835 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1836 hlist_del_init_rcu(&session->global_hlist);
1837 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1838 synchronize_rcu();
1839 }
1840 }
1841}
1842EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1843
James Chapman309795f2010-04-02 06:19:10 +00001844/* This function is used by the netlink SESSION_DELETE command and by
1845 pseudowire modules.
1846 */
1847int l2tp_session_delete(struct l2tp_session *session)
1848{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001849 if (session->ref)
1850 (*session->ref)(session);
1851 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001852 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001853 if (session->session_close != NULL)
1854 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001855 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001856 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001857 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001858 return 0;
1859}
1860EXPORT_SYMBOL_GPL(l2tp_session_delete);
1861
James Chapmanf7faffa2010-04-02 06:18:49 +00001862/* We come here whenever a session's send_seq, cookie_len or
1863 * l2specific_len parameters are set.
1864 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001865static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001866{
1867 if (version == L2TP_HDR_VER_2) {
1868 session->hdr_len = 6;
1869 if (session->send_seq)
1870 session->hdr_len += 4;
1871 } else {
James Chapman0d767512010-04-02 06:19:00 +00001872 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1873 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1874 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001875 }
1876
1877}
James Chapmanf7faffa2010-04-02 06:18:49 +00001878
James Chapmanfd558d12010-04-02 06:18:33 +00001879struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1880{
1881 struct l2tp_session *session;
1882
1883 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1884 if (session != NULL) {
1885 session->magic = L2TP_SESSION_MAGIC;
1886 session->tunnel = tunnel;
1887
1888 session->session_id = session_id;
1889 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001890 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001891 if (tunnel->version == L2TP_HDR_VER_2)
1892 session->nr_max = 0xffff;
1893 else
1894 session->nr_max = 0xffffff;
1895 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001896 session->nr_oos_count_max = 4;
1897
1898 /* Use NR of first received packet */
1899 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001900
1901 sprintf(&session->name[0], "sess %u/%u",
1902 tunnel->tunnel_id, session->session_id);
1903
1904 skb_queue_head_init(&session->reorder_q);
1905
1906 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001907 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001908
1909 /* Inherit debug options from tunnel */
1910 session->debug = tunnel->debug;
1911
1912 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001913 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001914 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001915 session->mtu = cfg->mtu;
1916 session->mru = cfg->mru;
1917 session->send_seq = cfg->send_seq;
1918 session->recv_seq = cfg->recv_seq;
1919 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001920 session->reorder_timeout = cfg->reorder_timeout;
1921 session->offset = cfg->offset;
1922 session->l2specific_type = cfg->l2specific_type;
1923 session->l2specific_len = cfg->l2specific_len;
1924 session->cookie_len = cfg->cookie_len;
1925 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1926 session->peer_cookie_len = cfg->peer_cookie_len;
1927 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001928 }
1929
James Chapmanf7faffa2010-04-02 06:18:49 +00001930 if (tunnel->version == L2TP_HDR_VER_2)
1931 session->build_header = l2tp_build_l2tpv2_header;
1932 else
1933 session->build_header = l2tp_build_l2tpv3_header;
1934
1935 l2tp_session_set_header_len(session, tunnel->version);
1936
James Chapmanfd558d12010-04-02 06:18:33 +00001937 /* Bump the reference count. The session context is deleted
1938 * only when this drops to zero.
1939 */
1940 l2tp_session_inc_refcount(session);
1941 l2tp_tunnel_inc_refcount(tunnel);
1942
1943 /* Ensure tunnel socket isn't deleted */
1944 sock_hold(tunnel->sock);
1945
1946 /* Add session to the tunnel's hash list */
1947 write_lock_bh(&tunnel->hlist_lock);
1948 hlist_add_head(&session->hlist,
1949 l2tp_session_id_hash(tunnel, session_id));
1950 write_unlock_bh(&tunnel->hlist_lock);
1951
James Chapmanf7faffa2010-04-02 06:18:49 +00001952 /* And to the global session list if L2TPv3 */
1953 if (tunnel->version != L2TP_HDR_VER_2) {
1954 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1955
James Chapmane02d4942010-04-02 06:19:16 +00001956 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1957 hlist_add_head_rcu(&session->global_hlist,
1958 l2tp_session_id_hash_2(pn, session_id));
1959 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001960 }
1961
James Chapmanfd558d12010-04-02 06:18:33 +00001962 /* Ignore management session in session count value */
1963 if (session->session_id != 0)
1964 atomic_inc(&l2tp_session_count);
1965 }
1966
1967 return session;
1968}
1969EXPORT_SYMBOL_GPL(l2tp_session_create);
1970
1971/*****************************************************************************
1972 * Init and cleanup
1973 *****************************************************************************/
1974
1975static __net_init int l2tp_init_net(struct net *net)
1976{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001977 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001978 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001979
James Chapmanfd558d12010-04-02 06:18:33 +00001980 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001981 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001982
James Chapmanf7faffa2010-04-02 06:18:49 +00001983 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1984 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1985
James Chapmane02d4942010-04-02 06:19:16 +00001986 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001987
James Chapmanfd558d12010-04-02 06:18:33 +00001988 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001989}
1990
Tom Parkin167eb172013-01-31 23:43:03 +00001991static __net_exit void l2tp_exit_net(struct net *net)
1992{
1993 struct l2tp_net *pn = l2tp_pernet(net);
1994 struct l2tp_tunnel *tunnel = NULL;
1995
1996 rcu_read_lock_bh();
1997 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1998 (void)l2tp_tunnel_delete(tunnel);
1999 }
2000 rcu_read_unlock_bh();
2001}
2002
James Chapmanfd558d12010-04-02 06:18:33 +00002003static struct pernet_operations l2tp_net_ops = {
2004 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00002005 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00002006 .id = &l2tp_net_id,
2007 .size = sizeof(struct l2tp_net),
2008};
2009
2010static int __init l2tp_init(void)
2011{
2012 int rc = 0;
2013
2014 rc = register_pernet_device(&l2tp_net_ops);
2015 if (rc)
2016 goto out;
2017
Tom Parkinf8ccac02013-01-31 23:43:00 +00002018 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
2019 if (!l2tp_wq) {
2020 pr_err("alloc_workqueue failed\n");
2021 rc = -ENOMEM;
2022 goto out;
2023 }
2024
Joe Perchesa4ca44f2012-05-16 09:55:56 +00002025 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00002026
2027out:
2028 return rc;
2029}
2030
2031static void __exit l2tp_exit(void)
2032{
2033 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00002034 if (l2tp_wq) {
2035 destroy_workqueue(l2tp_wq);
2036 l2tp_wq = NULL;
2037 }
James Chapmanfd558d12010-04-02 06:18:33 +00002038}
2039
2040module_init(l2tp_init);
2041module_exit(l2tp_exit);
2042
2043MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
2044MODULE_DESCRIPTION("L2TP core");
2045MODULE_LICENSE("GPL");
2046MODULE_VERSION(L2TP_DRV_VERSION);
2047