blob: 4828111c9ea7018a636a316bffe6e554675ea11c [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>
Tom Herbert85644b42014-07-13 19:49:48 -070055#include <net/udp_tunnel.h>
James Chapman309795f2010-04-02 06:19:10 +000056#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000057#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000058#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000059#include <net/inet6_connection_sock.h>
60#include <net/inet_ecn.h>
61#include <net/ip6_route.h>
David S. Millerd499bd22012-04-30 13:21:28 -040062#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000063
64#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000066
67#include "l2tp_core.h"
68
69#define L2TP_DRV_VERSION "V2.0"
70
71/* L2TP header constants */
72#define L2TP_HDRFLAG_T 0x8000
73#define L2TP_HDRFLAG_L 0x4000
74#define L2TP_HDRFLAG_S 0x0800
75#define L2TP_HDRFLAG_O 0x0200
76#define L2TP_HDRFLAG_P 0x0100
77
78#define L2TP_HDR_VER_MASK 0x000F
79#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000080#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000081
82/* L2TPv3 default L2-specific sublayer */
83#define L2TP_SLFLAG_S 0x40000000
84#define L2TP_SL_SEQ_MASK 0x00ffffff
85
86#define L2TP_HDR_SIZE_SEQ 10
87#define L2TP_HDR_SIZE_NOSEQ 6
88
89/* Default trace flags */
90#define L2TP_DEFAULT_DEBUG_FLAGS 0
91
James Chapmanfd558d12010-04-02 06:18:33 +000092/* Private data stored for received packets in the skb.
93 */
94struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000095 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000096 u16 has_seq;
97 u16 length;
98 unsigned long expires;
99};
100
101#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102
103static atomic_t l2tp_tunnel_count;
104static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000105static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000106
107/* per-net private data for this module */
108static unsigned int l2tp_net_id;
109struct l2tp_net {
110 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000111 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000112 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000113 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000114};
115
stephen hemmingerfc130842010-10-21 07:50:46 +0000116static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000117
David S. Miller8d8a51e2013-10-08 15:44:26 -0400118static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
119{
120 return sk->sk_user_data;
121}
122
Guillaume Nault9aaef502017-04-12 10:05:29 +0200123static inline struct l2tp_net *l2tp_pernet(const struct net *net)
James Chapmanfd558d12010-04-02 06:18:33 +0000124{
125 BUG_ON(!net);
126
127 return net_generic(net, l2tp_net_id);
128}
129
stephen hemmingerfc130842010-10-21 07:50:46 +0000130/* Tunnel reference counts. Incremented per session that is added to
131 * the tunnel.
132 */
133static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 atomic_inc(&tunnel->ref_count);
136}
137
138static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139{
140 if (atomic_dec_and_test(&tunnel->ref_count))
141 l2tp_tunnel_free(tunnel);
142}
143#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000144#define l2tp_tunnel_inc_refcount(_t) \
145do { \
146 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
147 __func__, __LINE__, (_t)->name, \
148 atomic_read(&_t->ref_count)); \
149 l2tp_tunnel_inc_refcount_1(_t); \
150} while (0)
Andy Zhou29abe2f2014-09-03 13:16:54 -0700151#define l2tp_tunnel_dec_refcount(_t) \
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000152do { \
153 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
154 __func__, __LINE__, (_t)->name, \
155 atomic_read(&_t->ref_count)); \
156 l2tp_tunnel_dec_refcount_1(_t); \
157} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000158#else
159#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
160#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
161#endif
162
James Chapmanf7faffa2010-04-02 06:18:49 +0000163/* Session hash global list for L2TPv3.
164 * The session_id SHOULD be random according to RFC3931, but several
165 * L2TP implementations use incrementing session_ids. So we do a real
166 * hash on the session_id, rather than a simple bitmask.
167 */
168static inline struct hlist_head *
169l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
170{
171 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
172
173}
174
Tom Parkin80d84ef2013-01-22 05:13:48 +0000175/* Lookup the tunnel socket, possibly involving the fs code if the socket is
176 * owned by userspace. A struct sock returned from this function must be
177 * released using l2tp_tunnel_sock_put once you're done with it.
178 */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800179static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000180{
181 int err = 0;
182 struct socket *sock = NULL;
183 struct sock *sk = NULL;
184
185 if (!tunnel)
186 goto out;
187
188 if (tunnel->fd >= 0) {
189 /* Socket is owned by userspace, who might be in the process
190 * of closing it. Look the socket up using the fd to ensure
191 * consistency.
192 */
193 sock = sockfd_lookup(tunnel->fd, &err);
194 if (sock)
195 sk = sock->sk;
196 } else {
197 /* Socket is owned by kernelspace */
198 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000199 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000200 }
201
202out:
203 return sk;
204}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000205
206/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800207static void l2tp_tunnel_sock_put(struct sock *sk)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000208{
209 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
210 if (tunnel) {
211 if (tunnel->fd >= 0) {
212 /* Socket is owned by userspace */
213 sockfd_put(sk->sk_socket);
214 }
215 sock_put(sk);
216 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000217 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000218}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000219
James Chapmanfd558d12010-04-02 06:18:33 +0000220/* Session hash list.
221 * The session_id SHOULD be random according to RFC2661, but several
222 * L2TP implementations (Cisco and Microsoft) use incrementing
223 * session_ids. So we do a real hash on the session_id, rather than a
224 * simple bitmask.
225 */
226static inline struct hlist_head *
227l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
228{
229 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
230}
231
Guillaume Nault55a3ce32017-04-11 13:12:21 +0200232/* Lookup a session. A new reference is held on the returned session.
Guillaume Nault61b9a042017-03-31 13:02:25 +0200233 * Optionally calls session->ref() too if do_ref is true.
234 */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200235struct l2tp_session *l2tp_session_get(const struct net *net,
Guillaume Nault61b9a042017-03-31 13:02:25 +0200236 struct l2tp_tunnel *tunnel,
237 u32 session_id, bool do_ref)
238{
239 struct hlist_head *session_list;
240 struct l2tp_session *session;
241
242 if (!tunnel) {
243 struct l2tp_net *pn = l2tp_pernet(net);
244
245 session_list = l2tp_session_id_hash_2(pn, session_id);
246
247 rcu_read_lock_bh();
248 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
249 if (session->session_id == session_id) {
250 l2tp_session_inc_refcount(session);
251 if (do_ref && session->ref)
252 session->ref(session);
253 rcu_read_unlock_bh();
254
255 return session;
256 }
257 }
258 rcu_read_unlock_bh();
259
260 return NULL;
261 }
262
263 session_list = l2tp_session_id_hash(tunnel, session_id);
264 read_lock_bh(&tunnel->hlist_lock);
265 hlist_for_each_entry(session, session_list, hlist) {
266 if (session->session_id == session_id) {
267 l2tp_session_inc_refcount(session);
268 if (do_ref && session->ref)
269 session->ref(session);
270 read_unlock_bh(&tunnel->hlist_lock);
271
272 return session;
273 }
274 }
275 read_unlock_bh(&tunnel->hlist_lock);
276
277 return NULL;
278}
279EXPORT_SYMBOL_GPL(l2tp_session_get);
280
Guillaume Naulte08293a2017-04-03 12:03:13 +0200281struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
282 bool do_ref)
James Chapmanfd558d12010-04-02 06:18:33 +0000283{
284 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000285 struct l2tp_session *session;
286 int count = 0;
287
288 read_lock_bh(&tunnel->hlist_lock);
289 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800290 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000291 if (++count > nth) {
Guillaume Naulte08293a2017-04-03 12:03:13 +0200292 l2tp_session_inc_refcount(session);
293 if (do_ref && session->ref)
294 session->ref(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000295 read_unlock_bh(&tunnel->hlist_lock);
296 return session;
297 }
298 }
299 }
300
301 read_unlock_bh(&tunnel->hlist_lock);
302
303 return NULL;
304}
Guillaume Naulte08293a2017-04-03 12:03:13 +0200305EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000306
James Chapman309795f2010-04-02 06:19:10 +0000307/* Lookup a session by interface name.
308 * This is very inefficient but is only used by management interfaces.
309 */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200310struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
311 const char *ifname,
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200312 bool do_ref)
James Chapman309795f2010-04-02 06:19:10 +0000313{
314 struct l2tp_net *pn = l2tp_pernet(net);
315 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000316 struct l2tp_session *session;
317
James Chapmane02d4942010-04-02 06:19:16 +0000318 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000319 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800320 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000321 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200322 l2tp_session_inc_refcount(session);
323 if (do_ref && session->ref)
324 session->ref(session);
James Chapmane02d4942010-04-02 06:19:16 +0000325 rcu_read_unlock_bh();
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200326
James Chapman309795f2010-04-02 06:19:10 +0000327 return session;
328 }
329 }
330 }
331
James Chapmane02d4942010-04-02 06:19:16 +0000332 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000333
334 return NULL;
335}
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200336EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000337
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200338static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
339 struct l2tp_session *session)
340{
341 struct l2tp_session *session_walk;
342 struct hlist_head *g_head;
343 struct hlist_head *head;
344 struct l2tp_net *pn;
345
346 head = l2tp_session_id_hash(tunnel, session->session_id);
347
348 write_lock_bh(&tunnel->hlist_lock);
349 hlist_for_each_entry(session_walk, head, hlist)
350 if (session_walk->session_id == session->session_id)
351 goto exist;
352
353 if (tunnel->version == L2TP_HDR_VER_3) {
354 pn = l2tp_pernet(tunnel->l2tp_net);
355 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
356 session->session_id);
357
358 spin_lock_bh(&pn->l2tp_session_hlist_lock);
359 hlist_for_each_entry(session_walk, g_head, global_hlist)
360 if (session_walk->session_id == session->session_id)
361 goto exist_glob;
362
363 hlist_add_head_rcu(&session->global_hlist, g_head);
364 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
365 }
366
367 hlist_add_head(&session->hlist, head);
368 write_unlock_bh(&tunnel->hlist_lock);
369
370 return 0;
371
372exist_glob:
373 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
374exist:
375 write_unlock_bh(&tunnel->hlist_lock);
376
377 return -EEXIST;
378}
379
James Chapmanfd558d12010-04-02 06:18:33 +0000380/* Lookup a tunnel by id
381 */
382struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
383{
384 struct l2tp_tunnel *tunnel;
385 struct l2tp_net *pn = l2tp_pernet(net);
386
James Chapmane02d4942010-04-02 06:19:16 +0000387 rcu_read_lock_bh();
388 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000389 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000390 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000391 return tunnel;
392 }
393 }
James Chapmane02d4942010-04-02 06:19:16 +0000394 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000395
396 return NULL;
397}
398EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
399
400struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
401{
402 struct l2tp_net *pn = l2tp_pernet(net);
403 struct l2tp_tunnel *tunnel;
404 int count = 0;
405
James Chapmane02d4942010-04-02 06:19:16 +0000406 rcu_read_lock_bh();
407 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000408 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000409 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000410 return tunnel;
411 }
412 }
413
James Chapmane02d4942010-04-02 06:19:16 +0000414 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000415
416 return NULL;
417}
418EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
419
420/*****************************************************************************
421 * Receive data handling
422 *****************************************************************************/
423
424/* Queue a skb in order. We come here only if the skb has an L2TP sequence
425 * number.
426 */
427static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
428{
429 struct sk_buff *skbp;
430 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000431 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000432
433 spin_lock_bh(&session->reorder_q.lock);
434 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
435 if (L2TP_SKB_CB(skbp)->ns > ns) {
436 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000437 l2tp_dbg(session, L2TP_MSG_SEQ,
438 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
439 session->name, ns, L2TP_SKB_CB(skbp)->ns,
440 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000441 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000442 goto out;
443 }
444 }
445
446 __skb_queue_tail(&session->reorder_q, skb);
447
448out:
449 spin_unlock_bh(&session->reorder_q.lock);
450}
451
452/* Dequeue a single skb.
453 */
454static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
455{
456 struct l2tp_tunnel *tunnel = session->tunnel;
457 int length = L2TP_SKB_CB(skb)->length;
458
459 /* We're about to requeue the skb, so return resources
460 * to its current owner (a socket receive buffer).
461 */
462 skb_orphan(skb);
463
Tom Parkin7b7c0712013-03-19 06:11:22 +0000464 atomic_long_inc(&tunnel->stats.rx_packets);
465 atomic_long_add(length, &tunnel->stats.rx_bytes);
466 atomic_long_inc(&session->stats.rx_packets);
467 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000468
469 if (L2TP_SKB_CB(skb)->has_seq) {
470 /* Bump our Nr */
471 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100472 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000473
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000474 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
475 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000476 }
477
478 /* call private receive handler */
479 if (session->recv_skb != NULL)
480 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
481 else
482 kfree_skb(skb);
483
484 if (session->deref)
485 (*session->deref)(session);
486}
487
488/* Dequeue skbs from the session's reorder_q, subject to packet order.
489 * Skbs that have been in the queue for too long are simply discarded.
490 */
491static void l2tp_recv_dequeue(struct l2tp_session *session)
492{
493 struct sk_buff *skb;
494 struct sk_buff *tmp;
495
496 /* If the pkt at the head of the queue has the nr that we
497 * expect to send up next, dequeue it and any other
498 * in-sequence packets behind it.
499 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000500start:
James Chapmanfd558d12010-04-02 06:18:33 +0000501 spin_lock_bh(&session->reorder_q.lock);
502 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
503 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000504 atomic_long_inc(&session->stats.rx_seq_discards);
505 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000506 l2tp_dbg(session, L2TP_MSG_SEQ,
507 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
508 session->name, L2TP_SKB_CB(skb)->ns,
509 L2TP_SKB_CB(skb)->length, session->nr,
510 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000511 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000512 __skb_unlink(skb, &session->reorder_q);
513 kfree_skb(skb);
514 if (session->deref)
515 (*session->deref)(session);
516 continue;
517 }
518
519 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000520 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000521 l2tp_dbg(session, L2TP_MSG_SEQ,
522 "%s: advancing nr to next pkt: %u -> %u",
523 session->name, session->nr,
524 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000525 session->reorder_skip = 0;
526 session->nr = L2TP_SKB_CB(skb)->ns;
527 }
James Chapmanfd558d12010-04-02 06:18:33 +0000528 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000529 l2tp_dbg(session, L2TP_MSG_SEQ,
530 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
531 session->name, L2TP_SKB_CB(skb)->ns,
532 L2TP_SKB_CB(skb)->length, session->nr,
533 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000534 goto out;
535 }
536 }
537 __skb_unlink(skb, &session->reorder_q);
538
539 /* Process the skb. We release the queue lock while we
540 * do so to let other contexts process the queue.
541 */
542 spin_unlock_bh(&session->reorder_q.lock);
543 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000544 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000545 }
546
547out:
548 spin_unlock_bh(&session->reorder_q.lock);
549}
550
James Chapman8a1631d2013-07-02 20:28:59 +0100551static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
552{
553 u32 nws;
554
555 if (nr >= session->nr)
556 nws = nr - session->nr;
557 else
558 nws = (session->nr_max + 1) - (session->nr - nr);
559
560 return nws < session->nr_window_size;
561}
562
James Chapmanb6dc01a2013-07-02 20:28:58 +0100563/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
564 * acceptable, else non-zero.
565 */
566static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
567{
James Chapman8a1631d2013-07-02 20:28:59 +0100568 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
569 /* Packet sequence number is outside allowed window.
570 * Discard it.
571 */
572 l2tp_dbg(session, L2TP_MSG_SEQ,
573 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
574 session->name, L2TP_SKB_CB(skb)->ns,
575 L2TP_SKB_CB(skb)->length, session->nr);
576 goto discard;
577 }
578
James Chapmanb6dc01a2013-07-02 20:28:58 +0100579 if (session->reorder_timeout != 0) {
580 /* Packet reordering enabled. Add skb to session's
581 * reorder queue, in order of ns.
582 */
583 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100584 goto out;
585 }
586
587 /* Packet reordering disabled. Discard out-of-sequence packets, while
588 * tracking the number if in-sequence packets after the first OOS packet
589 * is seen. After nr_oos_count_max in-sequence packets, reset the
590 * sequence number to re-enable packet reception.
591 */
592 if (L2TP_SKB_CB(skb)->ns == session->nr) {
593 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100594 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100595 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
596 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
597
598 if (nr_oos == nr_next)
599 session->nr_oos_count++;
600 else
601 session->nr_oos_count = 0;
602
603 session->nr_oos = nr_oos;
604 if (session->nr_oos_count > session->nr_oos_count_max) {
605 session->reorder_skip = 1;
606 l2tp_dbg(session, L2TP_MSG_SEQ,
607 "%s: %d oos packets received. Resetting sequence numbers\n",
608 session->name, session->nr_oos_count);
609 }
610 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100611 atomic_long_inc(&session->stats.rx_seq_discards);
612 l2tp_dbg(session, L2TP_MSG_SEQ,
613 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
614 session->name, L2TP_SKB_CB(skb)->ns,
615 L2TP_SKB_CB(skb)->length, session->nr,
616 skb_queue_len(&session->reorder_q));
617 goto discard;
618 }
619 skb_queue_tail(&session->reorder_q, skb);
620 }
621
James Chapmana0dbd822013-07-02 20:29:00 +0100622out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100623 return 0;
624
625discard:
626 return 1;
627}
628
James Chapmanf7faffa2010-04-02 06:18:49 +0000629/* Do receive processing of L2TP data frames. We handle both L2TPv2
630 * and L2TPv3 data frames here.
631 *
632 * L2TPv2 Data Message Header
633 *
634 * 0 1 2 3
635 * 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
636 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
637 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
638 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
639 * | Tunnel ID | Session ID |
640 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
641 * | Ns (opt) | Nr (opt) |
642 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
643 * | Offset Size (opt) | Offset pad... (opt)
644 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645 *
646 * Data frames are marked by T=0. All other fields are the same as
647 * those in L2TP control frames.
648 *
649 * L2TPv3 Data Message Header
650 *
651 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 * | L2TP Session Header |
653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
654 * | L2-Specific Sublayer |
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 * | Tunnel Payload ...
657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
658 *
659 * L2TPv3 Session Header Over IP
660 *
661 * 0 1 2 3
662 * 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
663 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
664 * | Session ID |
665 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
666 * | Cookie (optional, maximum 64 bits)...
667 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
668 * |
669 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670 *
671 * L2TPv3 L2-Specific Sublayer Format
672 *
673 * 0 1 2 3
674 * 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
675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 * |x|S|x|x|x|x|x|x| Sequence Number |
677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
678 *
679 * Cookie value, sublayer format and offset (pad) are negotiated with
680 * the peer when the session is set up. Unlike L2TPv2, we do not need
681 * to parse the packet header to determine if optional fields are
682 * present.
683 *
684 * Caller must already have parsed the frame and determined that it is
685 * a data (not control) frame before coming here. Fields up to the
686 * session-id have already been parsed and ptr points to the data
687 * after the session-id.
Guillaume Nault61b9a042017-03-31 13:02:25 +0200688 *
689 * session->ref() must have been called prior to l2tp_recv_common().
690 * session->deref() will be called automatically after skb is processed.
James Chapmanfd558d12010-04-02 06:18:33 +0000691 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000692void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
693 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
694 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000695{
James Chapmanf7faffa2010-04-02 06:18:49 +0000696 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000697 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000698 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000699
James Chapmanf7faffa2010-04-02 06:18:49 +0000700 /* Parse and check optional cookie */
701 if (session->peer_cookie_len > 0) {
702 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000703 l2tp_info(tunnel, L2TP_MSG_DATA,
704 "%s: cookie mismatch (%u/%u). Discarding.\n",
705 tunnel->name, tunnel->tunnel_id,
706 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000707 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000708 goto discard;
709 }
710 ptr += session->peer_cookie_len;
711 }
712
James Chapmanfd558d12010-04-02 06:18:33 +0000713 /* Handle the optional sequence numbers. Sequence numbers are
714 * in different places for L2TPv2 and L2TPv3.
715 *
716 * If we are the LAC, enable/disable sequence numbers under
717 * the control of the LNS. If no sequence numbers present but
718 * we were expecting them, discard frame.
719 */
720 ns = nr = 0;
721 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000722 if (tunnel->version == L2TP_HDR_VER_2) {
723 if (hdrflags & L2TP_HDRFLAG_S) {
724 ns = ntohs(*(__be16 *) ptr);
725 ptr += 2;
726 nr = ntohs(*(__be16 *) ptr);
727 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000728
James Chapmanf7faffa2010-04-02 06:18:49 +0000729 /* Store L2TP info in the skb */
730 L2TP_SKB_CB(skb)->ns = ns;
731 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000732
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000733 l2tp_dbg(session, L2TP_MSG_SEQ,
734 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
735 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000736 }
737 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
738 u32 l2h = ntohl(*(__be32 *) ptr);
739
740 if (l2h & 0x40000000) {
741 ns = l2h & 0x00ffffff;
742
743 /* Store L2TP info in the skb */
744 L2TP_SKB_CB(skb)->ns = ns;
745 L2TP_SKB_CB(skb)->has_seq = 1;
746
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000747 l2tp_dbg(session, L2TP_MSG_SEQ,
748 "%s: recv data ns=%u, session nr=%u\n",
749 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000750 }
James Chapmanfd558d12010-04-02 06:18:33 +0000751 }
752
James Chapmanf7faffa2010-04-02 06:18:49 +0000753 /* Advance past L2-specific header, if present */
754 ptr += session->l2specific_len;
755
James Chapmanfd558d12010-04-02 06:18:33 +0000756 if (L2TP_SKB_CB(skb)->has_seq) {
757 /* Received a packet with sequence numbers. If we're the LNS,
758 * check if we sre sending sequence numbers and if not,
759 * configure it so.
760 */
761 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000762 l2tp_info(session, L2TP_MSG_SEQ,
763 "%s: requested to enable seq numbers by LNS\n",
764 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000765 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000766 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000767 }
768 } else {
769 /* No sequence numbers.
770 * If user has configured mandatory sequence numbers, discard.
771 */
772 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000773 l2tp_warn(session, L2TP_MSG_SEQ,
774 "%s: recv data has no seq numbers when required. Discarding.\n",
775 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000776 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000777 goto discard;
778 }
779
780 /* If we're the LAC and we're sending sequence numbers, the
781 * LNS has requested that we no longer send sequence numbers.
782 * If we're the LNS and we're sending sequence numbers, the
783 * LAC is broken. Discard the frame.
784 */
785 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000786 l2tp_info(session, L2TP_MSG_SEQ,
787 "%s: requested to disable seq numbers by LNS\n",
788 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000789 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000790 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000791 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000792 l2tp_warn(session, L2TP_MSG_SEQ,
793 "%s: recv data has no seq numbers when required. Discarding.\n",
794 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000795 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000796 goto discard;
797 }
798 }
799
James Chapmanf7faffa2010-04-02 06:18:49 +0000800 /* Session data offset is handled differently for L2TPv2 and
801 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
802 * the header. For L2TPv3, the offset is negotiated using AVPs
803 * in the session setup control protocol.
804 */
805 if (tunnel->version == L2TP_HDR_VER_2) {
806 /* If offset bit set, skip it. */
807 if (hdrflags & L2TP_HDRFLAG_O) {
808 offset = ntohs(*(__be16 *)ptr);
809 ptr += 2 + offset;
810 }
811 } else
812 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000813
814 offset = ptr - optr;
815 if (!pskb_may_pull(skb, offset))
816 goto discard;
817
818 __skb_pull(skb, offset);
819
820 /* If caller wants to process the payload before we queue the
821 * packet, do so now.
822 */
823 if (payload_hook)
824 if ((*payload_hook)(skb))
825 goto discard;
826
827 /* Prepare skb for adding to the session's reorder_q. Hold
828 * packets for max reorder_timeout or 1 second if not
829 * reordering.
830 */
831 L2TP_SKB_CB(skb)->length = length;
832 L2TP_SKB_CB(skb)->expires = jiffies +
833 (session->reorder_timeout ? session->reorder_timeout : HZ);
834
835 /* Add packet to the session's receive queue. Reordering is done here, if
836 * enabled. Saved L2TP protocol info is stored in skb->sb[].
837 */
838 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100839 if (l2tp_recv_data_seq(session, skb))
840 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000841 } else {
842 /* No sequence numbers. Add the skb to the tail of the
843 * reorder queue. This ensures that it will be
844 * delivered after all previous sequenced skbs.
845 */
846 skb_queue_tail(&session->reorder_q, skb);
847 }
848
849 /* Try to dequeue as many skbs from reorder_q as we can. */
850 l2tp_recv_dequeue(session);
851
James Chapmanf7faffa2010-04-02 06:18:49 +0000852 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000853
854discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000855 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000856 kfree_skb(skb);
857
858 if (session->deref)
859 (*session->deref)(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
Tom Herbert58d60852014-05-07 16:52:48 -0700896 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000897
898 /* UDP always verifies the packet length. */
899 __skb_pull(skb, sizeof(struct udphdr));
900
901 /* Short packet? */
902 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000903 l2tp_info(tunnel, L2TP_MSG_DATA,
904 "%s: recv short packet (len=%d)\n",
905 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000906 goto error;
907 }
908
James Chapmanf7faffa2010-04-02 06:18:49 +0000909 /* Trace packet contents, if enabled */
910 if (tunnel->debug & L2TP_MSG_DATA) {
911 length = min(32u, skb->len);
912 if (!pskb_may_pull(skb, length))
913 goto error;
914
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000915 pr_debug("%s: recv\n", tunnel->name);
916 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000917 }
918
Eric Dumazete50e7052011-11-08 13:59:44 -0500919 /* Point to L2TP header */
920 optr = ptr = skb->data;
921
James Chapmanf7faffa2010-04-02 06:18:49 +0000922 /* Get L2TP header flags */
923 hdrflags = ntohs(*(__be16 *) ptr);
924
925 /* Check protocol version */
926 version = hdrflags & L2TP_HDR_VER_MASK;
927 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000928 l2tp_info(tunnel, L2TP_MSG_DATA,
929 "%s: recv protocol version mismatch: got %d expected %d\n",
930 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000931 goto error;
932 }
933
934 /* Get length of L2TP packet */
935 length = skb->len;
936
937 /* If type is control packet, it is handled by userspace. */
938 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000939 l2tp_dbg(tunnel, L2TP_MSG_DATA,
940 "%s: recv control packet, len=%d\n",
941 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000942 goto error;
943 }
944
945 /* Skip flags */
946 ptr += 2;
947
948 if (tunnel->version == L2TP_HDR_VER_2) {
949 /* If length is present, skip it */
950 if (hdrflags & L2TP_HDRFLAG_L)
951 ptr += 2;
952
953 /* Extract tunnel and session ID */
954 tunnel_id = ntohs(*(__be16 *) ptr);
955 ptr += 2;
956 session_id = ntohs(*(__be16 *) ptr);
957 ptr += 2;
958 } else {
959 ptr += 2; /* skip reserved bits */
960 tunnel_id = tunnel->tunnel_id;
961 session_id = ntohl(*(__be32 *) ptr);
962 ptr += 4;
963 }
964
965 /* Find the session context */
Guillaume Nault61b9a042017-03-31 13:02:25 +0200966 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
James Chapman309795f2010-04-02 06:19:10 +0000967 if (!session || !session->recv_skb) {
Guillaume Nault61b9a042017-03-31 13:02:25 +0200968 if (session) {
969 if (session->deref)
970 session->deref(session);
971 l2tp_session_dec_refcount(session);
972 }
973
James Chapmanf7faffa2010-04-02 06:18:49 +0000974 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000975 l2tp_info(tunnel, L2TP_MSG_DATA,
976 "%s: no session found (%u/%u). Passing up.\n",
977 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000978 goto error;
979 }
980
981 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200982 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000983
984 return 0;
985
James Chapmanfd558d12010-04-02 06:18:33 +0000986error:
987 /* Put UDP header back */
988 __skb_push(skb, sizeof(struct udphdr));
989
990 return 1;
991}
James Chapmanfd558d12010-04-02 06:18:33 +0000992
993/* UDP encapsulation receive handler. See net/ipv4/udp.c.
994 * Return codes:
995 * 0 : success.
996 * <0: error
997 * >0: skb should be passed up to userspace as UDP.
998 */
999int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1000{
1001 struct l2tp_tunnel *tunnel;
1002
1003 tunnel = l2tp_sock_to_tunnel(sk);
1004 if (tunnel == NULL)
1005 goto pass_up;
1006
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001007 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1008 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001009
1010 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1011 goto pass_up_put;
1012
1013 sock_put(sk);
1014 return 0;
1015
1016pass_up_put:
1017 sock_put(sk);
1018pass_up:
1019 return 1;
1020}
1021EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1022
1023/************************************************************************
1024 * Transmit handling
1025 ***********************************************************************/
1026
1027/* Build an L2TP header for the session into the buffer provided.
1028 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001029static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001030{
James Chapmanf7faffa2010-04-02 06:18:49 +00001031 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001032 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001033 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001034 u16 flags = L2TP_HDR_VER_2;
1035 u32 tunnel_id = tunnel->peer_tunnel_id;
1036 u32 session_id = session->peer_session_id;
1037
1038 if (session->send_seq)
1039 flags |= L2TP_HDRFLAG_S;
1040
1041 /* Setup L2TP header. */
1042 *bufp++ = htons(flags);
1043 *bufp++ = htons(tunnel_id);
1044 *bufp++ = htons(session_id);
1045 if (session->send_seq) {
1046 *bufp++ = htons(session->ns);
1047 *bufp++ = 0;
1048 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001049 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001050 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1051 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001052 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001053
1054 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001055}
1056
James Chapmanf7faffa2010-04-02 06:18:49 +00001057static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001058{
James Chapman0d767512010-04-02 06:19:00 +00001059 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001060 char *bufp = buf;
1061 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001062
James Chapman0d767512010-04-02 06:19:00 +00001063 /* Setup L2TP header. The header differs slightly for UDP and
1064 * IP encapsulations. For UDP, there is 4 bytes of flags.
1065 */
1066 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1067 u16 flags = L2TP_HDR_VER_3;
1068 *((__be16 *) bufp) = htons(flags);
1069 bufp += 2;
1070 *((__be16 *) bufp) = 0;
1071 bufp += 2;
1072 }
1073
James Chapmanf7faffa2010-04-02 06:18:49 +00001074 *((__be32 *) bufp) = htonl(session->peer_session_id);
1075 bufp += 4;
1076 if (session->cookie_len) {
1077 memcpy(bufp, &session->cookie[0], session->cookie_len);
1078 bufp += session->cookie_len;
1079 }
1080 if (session->l2specific_len) {
1081 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1082 u32 l2h = 0;
1083 if (session->send_seq) {
1084 l2h = 0x40000000 | session->ns;
1085 session->ns++;
1086 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001087 l2tp_dbg(session, L2TP_MSG_SEQ,
1088 "%s: updated ns to %u\n",
1089 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001090 }
1091
1092 *((__be32 *) bufp) = htonl(l2h);
1093 }
1094 bufp += session->l2specific_len;
1095 }
1096 if (session->offset)
1097 bufp += session->offset;
1098
1099 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001100}
James Chapmanfd558d12010-04-02 06:18:33 +00001101
stephen hemmingerfc130842010-10-21 07:50:46 +00001102static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001103 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001104{
1105 struct l2tp_tunnel *tunnel = session->tunnel;
1106 unsigned int len = skb->len;
1107 int error;
1108
1109 /* Debug */
1110 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001111 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001112 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001113 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001114 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001115 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001116
1117 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001118 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1119 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001120
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001121 pr_debug("%s: xmit\n", session->name);
1122 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1123 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001124 }
1125
1126 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001127 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001128#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001129 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001130 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001131 else
1132#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001133 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001134
1135 /* Update stats */
1136 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001137 atomic_long_inc(&tunnel->stats.tx_packets);
1138 atomic_long_add(len, &tunnel->stats.tx_bytes);
1139 atomic_long_inc(&session->stats.tx_packets);
1140 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001141 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001142 atomic_long_inc(&tunnel->stats.tx_errors);
1143 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001144 }
1145
1146 return 0;
1147}
James Chapmanfd558d12010-04-02 06:18:33 +00001148
James Chapmanfd558d12010-04-02 06:18:33 +00001149/* If caller requires the skb to have a ppp header, the header must be
1150 * inserted in the skb data before calling this function.
1151 */
1152int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1153{
1154 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001155 struct l2tp_tunnel *tunnel = session->tunnel;
1156 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001157 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001158 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001159 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001160 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001161 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1162 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001163 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001164
1165 /* Check that there's enough headroom in the skb to insert IP,
1166 * UDP and L2TP headers. If not enough, expand it to
1167 * make room. Adjust truesize.
1168 */
1169 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001170 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001171 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001172 kfree_skb(skb);
1173 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001174 }
James Chapmanfd558d12010-04-02 06:18:33 +00001175
James Chapmanfd558d12010-04-02 06:18:33 +00001176 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001177 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001178
James Chapman0d767512010-04-02 06:19:00 +00001179 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001180 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1181 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1182 IPSKB_REROUTED);
1183 nf_reset(skb);
1184
David S. Miller6af88da2011-05-08 13:45:20 -07001185 bh_lock_sock(sk);
1186 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001187 kfree_skb(skb);
1188 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001189 goto out_unlock;
1190 }
1191
James Chapmanfd558d12010-04-02 06:18:33 +00001192 /* Get routing info from the tunnel socket */
1193 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001194 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001195
David S. Millerd9d8da82011-05-06 22:23:20 -07001196 inet = inet_sk(sk);
1197 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001198 switch (tunnel->encap) {
1199 case L2TP_ENCAPTYPE_UDP:
1200 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001201 __skb_push(skb, sizeof(*uh));
1202 skb_reset_transport_header(skb);
1203 uh = udp_hdr(skb);
1204 uh->source = inet->inet_sport;
1205 uh->dest = inet->inet_dport;
1206 udp_len = uhlen + hdr_len + data_len;
1207 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001208
1209 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001210#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001211 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001212 udp6_set_csum(udp_get_no_check6_tx(sk),
1213 skb, &inet6_sk(sk)->saddr,
1214 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001215 else
1216#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001217 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1218 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001219 break;
1220
1221 case L2TP_ENCAPTYPE_IP:
1222 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001223 }
1224
David S. Millerd9d8da82011-05-06 22:23:20 -07001225 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001226out_unlock:
1227 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001228
Eric Dumazetb8c84302012-06-28 20:15:13 +00001229 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001230}
1231EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1232
1233/*****************************************************************************
1234 * Tinnel and session create/destroy.
1235 *****************************************************************************/
1236
1237/* Tunnel socket destruct hook.
1238 * The tunnel context is deleted only when all session sockets have been
1239 * closed.
1240 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001241static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001242{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001243 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001244 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001245
James Chapmanfd558d12010-04-02 06:18:33 +00001246 if (tunnel == NULL)
1247 goto end;
1248
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001249 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001250
James Chapmanfd558d12010-04-02 06:18:33 +00001251
Tom Parkinf8ccac02013-01-31 23:43:00 +00001252 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001253 switch (tunnel->encap) {
1254 case L2TP_ENCAPTYPE_UDP:
1255 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1256 (udp_sk(sk))->encap_type = 0;
1257 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001258 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001259 break;
1260 case L2TP_ENCAPTYPE_IP:
1261 break;
1262 }
James Chapmanfd558d12010-04-02 06:18:33 +00001263
1264 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001265 sk->sk_destruct = tunnel->old_sk_destruct;
1266 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001267 tunnel->sock = NULL;
1268
1269 /* Remove the tunnel struct from the tunnel list */
1270 pn = l2tp_pernet(tunnel->l2tp_net);
1271 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1272 list_del_rcu(&tunnel->list);
1273 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1274 atomic_dec(&l2tp_tunnel_count);
1275
1276 l2tp_tunnel_closeall(tunnel);
1277 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001278
1279 /* Call the original destructor */
1280 if (sk->sk_destruct)
1281 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001282end:
1283 return;
1284}
James Chapmanfd558d12010-04-02 06:18:33 +00001285
1286/* When the tunnel is closed, all the attached sessions need to go too.
1287 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001288void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001289{
1290 int hash;
1291 struct hlist_node *walk;
1292 struct hlist_node *tmp;
1293 struct l2tp_session *session;
1294
1295 BUG_ON(tunnel == NULL);
1296
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001297 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1298 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001299
1300 write_lock_bh(&tunnel->hlist_lock);
1301 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1302again:
1303 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1304 session = hlist_entry(walk, struct l2tp_session, hlist);
1305
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001306 l2tp_info(session, L2TP_MSG_CONTROL,
1307 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001308
1309 hlist_del_init(&session->hlist);
1310
James Chapmanfd558d12010-04-02 06:18:33 +00001311 if (session->ref != NULL)
1312 (*session->ref)(session);
1313
1314 write_unlock_bh(&tunnel->hlist_lock);
1315
Tom Parkinf6e16b22013-03-19 06:11:23 +00001316 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001317 l2tp_session_queue_purge(session);
1318
James Chapmanfd558d12010-04-02 06:18:33 +00001319 if (session->session_close != NULL)
1320 (*session->session_close)(session);
1321
1322 if (session->deref != NULL)
1323 (*session->deref)(session);
1324
Tom Parkin9980d002013-03-19 06:11:13 +00001325 l2tp_session_dec_refcount(session);
1326
James Chapmanfd558d12010-04-02 06:18:33 +00001327 write_lock_bh(&tunnel->hlist_lock);
1328
1329 /* Now restart from the beginning of this hash
1330 * chain. We always remove a session from the
1331 * list so we are guaranteed to make forward
1332 * progress.
1333 */
1334 goto again;
1335 }
1336 }
1337 write_unlock_bh(&tunnel->hlist_lock);
1338}
Tom Parkine34f4c72013-03-19 06:11:14 +00001339EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001340
Tom Parkin9980d002013-03-19 06:11:13 +00001341/* Tunnel socket destroy hook for UDP encapsulation */
1342static void l2tp_udp_encap_destroy(struct sock *sk)
1343{
1344 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1345 if (tunnel) {
1346 l2tp_tunnel_closeall(tunnel);
1347 sock_put(sk);
1348 }
1349}
1350
James Chapmanfd558d12010-04-02 06:18:33 +00001351/* Really kill the tunnel.
1352 * Come here only when all sessions have been cleared from the tunnel.
1353 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001354static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001355{
James Chapmanfd558d12010-04-02 06:18:33 +00001356 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1357 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001358 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001359 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001360}
James Chapmanfd558d12010-04-02 06:18:33 +00001361
Tom Parkinf8ccac02013-01-31 23:43:00 +00001362/* Workqueue tunnel deletion function */
1363static void l2tp_tunnel_del_work(struct work_struct *work)
1364{
1365 struct l2tp_tunnel *tunnel = NULL;
1366 struct socket *sock = NULL;
1367 struct sock *sk = NULL;
1368
1369 tunnel = container_of(work, struct l2tp_tunnel, del_work);
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001370
1371 l2tp_tunnel_closeall(tunnel);
1372
Tom Parkinf8ccac02013-01-31 23:43:00 +00001373 sk = l2tp_tunnel_sock_lookup(tunnel);
1374 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001375 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001376
1377 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001378
Tom Parkin02d13ed2013-03-19 06:11:18 +00001379 /* If the tunnel socket was created by userspace, then go through the
1380 * inet layer to shut the socket down, and let userspace close it.
1381 * Otherwise, if we created the socket directly within the kernel, use
1382 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001383 * In either case the tunnel resources are freed in the socket
1384 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001385 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001386 if (tunnel->fd >= 0) {
1387 if (sock)
1388 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001389 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001390 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001391 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001392 sock_release(sock);
1393 }
Tom Parkin167eb172013-01-31 23:43:03 +00001394 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001395
1396 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001397out:
1398 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001399}
James Chapmanfd558d12010-04-02 06:18:33 +00001400
James Chapman789a4a22010-04-02 06:19:40 +00001401/* Create a socket for the tunnel, if one isn't set up by
1402 * userspace. This is used for static tunnels where there is no
1403 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001404 *
1405 * Since we don't want these sockets to keep a namespace alive by
1406 * themselves, we drop the socket's namespace refcount after creation.
1407 * These sockets are freed when the namespace exits using the pernet
1408 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001409 */
Tom Parkin167eb172013-01-31 23:43:03 +00001410static int l2tp_tunnel_sock_create(struct net *net,
1411 u32 tunnel_id,
1412 u32 peer_tunnel_id,
1413 struct l2tp_tunnel_cfg *cfg,
1414 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001415{
1416 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001417 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001418 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001419
1420 switch (cfg->encap) {
1421 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001422 memset(&udp_conf, 0, sizeof(udp_conf));
1423
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001424#if IS_ENABLED(CONFIG_IPV6)
1425 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001426 udp_conf.family = AF_INET6;
1427 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1428 sizeof(udp_conf.local_ip6));
1429 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1430 sizeof(udp_conf.peer_ip6));
1431 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001432 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001433 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001434 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001435 } else
1436#endif
1437 {
Tom Herbert85644b42014-07-13 19:49:48 -07001438 udp_conf.family = AF_INET;
1439 udp_conf.local_ip = cfg->local_ip;
1440 udp_conf.peer_ip = cfg->peer_ip;
1441 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001442 }
James Chapman789a4a22010-04-02 06:19:40 +00001443
Tom Herbert85644b42014-07-13 19:49:48 -07001444 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1445 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1446
1447 err = udp_sock_create(net, &udp_conf, &sock);
1448 if (err < 0)
1449 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001450
1451 break;
1452
1453 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001454#if IS_ENABLED(CONFIG_IPV6)
1455 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001456 struct sockaddr_l2tpip6 ip6_addr = {0};
1457
Eric W. Biederman26abe142015-05-08 21:10:31 -05001458 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001459 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001460 if (err < 0)
1461 goto out;
1462
James Chapman5dac94e2012-04-29 21:48:55 +00001463 ip6_addr.l2tp_family = AF_INET6;
1464 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1465 sizeof(ip6_addr.l2tp_addr));
1466 ip6_addr.l2tp_conn_id = tunnel_id;
1467 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1468 sizeof(ip6_addr));
1469 if (err < 0)
1470 goto out;
1471
1472 ip6_addr.l2tp_family = AF_INET6;
1473 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1474 sizeof(ip6_addr.l2tp_addr));
1475 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1476 err = kernel_connect(sock,
1477 (struct sockaddr *) &ip6_addr,
1478 sizeof(ip6_addr), 0);
1479 if (err < 0)
1480 goto out;
1481 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001482#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001483 {
Tom Herbert85644b42014-07-13 19:49:48 -07001484 struct sockaddr_l2tpip ip_addr = {0};
1485
Eric W. Biederman26abe142015-05-08 21:10:31 -05001486 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001487 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001488 if (err < 0)
1489 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001490
James Chapman5dac94e2012-04-29 21:48:55 +00001491 ip_addr.l2tp_family = AF_INET;
1492 ip_addr.l2tp_addr = cfg->local_ip;
1493 ip_addr.l2tp_conn_id = tunnel_id;
1494 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1495 sizeof(ip_addr));
1496 if (err < 0)
1497 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001498
James Chapman5dac94e2012-04-29 21:48:55 +00001499 ip_addr.l2tp_family = AF_INET;
1500 ip_addr.l2tp_addr = cfg->peer_ip;
1501 ip_addr.l2tp_conn_id = peer_tunnel_id;
1502 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1503 sizeof(ip_addr), 0);
1504 if (err < 0)
1505 goto out;
1506 }
James Chapman789a4a22010-04-02 06:19:40 +00001507 break;
1508
1509 default:
1510 goto out;
1511 }
1512
1513out:
Tom Parkin167eb172013-01-31 23:43:03 +00001514 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001515 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001516 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001517 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001518 *sockp = NULL;
1519 }
1520
1521 return err;
1522}
1523
Eric Dumazet37159ef2012-09-04 07:18:57 +00001524static struct lock_class_key l2tp_socket_class;
1525
James Chapmanfd558d12010-04-02 06:18:33 +00001526int 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)
1527{
1528 struct l2tp_tunnel *tunnel = NULL;
1529 int err;
1530 struct socket *sock = NULL;
1531 struct sock *sk = NULL;
1532 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001533 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001534
1535 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001536 * the userspace L2TP daemon. If not specified, create a
1537 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001538 */
James Chapman789a4a22010-04-02 06:19:40 +00001539 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001540 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1541 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001542 if (err < 0)
1543 goto err;
1544 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001545 sock = sockfd_lookup(fd, &err);
1546 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001547 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001548 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001549 err = -EBADF;
1550 goto err;
1551 }
1552
1553 /* Reject namespace mismatches */
1554 if (!net_eq(sock_net(sock->sk), net)) {
1555 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1556 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001557 goto err;
1558 }
James Chapmanfd558d12010-04-02 06:18:33 +00001559 }
1560
1561 sk = sock->sk;
1562
James Chapman0d767512010-04-02 06:19:00 +00001563 if (cfg != NULL)
1564 encap = cfg->encap;
1565
James Chapmanfd558d12010-04-02 06:18:33 +00001566 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001567 switch (encap) {
1568 case L2TP_ENCAPTYPE_UDP:
1569 err = -EPROTONOSUPPORT;
1570 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001571 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001572 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1573 goto err;
1574 }
1575 break;
1576 case L2TP_ENCAPTYPE_IP:
1577 err = -EPROTONOSUPPORT;
1578 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001579 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001580 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1581 goto err;
1582 }
1583 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001584 }
1585
1586 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001587 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001588 if (tunnel != NULL) {
1589 /* This socket has already been prepped */
1590 err = -EBUSY;
1591 goto err;
1592 }
1593
James Chapmanfd558d12010-04-02 06:18:33 +00001594 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1595 if (tunnel == NULL) {
1596 err = -ENOMEM;
1597 goto err;
1598 }
1599
1600 tunnel->version = version;
1601 tunnel->tunnel_id = tunnel_id;
1602 tunnel->peer_tunnel_id = peer_tunnel_id;
1603 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1604
1605 tunnel->magic = L2TP_TUNNEL_MAGIC;
1606 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1607 rwlock_init(&tunnel->hlist_lock);
1608
1609 /* The net we belong to */
1610 tunnel->l2tp_net = net;
1611 pn = l2tp_pernet(net);
1612
James Chapman0d767512010-04-02 06:19:00 +00001613 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001614 tunnel->debug = cfg->debug;
1615
François Cachereule18503f2013-10-02 10:16:02 +02001616#if IS_ENABLED(CONFIG_IPV6)
1617 if (sk->sk_family == PF_INET6) {
1618 struct ipv6_pinfo *np = inet6_sk(sk);
1619
1620 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001621 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001622 struct inet_sock *inet = inet_sk(sk);
1623
1624 tunnel->v4mapped = true;
1625 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001626 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1627 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001628 } else {
1629 tunnel->v4mapped = false;
1630 }
1631 }
1632#endif
1633
James Chapmanfd558d12010-04-02 06:18:33 +00001634 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001635 tunnel->encap = encap;
1636 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001637 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001638
Andy Zhouc8fffce2014-09-16 17:31:19 -07001639 udp_cfg.sk_user_data = tunnel;
1640 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1641 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1642 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1643
1644 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1645 } else {
1646 sk->sk_user_data = tunnel;
1647 }
James Chapmanfd558d12010-04-02 06:18:33 +00001648
1649 /* Hook on the tunnel socket destructor so that we can cleanup
1650 * if the tunnel socket goes away.
1651 */
1652 tunnel->old_sk_destruct = sk->sk_destruct;
1653 sk->sk_destruct = &l2tp_tunnel_destruct;
1654 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001655 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001656 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1657
James Chapmanfd558d12010-04-02 06:18:33 +00001658 sk->sk_allocation = GFP_ATOMIC;
1659
Tom Parkinf8ccac02013-01-31 23:43:00 +00001660 /* Init delete workqueue struct */
1661 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1662
James Chapmanfd558d12010-04-02 06:18:33 +00001663 /* Add tunnel to our list */
1664 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001665 atomic_inc(&l2tp_tunnel_count);
1666
1667 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001668 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001669 */
1670 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001671 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1672 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1673 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001674
1675 err = 0;
1676err:
1677 if (tunnelp)
1678 *tunnelp = tunnel;
1679
James Chapman789a4a22010-04-02 06:19:40 +00001680 /* If tunnel's socket was created by the kernel, it doesn't
1681 * have a file.
1682 */
1683 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001684 sockfd_put(sock);
1685
1686 return err;
1687}
1688EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1689
James Chapman309795f2010-04-02 06:19:10 +00001690/* This function is used by the netlink TUNNEL_DELETE command.
1691 */
1692int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1693{
Alexander Couzens06a15f52015-09-28 11:32:42 +02001694 l2tp_tunnel_inc_refcount(tunnel);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001695 if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
1696 l2tp_tunnel_dec_refcount(tunnel);
1697 return 1;
1698 }
1699 return 0;
James Chapman309795f2010-04-02 06:19:10 +00001700}
1701EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1702
James Chapmanfd558d12010-04-02 06:18:33 +00001703/* Really kill the session.
1704 */
1705void l2tp_session_free(struct l2tp_session *session)
1706{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001707 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001708
1709 BUG_ON(atomic_read(&session->ref_count) != 0);
1710
Tom Parkinf6e16b22013-03-19 06:11:23 +00001711 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001712 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001713 if (session->session_id != 0)
1714 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001715 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001716 session->tunnel = NULL;
1717 l2tp_tunnel_dec_refcount(tunnel);
1718 }
1719
1720 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001721}
1722EXPORT_SYMBOL_GPL(l2tp_session_free);
1723
Tom Parkinf6e16b22013-03-19 06:11:23 +00001724/* Remove an l2tp session from l2tp_core's hash lists.
1725 * Provides a tidyup interface for pseudowire code which can't just route all
1726 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1727 * callback.
1728 */
1729void __l2tp_session_unhash(struct l2tp_session *session)
1730{
1731 struct l2tp_tunnel *tunnel = session->tunnel;
1732
1733 /* Remove the session from core hashes */
1734 if (tunnel) {
1735 /* Remove from the per-tunnel hash */
1736 write_lock_bh(&tunnel->hlist_lock);
1737 hlist_del_init(&session->hlist);
1738 write_unlock_bh(&tunnel->hlist_lock);
1739
1740 /* For L2TPv3 we have a per-net hash: remove from there, too */
1741 if (tunnel->version != L2TP_HDR_VER_2) {
1742 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1743 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1744 hlist_del_init_rcu(&session->global_hlist);
1745 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1746 synchronize_rcu();
1747 }
1748 }
1749}
1750EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1751
James Chapman309795f2010-04-02 06:19:10 +00001752/* This function is used by the netlink SESSION_DELETE command and by
1753 pseudowire modules.
1754 */
1755int l2tp_session_delete(struct l2tp_session *session)
1756{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001757 if (session->ref)
1758 (*session->ref)(session);
1759 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001760 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001761 if (session->session_close != NULL)
1762 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001763 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001764 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001765 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001766 return 0;
1767}
1768EXPORT_SYMBOL_GPL(l2tp_session_delete);
1769
James Chapmanf7faffa2010-04-02 06:18:49 +00001770/* We come here whenever a session's send_seq, cookie_len or
1771 * l2specific_len parameters are set.
1772 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001773void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001774{
1775 if (version == L2TP_HDR_VER_2) {
1776 session->hdr_len = 6;
1777 if (session->send_seq)
1778 session->hdr_len += 4;
1779 } else {
James Chapman0d767512010-04-02 06:19:00 +00001780 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1781 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1782 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001783 }
1784
1785}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001786EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001787
James Chapmanfd558d12010-04-02 06:18:33 +00001788struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1789{
1790 struct l2tp_session *session;
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001791 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001792
1793 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1794 if (session != NULL) {
1795 session->magic = L2TP_SESSION_MAGIC;
1796 session->tunnel = tunnel;
1797
1798 session->session_id = session_id;
1799 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001800 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001801 if (tunnel->version == L2TP_HDR_VER_2)
1802 session->nr_max = 0xffff;
1803 else
1804 session->nr_max = 0xffffff;
1805 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001806 session->nr_oos_count_max = 4;
1807
1808 /* Use NR of first received packet */
1809 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001810
1811 sprintf(&session->name[0], "sess %u/%u",
1812 tunnel->tunnel_id, session->session_id);
1813
1814 skb_queue_head_init(&session->reorder_q);
1815
1816 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001817 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001818
1819 /* Inherit debug options from tunnel */
1820 session->debug = tunnel->debug;
1821
1822 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001823 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001824 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001825 session->mtu = cfg->mtu;
1826 session->mru = cfg->mru;
1827 session->send_seq = cfg->send_seq;
1828 session->recv_seq = cfg->recv_seq;
1829 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001830 session->reorder_timeout = cfg->reorder_timeout;
1831 session->offset = cfg->offset;
1832 session->l2specific_type = cfg->l2specific_type;
1833 session->l2specific_len = cfg->l2specific_len;
1834 session->cookie_len = cfg->cookie_len;
1835 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1836 session->peer_cookie_len = cfg->peer_cookie_len;
1837 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001838 }
1839
James Chapmanf7faffa2010-04-02 06:18:49 +00001840 if (tunnel->version == L2TP_HDR_VER_2)
1841 session->build_header = l2tp_build_l2tpv2_header;
1842 else
1843 session->build_header = l2tp_build_l2tpv3_header;
1844
1845 l2tp_session_set_header_len(session, tunnel->version);
1846
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001847 err = l2tp_session_add_to_tunnel(tunnel, session);
1848 if (err) {
1849 kfree(session);
1850
1851 return ERR_PTR(err);
1852 }
1853
James Chapmanfd558d12010-04-02 06:18:33 +00001854 /* Bump the reference count. The session context is deleted
1855 * only when this drops to zero.
1856 */
1857 l2tp_session_inc_refcount(session);
1858 l2tp_tunnel_inc_refcount(tunnel);
1859
1860 /* Ensure tunnel socket isn't deleted */
1861 sock_hold(tunnel->sock);
1862
James Chapmanfd558d12010-04-02 06:18:33 +00001863 /* Ignore management session in session count value */
1864 if (session->session_id != 0)
1865 atomic_inc(&l2tp_session_count);
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001866
1867 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001868 }
1869
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001870 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001871}
1872EXPORT_SYMBOL_GPL(l2tp_session_create);
1873
1874/*****************************************************************************
1875 * Init and cleanup
1876 *****************************************************************************/
1877
1878static __net_init int l2tp_init_net(struct net *net)
1879{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001880 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001881 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001882
James Chapmanfd558d12010-04-02 06:18:33 +00001883 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001884 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001885
James Chapmanf7faffa2010-04-02 06:18:49 +00001886 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1887 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1888
James Chapmane02d4942010-04-02 06:19:16 +00001889 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001890
James Chapmanfd558d12010-04-02 06:18:33 +00001891 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001892}
1893
Tom Parkin167eb172013-01-31 23:43:03 +00001894static __net_exit void l2tp_exit_net(struct net *net)
1895{
1896 struct l2tp_net *pn = l2tp_pernet(net);
1897 struct l2tp_tunnel *tunnel = NULL;
1898
1899 rcu_read_lock_bh();
1900 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1901 (void)l2tp_tunnel_delete(tunnel);
1902 }
1903 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001904
1905 flush_workqueue(l2tp_wq);
1906 rcu_barrier();
Tom Parkin167eb172013-01-31 23:43:03 +00001907}
1908
James Chapmanfd558d12010-04-02 06:18:33 +00001909static struct pernet_operations l2tp_net_ops = {
1910 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001911 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001912 .id = &l2tp_net_id,
1913 .size = sizeof(struct l2tp_net),
1914};
1915
1916static int __init l2tp_init(void)
1917{
1918 int rc = 0;
1919
1920 rc = register_pernet_device(&l2tp_net_ops);
1921 if (rc)
1922 goto out;
1923
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001924 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001925 if (!l2tp_wq) {
1926 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001927 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001928 rc = -ENOMEM;
1929 goto out;
1930 }
1931
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001932 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001933
1934out:
1935 return rc;
1936}
1937
1938static void __exit l2tp_exit(void)
1939{
1940 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001941 if (l2tp_wq) {
1942 destroy_workqueue(l2tp_wq);
1943 l2tp_wq = NULL;
1944 }
James Chapmanfd558d12010-04-02 06:18:33 +00001945}
1946
1947module_init(l2tp_init);
1948module_exit(l2tp_exit);
1949
1950MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1951MODULE_DESCRIPTION("L2TP core");
1952MODULE_LICENSE("GPL");
1953MODULE_VERSION(L2TP_DRV_VERSION);
1954