blob: feae495a0a30accd9eb5a88baf1c23095bb38d66 [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{
499 struct udphdr *uh = udp_hdr(skb);
500 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000501 __wsum psum;
502
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000503 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000504 return 0;
505
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000506#if IS_ENABLED(CONFIG_IPV6)
507 if (sk->sk_family == PF_INET6) {
508 if (!uh->check) {
509 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
510 return 1;
511 }
512 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
513 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
514 &ipv6_hdr(skb)->daddr, ulen,
515 IPPROTO_UDP, skb->csum)) {
516 skb->ip_summed = CHECKSUM_UNNECESSARY;
517 return 0;
518 }
519 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
520 &ipv6_hdr(skb)->daddr,
521 skb->len, IPPROTO_UDP,
522 0));
523 } else
524#endif
525 {
526 struct inet_sock *inet;
527 if (!uh->check)
528 return 0;
529 inet = inet_sk(sk);
530 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
531 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000532
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000533 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
534 !csum_fold(csum_add(psum, skb->csum)))
535 return 0;
536 skb->csum = psum;
537 }
James Chapmanfd558d12010-04-02 06:18:33 +0000538
539 return __skb_checksum_complete(skb);
540}
541
James Chapman8a1631d2013-07-02 20:28:59 +0100542static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
543{
544 u32 nws;
545
546 if (nr >= session->nr)
547 nws = nr - session->nr;
548 else
549 nws = (session->nr_max + 1) - (session->nr - nr);
550
551 return nws < session->nr_window_size;
552}
553
James Chapmanb6dc01a2013-07-02 20:28:58 +0100554/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
555 * acceptable, else non-zero.
556 */
557static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
558{
James Chapman8a1631d2013-07-02 20:28:59 +0100559 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
560 /* Packet sequence number is outside allowed window.
561 * Discard it.
562 */
563 l2tp_dbg(session, L2TP_MSG_SEQ,
564 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
565 session->name, L2TP_SKB_CB(skb)->ns,
566 L2TP_SKB_CB(skb)->length, session->nr);
567 goto discard;
568 }
569
James Chapmanb6dc01a2013-07-02 20:28:58 +0100570 if (session->reorder_timeout != 0) {
571 /* Packet reordering enabled. Add skb to session's
572 * reorder queue, in order of ns.
573 */
574 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100575 goto out;
576 }
577
578 /* Packet reordering disabled. Discard out-of-sequence packets, while
579 * tracking the number if in-sequence packets after the first OOS packet
580 * is seen. After nr_oos_count_max in-sequence packets, reset the
581 * sequence number to re-enable packet reception.
582 */
583 if (L2TP_SKB_CB(skb)->ns == session->nr) {
584 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100585 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100586 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
587 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
588
589 if (nr_oos == nr_next)
590 session->nr_oos_count++;
591 else
592 session->nr_oos_count = 0;
593
594 session->nr_oos = nr_oos;
595 if (session->nr_oos_count > session->nr_oos_count_max) {
596 session->reorder_skip = 1;
597 l2tp_dbg(session, L2TP_MSG_SEQ,
598 "%s: %d oos packets received. Resetting sequence numbers\n",
599 session->name, session->nr_oos_count);
600 }
601 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100602 atomic_long_inc(&session->stats.rx_seq_discards);
603 l2tp_dbg(session, L2TP_MSG_SEQ,
604 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
605 session->name, L2TP_SKB_CB(skb)->ns,
606 L2TP_SKB_CB(skb)->length, session->nr,
607 skb_queue_len(&session->reorder_q));
608 goto discard;
609 }
610 skb_queue_tail(&session->reorder_q, skb);
611 }
612
James Chapmana0dbd822013-07-02 20:29:00 +0100613out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100614 return 0;
615
616discard:
617 return 1;
618}
619
James Chapmanf7faffa2010-04-02 06:18:49 +0000620/* Do receive processing of L2TP data frames. We handle both L2TPv2
621 * and L2TPv3 data frames here.
622 *
623 * L2TPv2 Data Message Header
624 *
625 * 0 1 2 3
626 * 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
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 * | Tunnel ID | Session ID |
631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * | Ns (opt) | Nr (opt) |
633 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * | Offset Size (opt) | Offset pad... (opt)
635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636 *
637 * Data frames are marked by T=0. All other fields are the same as
638 * those in L2TP control frames.
639 *
640 * L2TPv3 Data Message Header
641 *
642 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
643 * | L2TP Session Header |
644 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645 * | L2-Specific Sublayer |
646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
647 * | Tunnel Payload ...
648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
649 *
650 * L2TPv3 Session Header Over IP
651 *
652 * 0 1 2 3
653 * 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
654 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655 * | Session ID |
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 * | Cookie (optional, maximum 64 bits)...
658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 * |
660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661 *
662 * L2TPv3 L2-Specific Sublayer Format
663 *
664 * 0 1 2 3
665 * 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
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * |x|S|x|x|x|x|x|x| Sequence Number |
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 *
670 * Cookie value, sublayer format and offset (pad) are negotiated with
671 * the peer when the session is set up. Unlike L2TPv2, we do not need
672 * to parse the packet header to determine if optional fields are
673 * present.
674 *
675 * Caller must already have parsed the frame and determined that it is
676 * a data (not control) frame before coming here. Fields up to the
677 * session-id have already been parsed and ptr points to the data
678 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000679 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000680void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
681 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
682 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000683{
James Chapmanf7faffa2010-04-02 06:18:49 +0000684 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000685 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000686 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000687
688 /* The ref count is increased since we now hold a pointer to
689 * the session. Take care to decrement the refcnt when exiting
690 * this function from now on...
691 */
692 l2tp_session_inc_refcount(session);
693 if (session->ref)
694 (*session->ref)(session);
695
James Chapmanf7faffa2010-04-02 06:18:49 +0000696 /* Parse and check optional cookie */
697 if (session->peer_cookie_len > 0) {
698 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000699 l2tp_info(tunnel, L2TP_MSG_DATA,
700 "%s: cookie mismatch (%u/%u). Discarding.\n",
701 tunnel->name, tunnel->tunnel_id,
702 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000703 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000704 goto discard;
705 }
706 ptr += session->peer_cookie_len;
707 }
708
James Chapmanfd558d12010-04-02 06:18:33 +0000709 /* Handle the optional sequence numbers. Sequence numbers are
710 * in different places for L2TPv2 and L2TPv3.
711 *
712 * If we are the LAC, enable/disable sequence numbers under
713 * the control of the LNS. If no sequence numbers present but
714 * we were expecting them, discard frame.
715 */
716 ns = nr = 0;
717 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000718 if (tunnel->version == L2TP_HDR_VER_2) {
719 if (hdrflags & L2TP_HDRFLAG_S) {
720 ns = ntohs(*(__be16 *) ptr);
721 ptr += 2;
722 nr = ntohs(*(__be16 *) ptr);
723 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000724
James Chapmanf7faffa2010-04-02 06:18:49 +0000725 /* Store L2TP info in the skb */
726 L2TP_SKB_CB(skb)->ns = ns;
727 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000728
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000729 l2tp_dbg(session, L2TP_MSG_SEQ,
730 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
731 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000732 }
733 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
734 u32 l2h = ntohl(*(__be32 *) ptr);
735
736 if (l2h & 0x40000000) {
737 ns = l2h & 0x00ffffff;
738
739 /* Store L2TP info in the skb */
740 L2TP_SKB_CB(skb)->ns = ns;
741 L2TP_SKB_CB(skb)->has_seq = 1;
742
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000743 l2tp_dbg(session, L2TP_MSG_SEQ,
744 "%s: recv data ns=%u, session nr=%u\n",
745 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000746 }
James Chapmanfd558d12010-04-02 06:18:33 +0000747 }
748
James Chapmanf7faffa2010-04-02 06:18:49 +0000749 /* Advance past L2-specific header, if present */
750 ptr += session->l2specific_len;
751
James Chapmanfd558d12010-04-02 06:18:33 +0000752 if (L2TP_SKB_CB(skb)->has_seq) {
753 /* Received a packet with sequence numbers. If we're the LNS,
754 * check if we sre sending sequence numbers and if not,
755 * configure it so.
756 */
757 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000758 l2tp_info(session, L2TP_MSG_SEQ,
759 "%s: requested to enable seq numbers by LNS\n",
760 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000761 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000762 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000763 }
764 } else {
765 /* No sequence numbers.
766 * If user has configured mandatory sequence numbers, discard.
767 */
768 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000769 l2tp_warn(session, L2TP_MSG_SEQ,
770 "%s: recv data has no seq numbers when required. Discarding.\n",
771 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000772 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000773 goto discard;
774 }
775
776 /* If we're the LAC and we're sending sequence numbers, the
777 * LNS has requested that we no longer send sequence numbers.
778 * If we're the LNS and we're sending sequence numbers, the
779 * LAC is broken. Discard the frame.
780 */
781 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000782 l2tp_info(session, L2TP_MSG_SEQ,
783 "%s: requested to disable seq numbers by LNS\n",
784 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000785 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000786 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000787 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000788 l2tp_warn(session, L2TP_MSG_SEQ,
789 "%s: recv data has no seq numbers when required. Discarding.\n",
790 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000791 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000792 goto discard;
793 }
794 }
795
James Chapmanf7faffa2010-04-02 06:18:49 +0000796 /* Session data offset is handled differently for L2TPv2 and
797 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
798 * the header. For L2TPv3, the offset is negotiated using AVPs
799 * in the session setup control protocol.
800 */
801 if (tunnel->version == L2TP_HDR_VER_2) {
802 /* If offset bit set, skip it. */
803 if (hdrflags & L2TP_HDRFLAG_O) {
804 offset = ntohs(*(__be16 *)ptr);
805 ptr += 2 + offset;
806 }
807 } else
808 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000809
810 offset = ptr - optr;
811 if (!pskb_may_pull(skb, offset))
812 goto discard;
813
814 __skb_pull(skb, offset);
815
816 /* If caller wants to process the payload before we queue the
817 * packet, do so now.
818 */
819 if (payload_hook)
820 if ((*payload_hook)(skb))
821 goto discard;
822
823 /* Prepare skb for adding to the session's reorder_q. Hold
824 * packets for max reorder_timeout or 1 second if not
825 * reordering.
826 */
827 L2TP_SKB_CB(skb)->length = length;
828 L2TP_SKB_CB(skb)->expires = jiffies +
829 (session->reorder_timeout ? session->reorder_timeout : HZ);
830
831 /* Add packet to the session's receive queue. Reordering is done here, if
832 * enabled. Saved L2TP protocol info is stored in skb->sb[].
833 */
834 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100835 if (l2tp_recv_data_seq(session, skb))
836 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000837 } else {
838 /* No sequence numbers. Add the skb to the tail of the
839 * reorder queue. This ensures that it will be
840 * delivered after all previous sequenced skbs.
841 */
842 skb_queue_tail(&session->reorder_q, skb);
843 }
844
845 /* Try to dequeue as many skbs from reorder_q as we can. */
846 l2tp_recv_dequeue(session);
847
848 l2tp_session_dec_refcount(session);
849
James Chapmanf7faffa2010-04-02 06:18:49 +0000850 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000851
852discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000853 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000854 kfree_skb(skb);
855
856 if (session->deref)
857 (*session->deref)(session);
858
859 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000860}
861EXPORT_SYMBOL(l2tp_recv_common);
862
Tom Parkin48f72f92013-03-19 06:11:19 +0000863/* Drop skbs from the session's reorder_q
864 */
865int l2tp_session_queue_purge(struct l2tp_session *session)
866{
867 struct sk_buff *skb = NULL;
868 BUG_ON(!session);
869 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
870 while ((skb = skb_dequeue(&session->reorder_q))) {
871 atomic_long_inc(&session->stats.rx_errors);
872 kfree_skb(skb);
873 if (session->deref)
874 (*session->deref)(session);
875 }
876 return 0;
877}
878EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
879
James Chapmanf7faffa2010-04-02 06:18:49 +0000880/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
881 * here. The skb is not on a list when we get here.
882 * Returns 0 if the packet was a data packet and was successfully passed on.
883 * Returns 1 if the packet was not a good data packet and could not be
884 * forwarded. All such packets are passed up to userspace to deal with.
885 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000886static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
887 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000888{
889 struct l2tp_session *session = NULL;
890 unsigned char *ptr, *optr;
891 u16 hdrflags;
892 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000893 u16 version;
894 int length;
895
896 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
897 goto discard_bad_csum;
898
899 /* UDP always verifies the packet length. */
900 __skb_pull(skb, sizeof(struct udphdr));
901
902 /* Short packet? */
903 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000904 l2tp_info(tunnel, L2TP_MSG_DATA,
905 "%s: recv short packet (len=%d)\n",
906 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000907 goto error;
908 }
909
James Chapmanf7faffa2010-04-02 06:18:49 +0000910 /* Trace packet contents, if enabled */
911 if (tunnel->debug & L2TP_MSG_DATA) {
912 length = min(32u, skb->len);
913 if (!pskb_may_pull(skb, length))
914 goto error;
915
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000916 pr_debug("%s: recv\n", tunnel->name);
917 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000918 }
919
Eric Dumazete50e7052011-11-08 13:59:44 -0500920 /* Point to L2TP header */
921 optr = ptr = skb->data;
922
James Chapmanf7faffa2010-04-02 06:18:49 +0000923 /* Get L2TP header flags */
924 hdrflags = ntohs(*(__be16 *) ptr);
925
926 /* Check protocol version */
927 version = hdrflags & L2TP_HDR_VER_MASK;
928 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000929 l2tp_info(tunnel, L2TP_MSG_DATA,
930 "%s: recv protocol version mismatch: got %d expected %d\n",
931 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000932 goto error;
933 }
934
935 /* Get length of L2TP packet */
936 length = skb->len;
937
938 /* If type is control packet, it is handled by userspace. */
939 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000940 l2tp_dbg(tunnel, L2TP_MSG_DATA,
941 "%s: recv control packet, len=%d\n",
942 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000943 goto error;
944 }
945
946 /* Skip flags */
947 ptr += 2;
948
949 if (tunnel->version == L2TP_HDR_VER_2) {
950 /* If length is present, skip it */
951 if (hdrflags & L2TP_HDRFLAG_L)
952 ptr += 2;
953
954 /* Extract tunnel and session ID */
955 tunnel_id = ntohs(*(__be16 *) ptr);
956 ptr += 2;
957 session_id = ntohs(*(__be16 *) ptr);
958 ptr += 2;
959 } else {
960 ptr += 2; /* skip reserved bits */
961 tunnel_id = tunnel->tunnel_id;
962 session_id = ntohl(*(__be32 *) ptr);
963 ptr += 4;
964 }
965
966 /* Find the session context */
967 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000968 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000969 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000970 l2tp_info(tunnel, L2TP_MSG_DATA,
971 "%s: no session found (%u/%u). Passing up.\n",
972 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000973 goto error;
974 }
975
976 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000977
978 return 0;
979
980discard_bad_csum:
981 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
982 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000983 atomic_long_inc(&tunnel->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000984 kfree_skb(skb);
985
986 return 0;
987
988error:
989 /* Put UDP header back */
990 __skb_push(skb, sizeof(struct udphdr));
991
992 return 1;
993}
James Chapmanfd558d12010-04-02 06:18:33 +0000994
995/* UDP encapsulation receive handler. See net/ipv4/udp.c.
996 * Return codes:
997 * 0 : success.
998 * <0: error
999 * >0: skb should be passed up to userspace as UDP.
1000 */
1001int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1002{
1003 struct l2tp_tunnel *tunnel;
1004
1005 tunnel = l2tp_sock_to_tunnel(sk);
1006 if (tunnel == NULL)
1007 goto pass_up;
1008
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001009 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1010 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001011
1012 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1013 goto pass_up_put;
1014
1015 sock_put(sk);
1016 return 0;
1017
1018pass_up_put:
1019 sock_put(sk);
1020pass_up:
1021 return 1;
1022}
1023EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1024
1025/************************************************************************
1026 * Transmit handling
1027 ***********************************************************************/
1028
1029/* Build an L2TP header for the session into the buffer provided.
1030 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001031static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001032{
James Chapmanf7faffa2010-04-02 06:18:49 +00001033 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001034 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001035 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001036 u16 flags = L2TP_HDR_VER_2;
1037 u32 tunnel_id = tunnel->peer_tunnel_id;
1038 u32 session_id = session->peer_session_id;
1039
1040 if (session->send_seq)
1041 flags |= L2TP_HDRFLAG_S;
1042
1043 /* Setup L2TP header. */
1044 *bufp++ = htons(flags);
1045 *bufp++ = htons(tunnel_id);
1046 *bufp++ = htons(session_id);
1047 if (session->send_seq) {
1048 *bufp++ = htons(session->ns);
1049 *bufp++ = 0;
1050 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001051 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001052 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1053 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001054 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001055
1056 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001057}
1058
James Chapmanf7faffa2010-04-02 06:18:49 +00001059static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001060{
James Chapman0d767512010-04-02 06:19:00 +00001061 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001062 char *bufp = buf;
1063 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001064
James Chapman0d767512010-04-02 06:19:00 +00001065 /* Setup L2TP header. The header differs slightly for UDP and
1066 * IP encapsulations. For UDP, there is 4 bytes of flags.
1067 */
1068 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1069 u16 flags = L2TP_HDR_VER_3;
1070 *((__be16 *) bufp) = htons(flags);
1071 bufp += 2;
1072 *((__be16 *) bufp) = 0;
1073 bufp += 2;
1074 }
1075
James Chapmanf7faffa2010-04-02 06:18:49 +00001076 *((__be32 *) bufp) = htonl(session->peer_session_id);
1077 bufp += 4;
1078 if (session->cookie_len) {
1079 memcpy(bufp, &session->cookie[0], session->cookie_len);
1080 bufp += session->cookie_len;
1081 }
1082 if (session->l2specific_len) {
1083 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1084 u32 l2h = 0;
1085 if (session->send_seq) {
1086 l2h = 0x40000000 | session->ns;
1087 session->ns++;
1088 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001089 l2tp_dbg(session, L2TP_MSG_SEQ,
1090 "%s: updated ns to %u\n",
1091 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001092 }
1093
1094 *((__be32 *) bufp) = htonl(l2h);
1095 }
1096 bufp += session->l2specific_len;
1097 }
1098 if (session->offset)
1099 bufp += session->offset;
1100
1101 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001102}
James Chapmanfd558d12010-04-02 06:18:33 +00001103
stephen hemmingerfc130842010-10-21 07:50:46 +00001104static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001105 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001106{
1107 struct l2tp_tunnel *tunnel = session->tunnel;
1108 unsigned int len = skb->len;
1109 int error;
1110
1111 /* Debug */
1112 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001113 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1114 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001115 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001116 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1117 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001118
1119 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001120 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1121 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001122
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001123 pr_debug("%s: xmit\n", session->name);
1124 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1125 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001126 }
1127
1128 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001129 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001130#if IS_ENABLED(CONFIG_IPV6)
1131 if (skb->sk->sk_family == PF_INET6)
1132 error = inet6_csk_xmit(skb, NULL);
1133 else
1134#endif
1135 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001136
1137 /* Update stats */
1138 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001139 atomic_long_inc(&tunnel->stats.tx_packets);
1140 atomic_long_add(len, &tunnel->stats.tx_bytes);
1141 atomic_long_inc(&session->stats.tx_packets);
1142 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001143 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001144 atomic_long_inc(&tunnel->stats.tx_errors);
1145 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001146 }
1147
1148 return 0;
1149}
James Chapmanfd558d12010-04-02 06:18:33 +00001150
1151/* Automatically called when the skb is freed.
1152 */
1153static void l2tp_sock_wfree(struct sk_buff *skb)
1154{
1155 sock_put(skb->sk);
1156}
1157
1158/* For data skbs that we transmit, we associate with the tunnel socket
1159 * but don't do accounting.
1160 */
1161static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1162{
1163 sock_hold(sk);
1164 skb->sk = sk;
1165 skb->destructor = l2tp_sock_wfree;
1166}
1167
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001168#if IS_ENABLED(CONFIG_IPV6)
1169static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1170 int udp_len)
1171{
1172 struct ipv6_pinfo *np = inet6_sk(sk);
1173 struct udphdr *uh = udp_hdr(skb);
1174
1175 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1176 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1177 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1178 skb->ip_summed = CHECKSUM_UNNECESSARY;
1179 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1180 IPPROTO_UDP, csum);
1181 if (uh->check == 0)
1182 uh->check = CSUM_MANGLED_0;
1183 } else {
1184 skb->ip_summed = CHECKSUM_PARTIAL;
1185 skb->csum_start = skb_transport_header(skb) - skb->head;
1186 skb->csum_offset = offsetof(struct udphdr, check);
1187 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1188 udp_len, IPPROTO_UDP, 0);
1189 }
1190}
1191#endif
1192
James Chapmanfd558d12010-04-02 06:18:33 +00001193/* If caller requires the skb to have a ppp header, the header must be
1194 * inserted in the skb data before calling this function.
1195 */
1196int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1197{
1198 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001199 struct l2tp_tunnel *tunnel = session->tunnel;
1200 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001201 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001202 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001203 struct inet_sock *inet;
1204 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001205 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001206 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1207 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001208 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001209
1210 /* Check that there's enough headroom in the skb to insert IP,
1211 * UDP and L2TP headers. If not enough, expand it to
1212 * make room. Adjust truesize.
1213 */
1214 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001215 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001216 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001217 kfree_skb(skb);
1218 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001219 }
James Chapmanfd558d12010-04-02 06:18:33 +00001220
James Chapmanfd558d12010-04-02 06:18:33 +00001221 skb_orphan(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001222 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001223 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001224
James Chapman0d767512010-04-02 06:19:00 +00001225 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001226 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1227 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1228 IPSKB_REROUTED);
1229 nf_reset(skb);
1230
David S. Miller6af88da2011-05-08 13:45:20 -07001231 bh_lock_sock(sk);
1232 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001233 kfree_skb(skb);
1234 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001235 goto out_unlock;
1236 }
1237
James Chapmanfd558d12010-04-02 06:18:33 +00001238 /* Get routing info from the tunnel socket */
1239 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001240 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001241
David S. Millerd9d8da82011-05-06 22:23:20 -07001242 inet = inet_sk(sk);
1243 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001244 switch (tunnel->encap) {
1245 case L2TP_ENCAPTYPE_UDP:
1246 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001247 __skb_push(skb, sizeof(*uh));
1248 skb_reset_transport_header(skb);
1249 uh = udp_hdr(skb);
1250 uh->source = inet->inet_sport;
1251 uh->dest = inet->inet_dport;
1252 udp_len = uhlen + hdr_len + data_len;
1253 uh->len = htons(udp_len);
1254 uh->check = 0;
1255
1256 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001257#if IS_ENABLED(CONFIG_IPV6)
1258 if (sk->sk_family == PF_INET6)
1259 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1260 else
1261#endif
James Chapman0d767512010-04-02 06:19:00 +00001262 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1263 skb->ip_summed = CHECKSUM_NONE;
1264 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1265 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1266 skb->ip_summed = CHECKSUM_COMPLETE;
1267 csum = skb_checksum(skb, 0, udp_len, 0);
1268 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1269 inet->inet_daddr,
1270 udp_len, IPPROTO_UDP, csum);
1271 if (uh->check == 0)
1272 uh->check = CSUM_MANGLED_0;
1273 } else {
1274 skb->ip_summed = CHECKSUM_PARTIAL;
1275 skb->csum_start = skb_transport_header(skb) - skb->head;
1276 skb->csum_offset = offsetof(struct udphdr, check);
1277 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1278 inet->inet_daddr,
1279 udp_len, IPPROTO_UDP, 0);
1280 }
1281 break;
1282
1283 case L2TP_ENCAPTYPE_IP:
1284 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001285 }
1286
James Chapman0d767512010-04-02 06:19:00 +00001287 l2tp_skb_set_owner_w(skb, sk);
1288
David S. Millerd9d8da82011-05-06 22:23:20 -07001289 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001290out_unlock:
1291 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001292
Eric Dumazetb8c84302012-06-28 20:15:13 +00001293 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001294}
1295EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1296
1297/*****************************************************************************
1298 * Tinnel and session create/destroy.
1299 *****************************************************************************/
1300
1301/* Tunnel socket destruct hook.
1302 * The tunnel context is deleted only when all session sockets have been
1303 * closed.
1304 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001305static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001306{
1307 struct l2tp_tunnel *tunnel;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001308 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001309
1310 tunnel = sk->sk_user_data;
1311 if (tunnel == NULL)
1312 goto end;
1313
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001314 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001315
James Chapmanfd558d12010-04-02 06:18:33 +00001316
Tom Parkinf8ccac02013-01-31 23:43:00 +00001317 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001318 switch (tunnel->encap) {
1319 case L2TP_ENCAPTYPE_UDP:
1320 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1321 (udp_sk(sk))->encap_type = 0;
1322 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001323 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001324 break;
1325 case L2TP_ENCAPTYPE_IP:
1326 break;
1327 }
James Chapmanfd558d12010-04-02 06:18:33 +00001328
1329 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001330 sk->sk_destruct = tunnel->old_sk_destruct;
1331 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001332 tunnel->sock = NULL;
1333
1334 /* Remove the tunnel struct from the tunnel list */
1335 pn = l2tp_pernet(tunnel->l2tp_net);
1336 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1337 list_del_rcu(&tunnel->list);
1338 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1339 atomic_dec(&l2tp_tunnel_count);
1340
1341 l2tp_tunnel_closeall(tunnel);
1342 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001343
1344 /* Call the original destructor */
1345 if (sk->sk_destruct)
1346 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001347end:
1348 return;
1349}
James Chapmanfd558d12010-04-02 06:18:33 +00001350
1351/* When the tunnel is closed, all the attached sessions need to go too.
1352 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001353void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001354{
1355 int hash;
1356 struct hlist_node *walk;
1357 struct hlist_node *tmp;
1358 struct l2tp_session *session;
1359
1360 BUG_ON(tunnel == NULL);
1361
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001362 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1363 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001364
1365 write_lock_bh(&tunnel->hlist_lock);
1366 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1367again:
1368 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1369 session = hlist_entry(walk, struct l2tp_session, hlist);
1370
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001371 l2tp_info(session, L2TP_MSG_CONTROL,
1372 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001373
1374 hlist_del_init(&session->hlist);
1375
James Chapmanfd558d12010-04-02 06:18:33 +00001376 if (session->ref != NULL)
1377 (*session->ref)(session);
1378
1379 write_unlock_bh(&tunnel->hlist_lock);
1380
Tom Parkinf6e16b22013-03-19 06:11:23 +00001381 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001382 l2tp_session_queue_purge(session);
1383
James Chapmanfd558d12010-04-02 06:18:33 +00001384 if (session->session_close != NULL)
1385 (*session->session_close)(session);
1386
1387 if (session->deref != NULL)
1388 (*session->deref)(session);
1389
Tom Parkin9980d002013-03-19 06:11:13 +00001390 l2tp_session_dec_refcount(session);
1391
James Chapmanfd558d12010-04-02 06:18:33 +00001392 write_lock_bh(&tunnel->hlist_lock);
1393
1394 /* Now restart from the beginning of this hash
1395 * chain. We always remove a session from the
1396 * list so we are guaranteed to make forward
1397 * progress.
1398 */
1399 goto again;
1400 }
1401 }
1402 write_unlock_bh(&tunnel->hlist_lock);
1403}
Tom Parkine34f4c72013-03-19 06:11:14 +00001404EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001405
Tom Parkin9980d002013-03-19 06:11:13 +00001406/* Tunnel socket destroy hook for UDP encapsulation */
1407static void l2tp_udp_encap_destroy(struct sock *sk)
1408{
1409 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1410 if (tunnel) {
1411 l2tp_tunnel_closeall(tunnel);
1412 sock_put(sk);
1413 }
1414}
1415
James Chapmanfd558d12010-04-02 06:18:33 +00001416/* Really kill the tunnel.
1417 * Come here only when all sessions have been cleared from the tunnel.
1418 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001419static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001420{
James Chapmanfd558d12010-04-02 06:18:33 +00001421 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1422 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001423 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001424 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001425}
James Chapmanfd558d12010-04-02 06:18:33 +00001426
Tom Parkinf8ccac02013-01-31 23:43:00 +00001427/* Workqueue tunnel deletion function */
1428static void l2tp_tunnel_del_work(struct work_struct *work)
1429{
1430 struct l2tp_tunnel *tunnel = NULL;
1431 struct socket *sock = NULL;
1432 struct sock *sk = NULL;
1433
1434 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1435 sk = l2tp_tunnel_sock_lookup(tunnel);
1436 if (!sk)
1437 return;
1438
1439 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001440
Tom Parkin02d13ed2013-03-19 06:11:18 +00001441 /* If the tunnel socket was created by userspace, then go through the
1442 * inet layer to shut the socket down, and let userspace close it.
1443 * Otherwise, if we created the socket directly within the kernel, use
1444 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001445 * In either case the tunnel resources are freed in the socket
1446 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001447 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001448 if (tunnel->fd >= 0) {
1449 if (sock)
1450 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001451 } else {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001452 if (sock)
1453 kernel_sock_shutdown(sock, SHUT_RDWR);
1454 sk_release_kernel(sk);
Tom Parkin167eb172013-01-31 23:43:03 +00001455 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001456
1457 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001458}
James Chapmanfd558d12010-04-02 06:18:33 +00001459
James Chapman789a4a22010-04-02 06:19:40 +00001460/* Create a socket for the tunnel, if one isn't set up by
1461 * userspace. This is used for static tunnels where there is no
1462 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001463 *
1464 * Since we don't want these sockets to keep a namespace alive by
1465 * themselves, we drop the socket's namespace refcount after creation.
1466 * These sockets are freed when the namespace exits using the pernet
1467 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001468 */
Tom Parkin167eb172013-01-31 23:43:03 +00001469static int l2tp_tunnel_sock_create(struct net *net,
1470 u32 tunnel_id,
1471 u32 peer_tunnel_id,
1472 struct l2tp_tunnel_cfg *cfg,
1473 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001474{
1475 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001476 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001477 struct sockaddr_in udp_addr = {0};
1478 struct sockaddr_l2tpip ip_addr = {0};
1479#if IS_ENABLED(CONFIG_IPV6)
1480 struct sockaddr_in6 udp6_addr = {0};
1481 struct sockaddr_l2tpip6 ip6_addr = {0};
1482#endif
James Chapman789a4a22010-04-02 06:19:40 +00001483
1484 switch (cfg->encap) {
1485 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001486#if IS_ENABLED(CONFIG_IPV6)
1487 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001488 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001489 if (err < 0)
1490 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001491
Tom Parkin167eb172013-01-31 23:43:03 +00001492 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001493
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001494 udp6_addr.sin6_family = AF_INET6;
1495 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1496 sizeof(udp6_addr.sin6_addr));
1497 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1498 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1499 sizeof(udp6_addr));
1500 if (err < 0)
1501 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001502
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001503 udp6_addr.sin6_family = AF_INET6;
1504 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1505 sizeof(udp6_addr.sin6_addr));
1506 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1507 err = kernel_connect(sock,
1508 (struct sockaddr *) &udp6_addr,
1509 sizeof(udp6_addr), 0);
1510 if (err < 0)
1511 goto out;
1512 } else
1513#endif
1514 {
Tom Parkin167eb172013-01-31 23:43:03 +00001515 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001516 if (err < 0)
1517 goto out;
1518
Tom Parkin167eb172013-01-31 23:43:03 +00001519 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001520
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001521 udp_addr.sin_family = AF_INET;
1522 udp_addr.sin_addr = cfg->local_ip;
1523 udp_addr.sin_port = htons(cfg->local_udp_port);
1524 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1525 sizeof(udp_addr));
1526 if (err < 0)
1527 goto out;
1528
1529 udp_addr.sin_family = AF_INET;
1530 udp_addr.sin_addr = cfg->peer_ip;
1531 udp_addr.sin_port = htons(cfg->peer_udp_port);
1532 err = kernel_connect(sock,
1533 (struct sockaddr *) &udp_addr,
1534 sizeof(udp_addr), 0);
1535 if (err < 0)
1536 goto out;
1537 }
James Chapman789a4a22010-04-02 06:19:40 +00001538
1539 if (!cfg->use_udp_checksums)
1540 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1541
1542 break;
1543
1544 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001545#if IS_ENABLED(CONFIG_IPV6)
1546 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001547 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1548 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001549 if (err < 0)
1550 goto out;
1551
Tom Parkin167eb172013-01-31 23:43:03 +00001552 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001553
James Chapman5dac94e2012-04-29 21:48:55 +00001554 ip6_addr.l2tp_family = AF_INET6;
1555 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1556 sizeof(ip6_addr.l2tp_addr));
1557 ip6_addr.l2tp_conn_id = tunnel_id;
1558 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1559 sizeof(ip6_addr));
1560 if (err < 0)
1561 goto out;
1562
1563 ip6_addr.l2tp_family = AF_INET6;
1564 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1565 sizeof(ip6_addr.l2tp_addr));
1566 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1567 err = kernel_connect(sock,
1568 (struct sockaddr *) &ip6_addr,
1569 sizeof(ip6_addr), 0);
1570 if (err < 0)
1571 goto out;
1572 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001573#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001574 {
Tom Parkin167eb172013-01-31 23:43:03 +00001575 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1576 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001577 if (err < 0)
1578 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001579
Tom Parkin167eb172013-01-31 23:43:03 +00001580 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001581
James Chapman5dac94e2012-04-29 21:48:55 +00001582 ip_addr.l2tp_family = AF_INET;
1583 ip_addr.l2tp_addr = cfg->local_ip;
1584 ip_addr.l2tp_conn_id = tunnel_id;
1585 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1586 sizeof(ip_addr));
1587 if (err < 0)
1588 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001589
James Chapman5dac94e2012-04-29 21:48:55 +00001590 ip_addr.l2tp_family = AF_INET;
1591 ip_addr.l2tp_addr = cfg->peer_ip;
1592 ip_addr.l2tp_conn_id = peer_tunnel_id;
1593 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1594 sizeof(ip_addr), 0);
1595 if (err < 0)
1596 goto out;
1597 }
James Chapman789a4a22010-04-02 06:19:40 +00001598 break;
1599
1600 default:
1601 goto out;
1602 }
1603
1604out:
Tom Parkin167eb172013-01-31 23:43:03 +00001605 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001606 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001607 kernel_sock_shutdown(sock, SHUT_RDWR);
1608 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001609 *sockp = NULL;
1610 }
1611
1612 return err;
1613}
1614
Eric Dumazet37159ef2012-09-04 07:18:57 +00001615static struct lock_class_key l2tp_socket_class;
1616
James Chapmanfd558d12010-04-02 06:18:33 +00001617int 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)
1618{
1619 struct l2tp_tunnel *tunnel = NULL;
1620 int err;
1621 struct socket *sock = NULL;
1622 struct sock *sk = NULL;
1623 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001624 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001625
1626 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001627 * the userspace L2TP daemon. If not specified, create a
1628 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001629 */
James Chapman789a4a22010-04-02 06:19:40 +00001630 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001631 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1632 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001633 if (err < 0)
1634 goto err;
1635 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001636 sock = sockfd_lookup(fd, &err);
1637 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001638 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001639 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001640 err = -EBADF;
1641 goto err;
1642 }
1643
1644 /* Reject namespace mismatches */
1645 if (!net_eq(sock_net(sock->sk), net)) {
1646 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1647 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001648 goto err;
1649 }
James Chapmanfd558d12010-04-02 06:18:33 +00001650 }
1651
1652 sk = sock->sk;
1653
James Chapman0d767512010-04-02 06:19:00 +00001654 if (cfg != NULL)
1655 encap = cfg->encap;
1656
James Chapmanfd558d12010-04-02 06:18:33 +00001657 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001658 switch (encap) {
1659 case L2TP_ENCAPTYPE_UDP:
1660 err = -EPROTONOSUPPORT;
1661 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001662 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001663 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1664 goto err;
1665 }
1666 break;
1667 case L2TP_ENCAPTYPE_IP:
1668 err = -EPROTONOSUPPORT;
1669 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001670 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001671 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1672 goto err;
1673 }
1674 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001675 }
1676
1677 /* Check if this socket has already been prepped */
1678 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1679 if (tunnel != NULL) {
1680 /* This socket has already been prepped */
1681 err = -EBUSY;
1682 goto err;
1683 }
1684
James Chapmanfd558d12010-04-02 06:18:33 +00001685 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1686 if (tunnel == NULL) {
1687 err = -ENOMEM;
1688 goto err;
1689 }
1690
1691 tunnel->version = version;
1692 tunnel->tunnel_id = tunnel_id;
1693 tunnel->peer_tunnel_id = peer_tunnel_id;
1694 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1695
1696 tunnel->magic = L2TP_TUNNEL_MAGIC;
1697 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1698 rwlock_init(&tunnel->hlist_lock);
1699
1700 /* The net we belong to */
1701 tunnel->l2tp_net = net;
1702 pn = l2tp_pernet(net);
1703
James Chapman0d767512010-04-02 06:19:00 +00001704 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001705 tunnel->debug = cfg->debug;
1706
1707 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001708 tunnel->encap = encap;
1709 if (encap == L2TP_ENCAPTYPE_UDP) {
1710 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1711 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1712 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001713 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001714#if IS_ENABLED(CONFIG_IPV6)
1715 if (sk->sk_family == PF_INET6)
1716 udpv6_encap_enable();
1717 else
1718#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001719 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001720 }
James Chapmanfd558d12010-04-02 06:18:33 +00001721
1722 sk->sk_user_data = tunnel;
1723
1724 /* Hook on the tunnel socket destructor so that we can cleanup
1725 * if the tunnel socket goes away.
1726 */
1727 tunnel->old_sk_destruct = sk->sk_destruct;
1728 sk->sk_destruct = &l2tp_tunnel_destruct;
1729 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001730 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001731 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1732
James Chapmanfd558d12010-04-02 06:18:33 +00001733 sk->sk_allocation = GFP_ATOMIC;
1734
Tom Parkinf8ccac02013-01-31 23:43:00 +00001735 /* Init delete workqueue struct */
1736 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1737
James Chapmanfd558d12010-04-02 06:18:33 +00001738 /* Add tunnel to our list */
1739 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001740 atomic_inc(&l2tp_tunnel_count);
1741
1742 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001743 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001744 */
1745 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001746 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1747 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1748 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001749
1750 err = 0;
1751err:
1752 if (tunnelp)
1753 *tunnelp = tunnel;
1754
James Chapman789a4a22010-04-02 06:19:40 +00001755 /* If tunnel's socket was created by the kernel, it doesn't
1756 * have a file.
1757 */
1758 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001759 sockfd_put(sock);
1760
1761 return err;
1762}
1763EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1764
James Chapman309795f2010-04-02 06:19:10 +00001765/* This function is used by the netlink TUNNEL_DELETE command.
1766 */
1767int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1768{
Tom Parkin2b551c62013-03-19 06:11:16 +00001769 l2tp_tunnel_closeall(tunnel);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001770 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001771}
1772EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1773
James Chapmanfd558d12010-04-02 06:18:33 +00001774/* Really kill the session.
1775 */
1776void l2tp_session_free(struct l2tp_session *session)
1777{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001778 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001779
1780 BUG_ON(atomic_read(&session->ref_count) != 0);
1781
Tom Parkinf6e16b22013-03-19 06:11:23 +00001782 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001783 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001784 if (session->session_id != 0)
1785 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001786 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001787 session->tunnel = NULL;
1788 l2tp_tunnel_dec_refcount(tunnel);
1789 }
1790
1791 kfree(session);
1792
1793 return;
1794}
1795EXPORT_SYMBOL_GPL(l2tp_session_free);
1796
Tom Parkinf6e16b22013-03-19 06:11:23 +00001797/* Remove an l2tp session from l2tp_core's hash lists.
1798 * Provides a tidyup interface for pseudowire code which can't just route all
1799 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1800 * callback.
1801 */
1802void __l2tp_session_unhash(struct l2tp_session *session)
1803{
1804 struct l2tp_tunnel *tunnel = session->tunnel;
1805
1806 /* Remove the session from core hashes */
1807 if (tunnel) {
1808 /* Remove from the per-tunnel hash */
1809 write_lock_bh(&tunnel->hlist_lock);
1810 hlist_del_init(&session->hlist);
1811 write_unlock_bh(&tunnel->hlist_lock);
1812
1813 /* For L2TPv3 we have a per-net hash: remove from there, too */
1814 if (tunnel->version != L2TP_HDR_VER_2) {
1815 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1816 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1817 hlist_del_init_rcu(&session->global_hlist);
1818 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1819 synchronize_rcu();
1820 }
1821 }
1822}
1823EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1824
James Chapman309795f2010-04-02 06:19:10 +00001825/* This function is used by the netlink SESSION_DELETE command and by
1826 pseudowire modules.
1827 */
1828int l2tp_session_delete(struct l2tp_session *session)
1829{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001830 if (session->ref)
1831 (*session->ref)(session);
1832 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001833 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001834 if (session->session_close != NULL)
1835 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001836 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001837 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001838 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001839 return 0;
1840}
1841EXPORT_SYMBOL_GPL(l2tp_session_delete);
1842
James Chapmanf7faffa2010-04-02 06:18:49 +00001843/* We come here whenever a session's send_seq, cookie_len or
1844 * l2specific_len parameters are set.
1845 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001846static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001847{
1848 if (version == L2TP_HDR_VER_2) {
1849 session->hdr_len = 6;
1850 if (session->send_seq)
1851 session->hdr_len += 4;
1852 } else {
James Chapman0d767512010-04-02 06:19:00 +00001853 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1854 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1855 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001856 }
1857
1858}
James Chapmanf7faffa2010-04-02 06:18:49 +00001859
James Chapmanfd558d12010-04-02 06:18:33 +00001860struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1861{
1862 struct l2tp_session *session;
1863
1864 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1865 if (session != NULL) {
1866 session->magic = L2TP_SESSION_MAGIC;
1867 session->tunnel = tunnel;
1868
1869 session->session_id = session_id;
1870 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001871 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001872 if (tunnel->version == L2TP_HDR_VER_2)
1873 session->nr_max = 0xffff;
1874 else
1875 session->nr_max = 0xffffff;
1876 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001877 session->nr_oos_count_max = 4;
1878
1879 /* Use NR of first received packet */
1880 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001881
1882 sprintf(&session->name[0], "sess %u/%u",
1883 tunnel->tunnel_id, session->session_id);
1884
1885 skb_queue_head_init(&session->reorder_q);
1886
1887 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001888 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001889
1890 /* Inherit debug options from tunnel */
1891 session->debug = tunnel->debug;
1892
1893 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001894 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001895 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001896 session->mtu = cfg->mtu;
1897 session->mru = cfg->mru;
1898 session->send_seq = cfg->send_seq;
1899 session->recv_seq = cfg->recv_seq;
1900 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001901 session->reorder_timeout = cfg->reorder_timeout;
1902 session->offset = cfg->offset;
1903 session->l2specific_type = cfg->l2specific_type;
1904 session->l2specific_len = cfg->l2specific_len;
1905 session->cookie_len = cfg->cookie_len;
1906 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1907 session->peer_cookie_len = cfg->peer_cookie_len;
1908 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001909 }
1910
James Chapmanf7faffa2010-04-02 06:18:49 +00001911 if (tunnel->version == L2TP_HDR_VER_2)
1912 session->build_header = l2tp_build_l2tpv2_header;
1913 else
1914 session->build_header = l2tp_build_l2tpv3_header;
1915
1916 l2tp_session_set_header_len(session, tunnel->version);
1917
James Chapmanfd558d12010-04-02 06:18:33 +00001918 /* Bump the reference count. The session context is deleted
1919 * only when this drops to zero.
1920 */
1921 l2tp_session_inc_refcount(session);
1922 l2tp_tunnel_inc_refcount(tunnel);
1923
1924 /* Ensure tunnel socket isn't deleted */
1925 sock_hold(tunnel->sock);
1926
1927 /* Add session to the tunnel's hash list */
1928 write_lock_bh(&tunnel->hlist_lock);
1929 hlist_add_head(&session->hlist,
1930 l2tp_session_id_hash(tunnel, session_id));
1931 write_unlock_bh(&tunnel->hlist_lock);
1932
James Chapmanf7faffa2010-04-02 06:18:49 +00001933 /* And to the global session list if L2TPv3 */
1934 if (tunnel->version != L2TP_HDR_VER_2) {
1935 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1936
James Chapmane02d4942010-04-02 06:19:16 +00001937 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1938 hlist_add_head_rcu(&session->global_hlist,
1939 l2tp_session_id_hash_2(pn, session_id));
1940 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001941 }
1942
James Chapmanfd558d12010-04-02 06:18:33 +00001943 /* Ignore management session in session count value */
1944 if (session->session_id != 0)
1945 atomic_inc(&l2tp_session_count);
1946 }
1947
1948 return session;
1949}
1950EXPORT_SYMBOL_GPL(l2tp_session_create);
1951
1952/*****************************************************************************
1953 * Init and cleanup
1954 *****************************************************************************/
1955
1956static __net_init int l2tp_init_net(struct net *net)
1957{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001958 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001959 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001960
James Chapmanfd558d12010-04-02 06:18:33 +00001961 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001962 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001963
James Chapmanf7faffa2010-04-02 06:18:49 +00001964 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1965 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1966
James Chapmane02d4942010-04-02 06:19:16 +00001967 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001968
James Chapmanfd558d12010-04-02 06:18:33 +00001969 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001970}
1971
Tom Parkin167eb172013-01-31 23:43:03 +00001972static __net_exit void l2tp_exit_net(struct net *net)
1973{
1974 struct l2tp_net *pn = l2tp_pernet(net);
1975 struct l2tp_tunnel *tunnel = NULL;
1976
1977 rcu_read_lock_bh();
1978 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1979 (void)l2tp_tunnel_delete(tunnel);
1980 }
1981 rcu_read_unlock_bh();
1982}
1983
James Chapmanfd558d12010-04-02 06:18:33 +00001984static struct pernet_operations l2tp_net_ops = {
1985 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001986 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001987 .id = &l2tp_net_id,
1988 .size = sizeof(struct l2tp_net),
1989};
1990
1991static int __init l2tp_init(void)
1992{
1993 int rc = 0;
1994
1995 rc = register_pernet_device(&l2tp_net_ops);
1996 if (rc)
1997 goto out;
1998
Tom Parkinf8ccac02013-01-31 23:43:00 +00001999 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
2000 if (!l2tp_wq) {
2001 pr_err("alloc_workqueue failed\n");
2002 rc = -ENOMEM;
2003 goto out;
2004 }
2005
Joe Perchesa4ca44f2012-05-16 09:55:56 +00002006 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00002007
2008out:
2009 return rc;
2010}
2011
2012static void __exit l2tp_exit(void)
2013{
2014 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00002015 if (l2tp_wq) {
2016 destroy_workqueue(l2tp_wq);
2017 l2tp_wq = NULL;
2018 }
James Chapmanfd558d12010-04-02 06:18:33 +00002019}
2020
2021module_init(l2tp_init);
2022module_exit(l2tp_exit);
2023
2024MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
2025MODULE_DESCRIPTION("L2TP core");
2026MODULE_LICENSE("GPL");
2027MODULE_VERSION(L2TP_DRV_VERSION);
2028