blob: 046a5ba0ebd2cf59bb68db538b5ef238b1c6875c [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
James Chapmanfd558d12010-04-02 06:18:33 +0000123static inline struct l2tp_net *l2tp_pernet(struct net *net)
124{
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 Chapmanf7faffa2010-04-02 06:18:49 +0000220/* Lookup a session by id in the global session list
221 */
222static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
223{
224 struct l2tp_net *pn = l2tp_pernet(net);
225 struct hlist_head *session_list =
226 l2tp_session_id_hash_2(pn, session_id);
227 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000228
James Chapmane02d4942010-04-02 06:19:16 +0000229 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800230 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000231 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000232 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000233 return session;
234 }
235 }
James Chapmane02d4942010-04-02 06:19:16 +0000236 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000237
238 return NULL;
239}
240
James Chapmanfd558d12010-04-02 06:18:33 +0000241/* Session hash list.
242 * The session_id SHOULD be random according to RFC2661, but several
243 * L2TP implementations (Cisco and Microsoft) use incrementing
244 * session_ids. So we do a real hash on the session_id, rather than a
245 * simple bitmask.
246 */
247static inline struct hlist_head *
248l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
249{
250 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
251}
252
253/* Lookup a session by id
254 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000255struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000256{
James Chapmanf7faffa2010-04-02 06:18:49 +0000257 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000258 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000259
James Chapmanf7faffa2010-04-02 06:18:49 +0000260 /* In L2TPv3, session_ids are unique over all tunnels and we
261 * sometimes need to look them up before we know the
262 * tunnel.
263 */
264 if (tunnel == NULL)
265 return l2tp_session_find_2(net, session_id);
266
267 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000268 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800269 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000270 if (session->session_id == session_id) {
271 read_unlock_bh(&tunnel->hlist_lock);
272 return session;
273 }
274 }
275 read_unlock_bh(&tunnel->hlist_lock);
276
277 return NULL;
278}
279EXPORT_SYMBOL_GPL(l2tp_session_find);
280
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200281/* Like l2tp_session_find() but takes a reference on the returned session.
282 * Optionally calls session->ref() too if do_ref is true.
283 */
284struct l2tp_session *l2tp_session_get(struct net *net,
285 struct l2tp_tunnel *tunnel,
286 u32 session_id, bool do_ref)
287{
288 struct hlist_head *session_list;
289 struct l2tp_session *session;
290
291 if (!tunnel) {
292 struct l2tp_net *pn = l2tp_pernet(net);
293
294 session_list = l2tp_session_id_hash_2(pn, session_id);
295
296 rcu_read_lock_bh();
297 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
298 if (session->session_id == session_id) {
299 l2tp_session_inc_refcount(session);
300 if (do_ref && session->ref)
301 session->ref(session);
302 rcu_read_unlock_bh();
303
304 return session;
305 }
306 }
307 rcu_read_unlock_bh();
308
309 return NULL;
310 }
311
312 session_list = l2tp_session_id_hash(tunnel, session_id);
313 read_lock_bh(&tunnel->hlist_lock);
314 hlist_for_each_entry(session, session_list, hlist) {
315 if (session->session_id == session_id) {
316 l2tp_session_inc_refcount(session);
317 if (do_ref && session->ref)
318 session->ref(session);
319 read_unlock_bh(&tunnel->hlist_lock);
320
321 return session;
322 }
323 }
324 read_unlock_bh(&tunnel->hlist_lock);
325
326 return NULL;
327}
328EXPORT_SYMBOL_GPL(l2tp_session_get);
329
Guillaume Naultb7902602017-04-03 12:03:13 +0200330struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
331 bool do_ref)
James Chapmanfd558d12010-04-02 06:18:33 +0000332{
333 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000334 struct l2tp_session *session;
335 int count = 0;
336
337 read_lock_bh(&tunnel->hlist_lock);
338 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800339 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000340 if (++count > nth) {
Guillaume Naultb7902602017-04-03 12:03:13 +0200341 l2tp_session_inc_refcount(session);
342 if (do_ref && session->ref)
343 session->ref(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000344 read_unlock_bh(&tunnel->hlist_lock);
345 return session;
346 }
347 }
348 }
349
350 read_unlock_bh(&tunnel->hlist_lock);
351
352 return NULL;
353}
Guillaume Naultb7902602017-04-03 12:03:13 +0200354EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000355
James Chapman309795f2010-04-02 06:19:10 +0000356/* Lookup a session by interface name.
357 * This is very inefficient but is only used by management interfaces.
358 */
359struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
360{
361 struct l2tp_net *pn = l2tp_pernet(net);
362 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000363 struct l2tp_session *session;
364
James Chapmane02d4942010-04-02 06:19:16 +0000365 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000366 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800367 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000368 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000369 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000370 return session;
371 }
372 }
373 }
374
James Chapmane02d4942010-04-02 06:19:16 +0000375 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000376
377 return NULL;
378}
379EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
380
James Chapmanfd558d12010-04-02 06:18:33 +0000381/* Lookup a tunnel by id
382 */
383struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
384{
385 struct l2tp_tunnel *tunnel;
386 struct l2tp_net *pn = l2tp_pernet(net);
387
James Chapmane02d4942010-04-02 06:19:16 +0000388 rcu_read_lock_bh();
389 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000390 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000391 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000392 return tunnel;
393 }
394 }
James Chapmane02d4942010-04-02 06:19:16 +0000395 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000396
397 return NULL;
398}
399EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
400
401struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
402{
403 struct l2tp_net *pn = l2tp_pernet(net);
404 struct l2tp_tunnel *tunnel;
405 int count = 0;
406
James Chapmane02d4942010-04-02 06:19:16 +0000407 rcu_read_lock_bh();
408 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000409 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000410 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000411 return tunnel;
412 }
413 }
414
James Chapmane02d4942010-04-02 06:19:16 +0000415 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000416
417 return NULL;
418}
419EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
420
421/*****************************************************************************
422 * Receive data handling
423 *****************************************************************************/
424
425/* Queue a skb in order. We come here only if the skb has an L2TP sequence
426 * number.
427 */
428static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
429{
430 struct sk_buff *skbp;
431 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000432 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000433
434 spin_lock_bh(&session->reorder_q.lock);
435 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
436 if (L2TP_SKB_CB(skbp)->ns > ns) {
437 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000438 l2tp_dbg(session, L2TP_MSG_SEQ,
439 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
440 session->name, ns, L2TP_SKB_CB(skbp)->ns,
441 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000442 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000443 goto out;
444 }
445 }
446
447 __skb_queue_tail(&session->reorder_q, skb);
448
449out:
450 spin_unlock_bh(&session->reorder_q.lock);
451}
452
453/* Dequeue a single skb.
454 */
455static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
456{
457 struct l2tp_tunnel *tunnel = session->tunnel;
458 int length = L2TP_SKB_CB(skb)->length;
459
460 /* We're about to requeue the skb, so return resources
461 * to its current owner (a socket receive buffer).
462 */
463 skb_orphan(skb);
464
Tom Parkin7b7c0712013-03-19 06:11:22 +0000465 atomic_long_inc(&tunnel->stats.rx_packets);
466 atomic_long_add(length, &tunnel->stats.rx_bytes);
467 atomic_long_inc(&session->stats.rx_packets);
468 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000469
470 if (L2TP_SKB_CB(skb)->has_seq) {
471 /* Bump our Nr */
472 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100473 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000474
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000475 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
476 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000477 }
478
479 /* call private receive handler */
480 if (session->recv_skb != NULL)
481 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
482 else
483 kfree_skb(skb);
484
485 if (session->deref)
486 (*session->deref)(session);
487}
488
489/* Dequeue skbs from the session's reorder_q, subject to packet order.
490 * Skbs that have been in the queue for too long are simply discarded.
491 */
492static void l2tp_recv_dequeue(struct l2tp_session *session)
493{
494 struct sk_buff *skb;
495 struct sk_buff *tmp;
496
497 /* If the pkt at the head of the queue has the nr that we
498 * expect to send up next, dequeue it and any other
499 * in-sequence packets behind it.
500 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000501start:
James Chapmanfd558d12010-04-02 06:18:33 +0000502 spin_lock_bh(&session->reorder_q.lock);
503 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
504 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000505 atomic_long_inc(&session->stats.rx_seq_discards);
506 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000507 l2tp_dbg(session, L2TP_MSG_SEQ,
508 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
509 session->name, L2TP_SKB_CB(skb)->ns,
510 L2TP_SKB_CB(skb)->length, session->nr,
511 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000512 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000513 __skb_unlink(skb, &session->reorder_q);
514 kfree_skb(skb);
515 if (session->deref)
516 (*session->deref)(session);
517 continue;
518 }
519
520 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000521 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000522 l2tp_dbg(session, L2TP_MSG_SEQ,
523 "%s: advancing nr to next pkt: %u -> %u",
524 session->name, session->nr,
525 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000526 session->reorder_skip = 0;
527 session->nr = L2TP_SKB_CB(skb)->ns;
528 }
James Chapmanfd558d12010-04-02 06:18:33 +0000529 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000530 l2tp_dbg(session, L2TP_MSG_SEQ,
531 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
532 session->name, L2TP_SKB_CB(skb)->ns,
533 L2TP_SKB_CB(skb)->length, session->nr,
534 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000535 goto out;
536 }
537 }
538 __skb_unlink(skb, &session->reorder_q);
539
540 /* Process the skb. We release the queue lock while we
541 * do so to let other contexts process the queue.
542 */
543 spin_unlock_bh(&session->reorder_q.lock);
544 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000545 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000546 }
547
548out:
549 spin_unlock_bh(&session->reorder_q.lock);
550}
551
James Chapman8a1631d2013-07-02 20:28:59 +0100552static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
553{
554 u32 nws;
555
556 if (nr >= session->nr)
557 nws = nr - session->nr;
558 else
559 nws = (session->nr_max + 1) - (session->nr - nr);
560
561 return nws < session->nr_window_size;
562}
563
James Chapmanb6dc01a2013-07-02 20:28:58 +0100564/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
565 * acceptable, else non-zero.
566 */
567static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
568{
James Chapman8a1631d2013-07-02 20:28:59 +0100569 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
570 /* Packet sequence number is outside allowed window.
571 * Discard it.
572 */
573 l2tp_dbg(session, L2TP_MSG_SEQ,
574 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
575 session->name, L2TP_SKB_CB(skb)->ns,
576 L2TP_SKB_CB(skb)->length, session->nr);
577 goto discard;
578 }
579
James Chapmanb6dc01a2013-07-02 20:28:58 +0100580 if (session->reorder_timeout != 0) {
581 /* Packet reordering enabled. Add skb to session's
582 * reorder queue, in order of ns.
583 */
584 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100585 goto out;
586 }
587
588 /* Packet reordering disabled. Discard out-of-sequence packets, while
589 * tracking the number if in-sequence packets after the first OOS packet
590 * is seen. After nr_oos_count_max in-sequence packets, reset the
591 * sequence number to re-enable packet reception.
592 */
593 if (L2TP_SKB_CB(skb)->ns == session->nr) {
594 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100595 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100596 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
597 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
598
599 if (nr_oos == nr_next)
600 session->nr_oos_count++;
601 else
602 session->nr_oos_count = 0;
603
604 session->nr_oos = nr_oos;
605 if (session->nr_oos_count > session->nr_oos_count_max) {
606 session->reorder_skip = 1;
607 l2tp_dbg(session, L2TP_MSG_SEQ,
608 "%s: %d oos packets received. Resetting sequence numbers\n",
609 session->name, session->nr_oos_count);
610 }
611 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100612 atomic_long_inc(&session->stats.rx_seq_discards);
613 l2tp_dbg(session, L2TP_MSG_SEQ,
614 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
615 session->name, L2TP_SKB_CB(skb)->ns,
616 L2TP_SKB_CB(skb)->length, session->nr,
617 skb_queue_len(&session->reorder_q));
618 goto discard;
619 }
620 skb_queue_tail(&session->reorder_q, skb);
621 }
622
James Chapmana0dbd822013-07-02 20:29:00 +0100623out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100624 return 0;
625
626discard:
627 return 1;
628}
629
James Chapmanf7faffa2010-04-02 06:18:49 +0000630/* Do receive processing of L2TP data frames. We handle both L2TPv2
631 * and L2TPv3 data frames here.
632 *
633 * L2TPv2 Data Message Header
634 *
635 * 0 1 2 3
636 * 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
637 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
639 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * | Tunnel ID | Session ID |
641 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 * | Ns (opt) | Nr (opt) |
643 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
644 * | Offset Size (opt) | Offset pad... (opt)
645 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
646 *
647 * Data frames are marked by T=0. All other fields are the same as
648 * those in L2TP control frames.
649 *
650 * L2TPv3 Data Message Header
651 *
652 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
653 * | L2TP Session Header |
654 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655 * | L2-Specific Sublayer |
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 * | Tunnel Payload ...
658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 *
660 * L2TPv3 Session Header Over IP
661 *
662 * 0 1 2 3
663 * 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
664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665 * | Session ID |
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * | Cookie (optional, maximum 64 bits)...
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 * |
670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
671 *
672 * L2TPv3 L2-Specific Sublayer Format
673 *
674 * 0 1 2 3
675 * 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
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677 * |x|S|x|x|x|x|x|x| Sequence Number |
678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
679 *
680 * Cookie value, sublayer format and offset (pad) are negotiated with
681 * the peer when the session is set up. Unlike L2TPv2, we do not need
682 * to parse the packet header to determine if optional fields are
683 * present.
684 *
685 * Caller must already have parsed the frame and determined that it is
686 * a data (not control) frame before coming here. Fields up to the
687 * session-id have already been parsed and ptr points to the data
688 * after the session-id.
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200689 *
690 * session->ref() must have been called prior to l2tp_recv_common().
691 * session->deref() will be called automatically after skb is processed.
James Chapmanfd558d12010-04-02 06:18:33 +0000692 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000693void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
694 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
695 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000696{
James Chapmanf7faffa2010-04-02 06:18:49 +0000697 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000698 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000699 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000700
James Chapmanf7faffa2010-04-02 06:18:49 +0000701 /* Parse and check optional cookie */
702 if (session->peer_cookie_len > 0) {
703 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000704 l2tp_info(tunnel, L2TP_MSG_DATA,
705 "%s: cookie mismatch (%u/%u). Discarding.\n",
706 tunnel->name, tunnel->tunnel_id,
707 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000708 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000709 goto discard;
710 }
711 ptr += session->peer_cookie_len;
712 }
713
James Chapmanfd558d12010-04-02 06:18:33 +0000714 /* Handle the optional sequence numbers. Sequence numbers are
715 * in different places for L2TPv2 and L2TPv3.
716 *
717 * If we are the LAC, enable/disable sequence numbers under
718 * the control of the LNS. If no sequence numbers present but
719 * we were expecting them, discard frame.
720 */
721 ns = nr = 0;
722 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000723 if (tunnel->version == L2TP_HDR_VER_2) {
724 if (hdrflags & L2TP_HDRFLAG_S) {
725 ns = ntohs(*(__be16 *) ptr);
726 ptr += 2;
727 nr = ntohs(*(__be16 *) ptr);
728 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000729
James Chapmanf7faffa2010-04-02 06:18:49 +0000730 /* Store L2TP info in the skb */
731 L2TP_SKB_CB(skb)->ns = ns;
732 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000733
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000734 l2tp_dbg(session, L2TP_MSG_SEQ,
735 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
736 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000737 }
738 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
739 u32 l2h = ntohl(*(__be32 *) ptr);
740
741 if (l2h & 0x40000000) {
742 ns = l2h & 0x00ffffff;
743
744 /* Store L2TP info in the skb */
745 L2TP_SKB_CB(skb)->ns = ns;
746 L2TP_SKB_CB(skb)->has_seq = 1;
747
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000748 l2tp_dbg(session, L2TP_MSG_SEQ,
749 "%s: recv data ns=%u, session nr=%u\n",
750 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000751 }
James Chapmanfd558d12010-04-02 06:18:33 +0000752 }
753
James Chapmanf7faffa2010-04-02 06:18:49 +0000754 /* Advance past L2-specific header, if present */
755 ptr += session->l2specific_len;
756
James Chapmanfd558d12010-04-02 06:18:33 +0000757 if (L2TP_SKB_CB(skb)->has_seq) {
758 /* Received a packet with sequence numbers. If we're the LNS,
759 * check if we sre sending sequence numbers and if not,
760 * configure it so.
761 */
762 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000763 l2tp_info(session, L2TP_MSG_SEQ,
764 "%s: requested to enable seq numbers by LNS\n",
765 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000766 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000767 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000768 }
769 } else {
770 /* No sequence numbers.
771 * If user has configured mandatory sequence numbers, discard.
772 */
773 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000774 l2tp_warn(session, L2TP_MSG_SEQ,
775 "%s: recv data has no seq numbers when required. Discarding.\n",
776 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000777 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000778 goto discard;
779 }
780
781 /* If we're the LAC and we're sending sequence numbers, the
782 * LNS has requested that we no longer send sequence numbers.
783 * If we're the LNS and we're sending sequence numbers, the
784 * LAC is broken. Discard the frame.
785 */
786 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000787 l2tp_info(session, L2TP_MSG_SEQ,
788 "%s: requested to disable seq numbers by LNS\n",
789 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000790 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000791 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000792 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000793 l2tp_warn(session, L2TP_MSG_SEQ,
794 "%s: recv data has no seq numbers when required. Discarding.\n",
795 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000796 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000797 goto discard;
798 }
799 }
800
James Chapmanf7faffa2010-04-02 06:18:49 +0000801 /* Session data offset is handled differently for L2TPv2 and
802 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
803 * the header. For L2TPv3, the offset is negotiated using AVPs
804 * in the session setup control protocol.
805 */
806 if (tunnel->version == L2TP_HDR_VER_2) {
807 /* If offset bit set, skip it. */
808 if (hdrflags & L2TP_HDRFLAG_O) {
809 offset = ntohs(*(__be16 *)ptr);
810 ptr += 2 + offset;
811 }
812 } else
813 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000814
815 offset = ptr - optr;
816 if (!pskb_may_pull(skb, offset))
817 goto discard;
818
819 __skb_pull(skb, offset);
820
821 /* If caller wants to process the payload before we queue the
822 * packet, do so now.
823 */
824 if (payload_hook)
825 if ((*payload_hook)(skb))
826 goto discard;
827
828 /* Prepare skb for adding to the session's reorder_q. Hold
829 * packets for max reorder_timeout or 1 second if not
830 * reordering.
831 */
832 L2TP_SKB_CB(skb)->length = length;
833 L2TP_SKB_CB(skb)->expires = jiffies +
834 (session->reorder_timeout ? session->reorder_timeout : HZ);
835
836 /* Add packet to the session's receive queue. Reordering is done here, if
837 * enabled. Saved L2TP protocol info is stored in skb->sb[].
838 */
839 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100840 if (l2tp_recv_data_seq(session, skb))
841 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000842 } else {
843 /* No sequence numbers. Add the skb to the tail of the
844 * reorder queue. This ensures that it will be
845 * delivered after all previous sequenced skbs.
846 */
847 skb_queue_tail(&session->reorder_q, skb);
848 }
849
850 /* Try to dequeue as many skbs from reorder_q as we can. */
851 l2tp_recv_dequeue(session);
852
James Chapmanf7faffa2010-04-02 06:18:49 +0000853 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000854
855discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000856 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000857 kfree_skb(skb);
858
859 if (session->deref)
860 (*session->deref)(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000861}
862EXPORT_SYMBOL(l2tp_recv_common);
863
Tom Parkin48f72f92013-03-19 06:11:19 +0000864/* Drop skbs from the session's reorder_q
865 */
866int l2tp_session_queue_purge(struct l2tp_session *session)
867{
868 struct sk_buff *skb = NULL;
869 BUG_ON(!session);
870 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
871 while ((skb = skb_dequeue(&session->reorder_q))) {
872 atomic_long_inc(&session->stats.rx_errors);
873 kfree_skb(skb);
874 if (session->deref)
875 (*session->deref)(session);
876 }
877 return 0;
878}
879EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
880
James Chapmanf7faffa2010-04-02 06:18:49 +0000881/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
882 * here. The skb is not on a list when we get here.
883 * Returns 0 if the packet was a data packet and was successfully passed on.
884 * Returns 1 if the packet was not a good data packet and could not be
885 * forwarded. All such packets are passed up to userspace to deal with.
886 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000887static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
888 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000889{
890 struct l2tp_session *session = NULL;
891 unsigned char *ptr, *optr;
892 u16 hdrflags;
893 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000894 u16 version;
895 int length;
896
Tom Herbert58d60852014-05-07 16:52:48 -0700897 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000898
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 */
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200967 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
James Chapman309795f2010-04-02 06:19:10 +0000968 if (!session || !session->recv_skb) {
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200969 if (session) {
970 if (session->deref)
971 session->deref(session);
972 l2tp_session_dec_refcount(session);
973 }
974
James Chapmanf7faffa2010-04-02 06:18:49 +0000975 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000976 l2tp_info(tunnel, L2TP_MSG_DATA,
977 "%s: no session found (%u/%u). Passing up.\n",
978 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000979 goto error;
980 }
981
982 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200983 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000984
985 return 0;
986
James Chapmanfd558d12010-04-02 06:18:33 +0000987error:
988 /* Put UDP header back */
989 __skb_push(skb, sizeof(struct udphdr));
990
991 return 1;
992}
James Chapmanfd558d12010-04-02 06:18:33 +0000993
994/* UDP encapsulation receive handler. See net/ipv4/udp.c.
995 * Return codes:
996 * 0 : success.
997 * <0: error
998 * >0: skb should be passed up to userspace as UDP.
999 */
1000int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1001{
1002 struct l2tp_tunnel *tunnel;
1003
1004 tunnel = l2tp_sock_to_tunnel(sk);
1005 if (tunnel == NULL)
1006 goto pass_up;
1007
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001008 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1009 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001010
1011 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1012 goto pass_up_put;
1013
1014 sock_put(sk);
1015 return 0;
1016
1017pass_up_put:
1018 sock_put(sk);
1019pass_up:
1020 return 1;
1021}
1022EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1023
1024/************************************************************************
1025 * Transmit handling
1026 ***********************************************************************/
1027
1028/* Build an L2TP header for the session into the buffer provided.
1029 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001030static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001031{
James Chapmanf7faffa2010-04-02 06:18:49 +00001032 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001033 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001034 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001035 u16 flags = L2TP_HDR_VER_2;
1036 u32 tunnel_id = tunnel->peer_tunnel_id;
1037 u32 session_id = session->peer_session_id;
1038
1039 if (session->send_seq)
1040 flags |= L2TP_HDRFLAG_S;
1041
1042 /* Setup L2TP header. */
1043 *bufp++ = htons(flags);
1044 *bufp++ = htons(tunnel_id);
1045 *bufp++ = htons(session_id);
1046 if (session->send_seq) {
1047 *bufp++ = htons(session->ns);
1048 *bufp++ = 0;
1049 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001050 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001051 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1052 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001053 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001054
1055 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001056}
1057
James Chapmanf7faffa2010-04-02 06:18:49 +00001058static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001059{
James Chapman0d767512010-04-02 06:19:00 +00001060 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001061 char *bufp = buf;
1062 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001063
James Chapman0d767512010-04-02 06:19:00 +00001064 /* Setup L2TP header. The header differs slightly for UDP and
1065 * IP encapsulations. For UDP, there is 4 bytes of flags.
1066 */
1067 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1068 u16 flags = L2TP_HDR_VER_3;
1069 *((__be16 *) bufp) = htons(flags);
1070 bufp += 2;
1071 *((__be16 *) bufp) = 0;
1072 bufp += 2;
1073 }
1074
James Chapmanf7faffa2010-04-02 06:18:49 +00001075 *((__be32 *) bufp) = htonl(session->peer_session_id);
1076 bufp += 4;
1077 if (session->cookie_len) {
1078 memcpy(bufp, &session->cookie[0], session->cookie_len);
1079 bufp += session->cookie_len;
1080 }
1081 if (session->l2specific_len) {
1082 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1083 u32 l2h = 0;
1084 if (session->send_seq) {
1085 l2h = 0x40000000 | session->ns;
1086 session->ns++;
1087 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001088 l2tp_dbg(session, L2TP_MSG_SEQ,
1089 "%s: updated ns to %u\n",
1090 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001091 }
1092
1093 *((__be32 *) bufp) = htonl(l2h);
1094 }
1095 bufp += session->l2specific_len;
1096 }
1097 if (session->offset)
1098 bufp += session->offset;
1099
1100 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001101}
James Chapmanfd558d12010-04-02 06:18:33 +00001102
stephen hemmingerfc130842010-10-21 07:50:46 +00001103static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001104 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001105{
1106 struct l2tp_tunnel *tunnel = session->tunnel;
1107 unsigned int len = skb->len;
1108 int error;
1109
1110 /* Debug */
1111 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001112 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1113 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001114 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001115 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1116 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001117
1118 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001119 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1120 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001121
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001122 pr_debug("%s: xmit\n", session->name);
1123 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1124 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001125 }
1126
1127 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001128 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001129#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001130 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001131 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001132 else
1133#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001134 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001135
1136 /* Update stats */
1137 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001138 atomic_long_inc(&tunnel->stats.tx_packets);
1139 atomic_long_add(len, &tunnel->stats.tx_bytes);
1140 atomic_long_inc(&session->stats.tx_packets);
1141 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001142 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001143 atomic_long_inc(&tunnel->stats.tx_errors);
1144 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001145 }
1146
1147 return 0;
1148}
James Chapmanfd558d12010-04-02 06:18:33 +00001149
James Chapmanfd558d12010-04-02 06:18:33 +00001150/* If caller requires the skb to have a ppp header, the header must be
1151 * inserted in the skb data before calling this function.
1152 */
1153int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1154{
1155 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001156 struct l2tp_tunnel *tunnel = session->tunnel;
1157 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001158 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001159 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001160 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001161 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001162 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1163 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001164 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001165
1166 /* Check that there's enough headroom in the skb to insert IP,
1167 * UDP and L2TP headers. If not enough, expand it to
1168 * make room. Adjust truesize.
1169 */
1170 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001171 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001172 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001173 kfree_skb(skb);
1174 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001175 }
James Chapmanfd558d12010-04-02 06:18:33 +00001176
James Chapmanfd558d12010-04-02 06:18:33 +00001177 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001178 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001179
James Chapman0d767512010-04-02 06:19:00 +00001180 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001181 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1182 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1183 IPSKB_REROUTED);
1184 nf_reset(skb);
1185
David S. Miller6af88da2011-05-08 13:45:20 -07001186 bh_lock_sock(sk);
1187 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001188 kfree_skb(skb);
1189 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001190 goto out_unlock;
1191 }
1192
James Chapmanfd558d12010-04-02 06:18:33 +00001193 /* Get routing info from the tunnel socket */
1194 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001195 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001196
David S. Millerd9d8da82011-05-06 22:23:20 -07001197 inet = inet_sk(sk);
1198 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001199 switch (tunnel->encap) {
1200 case L2TP_ENCAPTYPE_UDP:
1201 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001202 __skb_push(skb, sizeof(*uh));
1203 skb_reset_transport_header(skb);
1204 uh = udp_hdr(skb);
1205 uh->source = inet->inet_sport;
1206 uh->dest = inet->inet_dport;
1207 udp_len = uhlen + hdr_len + data_len;
1208 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001209
1210 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001211#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001212 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001213 udp6_set_csum(udp_get_no_check6_tx(sk),
1214 skb, &inet6_sk(sk)->saddr,
1215 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001216 else
1217#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001218 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1219 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001220 break;
1221
1222 case L2TP_ENCAPTYPE_IP:
1223 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001224 }
1225
David S. Millerd9d8da82011-05-06 22:23:20 -07001226 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001227out_unlock:
1228 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001229
Eric Dumazetb8c84302012-06-28 20:15:13 +00001230 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001231}
1232EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1233
1234/*****************************************************************************
1235 * Tinnel and session create/destroy.
1236 *****************************************************************************/
1237
1238/* Tunnel socket destruct hook.
1239 * The tunnel context is deleted only when all session sockets have been
1240 * closed.
1241 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001242static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001243{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001244 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001245 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001246
James Chapmanfd558d12010-04-02 06:18:33 +00001247 if (tunnel == NULL)
1248 goto end;
1249
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001250 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001251
James Chapmanfd558d12010-04-02 06:18:33 +00001252
Tom Parkinf8ccac02013-01-31 23:43:00 +00001253 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001254 switch (tunnel->encap) {
1255 case L2TP_ENCAPTYPE_UDP:
1256 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1257 (udp_sk(sk))->encap_type = 0;
1258 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001259 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001260 break;
1261 case L2TP_ENCAPTYPE_IP:
1262 break;
1263 }
James Chapmanfd558d12010-04-02 06:18:33 +00001264
1265 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001266 sk->sk_destruct = tunnel->old_sk_destruct;
1267 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001268 tunnel->sock = NULL;
1269
1270 /* Remove the tunnel struct from the tunnel list */
1271 pn = l2tp_pernet(tunnel->l2tp_net);
1272 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1273 list_del_rcu(&tunnel->list);
1274 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1275 atomic_dec(&l2tp_tunnel_count);
1276
1277 l2tp_tunnel_closeall(tunnel);
1278 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001279
1280 /* Call the original destructor */
1281 if (sk->sk_destruct)
1282 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001283end:
1284 return;
1285}
James Chapmanfd558d12010-04-02 06:18:33 +00001286
1287/* When the tunnel is closed, all the attached sessions need to go too.
1288 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001289void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001290{
1291 int hash;
1292 struct hlist_node *walk;
1293 struct hlist_node *tmp;
1294 struct l2tp_session *session;
1295
1296 BUG_ON(tunnel == NULL);
1297
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001298 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1299 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001300
1301 write_lock_bh(&tunnel->hlist_lock);
1302 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1303again:
1304 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1305 session = hlist_entry(walk, struct l2tp_session, hlist);
1306
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001307 l2tp_info(session, L2TP_MSG_CONTROL,
1308 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001309
1310 hlist_del_init(&session->hlist);
1311
James Chapmanfd558d12010-04-02 06:18:33 +00001312 if (session->ref != NULL)
1313 (*session->ref)(session);
1314
1315 write_unlock_bh(&tunnel->hlist_lock);
1316
Tom Parkinf6e16b22013-03-19 06:11:23 +00001317 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001318 l2tp_session_queue_purge(session);
1319
James Chapmanfd558d12010-04-02 06:18:33 +00001320 if (session->session_close != NULL)
1321 (*session->session_close)(session);
1322
1323 if (session->deref != NULL)
1324 (*session->deref)(session);
1325
Tom Parkin9980d002013-03-19 06:11:13 +00001326 l2tp_session_dec_refcount(session);
1327
James Chapmanfd558d12010-04-02 06:18:33 +00001328 write_lock_bh(&tunnel->hlist_lock);
1329
1330 /* Now restart from the beginning of this hash
1331 * chain. We always remove a session from the
1332 * list so we are guaranteed to make forward
1333 * progress.
1334 */
1335 goto again;
1336 }
1337 }
1338 write_unlock_bh(&tunnel->hlist_lock);
1339}
Tom Parkine34f4c72013-03-19 06:11:14 +00001340EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001341
Tom Parkin9980d002013-03-19 06:11:13 +00001342/* Tunnel socket destroy hook for UDP encapsulation */
1343static void l2tp_udp_encap_destroy(struct sock *sk)
1344{
1345 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1346 if (tunnel) {
1347 l2tp_tunnel_closeall(tunnel);
1348 sock_put(sk);
1349 }
1350}
1351
James Chapmanfd558d12010-04-02 06:18:33 +00001352/* Really kill the tunnel.
1353 * Come here only when all sessions have been cleared from the tunnel.
1354 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001355static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001356{
James Chapmanfd558d12010-04-02 06:18:33 +00001357 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1358 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001359 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001360 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001361}
James Chapmanfd558d12010-04-02 06:18:33 +00001362
Tom Parkinf8ccac02013-01-31 23:43:00 +00001363/* Workqueue tunnel deletion function */
1364static void l2tp_tunnel_del_work(struct work_struct *work)
1365{
1366 struct l2tp_tunnel *tunnel = NULL;
1367 struct socket *sock = NULL;
1368 struct sock *sk = NULL;
1369
1370 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1371 sk = l2tp_tunnel_sock_lookup(tunnel);
1372 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001373 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001374
1375 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001376
Tom Parkin02d13ed2013-03-19 06:11:18 +00001377 /* If the tunnel socket was created by userspace, then go through the
1378 * inet layer to shut the socket down, and let userspace close it.
1379 * Otherwise, if we created the socket directly within the kernel, use
1380 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001381 * In either case the tunnel resources are freed in the socket
1382 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001383 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001384 if (tunnel->fd >= 0) {
1385 if (sock)
1386 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001387 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001388 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001389 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001390 sock_release(sock);
1391 }
Tom Parkin167eb172013-01-31 23:43:03 +00001392 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001393
1394 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001395out:
1396 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001397}
James Chapmanfd558d12010-04-02 06:18:33 +00001398
James Chapman789a4a22010-04-02 06:19:40 +00001399/* Create a socket for the tunnel, if one isn't set up by
1400 * userspace. This is used for static tunnels where there is no
1401 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001402 *
1403 * Since we don't want these sockets to keep a namespace alive by
1404 * themselves, we drop the socket's namespace refcount after creation.
1405 * These sockets are freed when the namespace exits using the pernet
1406 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001407 */
Tom Parkin167eb172013-01-31 23:43:03 +00001408static int l2tp_tunnel_sock_create(struct net *net,
1409 u32 tunnel_id,
1410 u32 peer_tunnel_id,
1411 struct l2tp_tunnel_cfg *cfg,
1412 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001413{
1414 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001415 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001416 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001417
1418 switch (cfg->encap) {
1419 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001420 memset(&udp_conf, 0, sizeof(udp_conf));
1421
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001422#if IS_ENABLED(CONFIG_IPV6)
1423 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001424 udp_conf.family = AF_INET6;
1425 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1426 sizeof(udp_conf.local_ip6));
1427 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1428 sizeof(udp_conf.peer_ip6));
1429 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001430 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001431 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001432 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001433 } else
1434#endif
1435 {
Tom Herbert85644b42014-07-13 19:49:48 -07001436 udp_conf.family = AF_INET;
1437 udp_conf.local_ip = cfg->local_ip;
1438 udp_conf.peer_ip = cfg->peer_ip;
1439 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001440 }
James Chapman789a4a22010-04-02 06:19:40 +00001441
Tom Herbert85644b42014-07-13 19:49:48 -07001442 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1443 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1444
1445 err = udp_sock_create(net, &udp_conf, &sock);
1446 if (err < 0)
1447 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001448
1449 break;
1450
1451 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001452#if IS_ENABLED(CONFIG_IPV6)
1453 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001454 struct sockaddr_l2tpip6 ip6_addr = {0};
1455
Eric W. Biederman26abe142015-05-08 21:10:31 -05001456 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001457 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001458 if (err < 0)
1459 goto out;
1460
James Chapman5dac94e2012-04-29 21:48:55 +00001461 ip6_addr.l2tp_family = AF_INET6;
1462 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1463 sizeof(ip6_addr.l2tp_addr));
1464 ip6_addr.l2tp_conn_id = tunnel_id;
1465 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1466 sizeof(ip6_addr));
1467 if (err < 0)
1468 goto out;
1469
1470 ip6_addr.l2tp_family = AF_INET6;
1471 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1472 sizeof(ip6_addr.l2tp_addr));
1473 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1474 err = kernel_connect(sock,
1475 (struct sockaddr *) &ip6_addr,
1476 sizeof(ip6_addr), 0);
1477 if (err < 0)
1478 goto out;
1479 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001480#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001481 {
Tom Herbert85644b42014-07-13 19:49:48 -07001482 struct sockaddr_l2tpip ip_addr = {0};
1483
Eric W. Biederman26abe142015-05-08 21:10:31 -05001484 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001485 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001486 if (err < 0)
1487 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001488
James Chapman5dac94e2012-04-29 21:48:55 +00001489 ip_addr.l2tp_family = AF_INET;
1490 ip_addr.l2tp_addr = cfg->local_ip;
1491 ip_addr.l2tp_conn_id = tunnel_id;
1492 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1493 sizeof(ip_addr));
1494 if (err < 0)
1495 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001496
James Chapman5dac94e2012-04-29 21:48:55 +00001497 ip_addr.l2tp_family = AF_INET;
1498 ip_addr.l2tp_addr = cfg->peer_ip;
1499 ip_addr.l2tp_conn_id = peer_tunnel_id;
1500 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1501 sizeof(ip_addr), 0);
1502 if (err < 0)
1503 goto out;
1504 }
James Chapman789a4a22010-04-02 06:19:40 +00001505 break;
1506
1507 default:
1508 goto out;
1509 }
1510
1511out:
Tom Parkin167eb172013-01-31 23:43:03 +00001512 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001513 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001514 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001515 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001516 *sockp = NULL;
1517 }
1518
1519 return err;
1520}
1521
Eric Dumazet37159ef2012-09-04 07:18:57 +00001522static struct lock_class_key l2tp_socket_class;
1523
James Chapmanfd558d12010-04-02 06:18:33 +00001524int 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)
1525{
1526 struct l2tp_tunnel *tunnel = NULL;
1527 int err;
1528 struct socket *sock = NULL;
1529 struct sock *sk = NULL;
1530 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001531 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001532
1533 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001534 * the userspace L2TP daemon. If not specified, create a
1535 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001536 */
James Chapman789a4a22010-04-02 06:19:40 +00001537 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001538 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1539 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001540 if (err < 0)
1541 goto err;
1542 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001543 sock = sockfd_lookup(fd, &err);
1544 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001545 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001546 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001547 err = -EBADF;
1548 goto err;
1549 }
1550
1551 /* Reject namespace mismatches */
1552 if (!net_eq(sock_net(sock->sk), net)) {
1553 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1554 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001555 goto err;
1556 }
James Chapmanfd558d12010-04-02 06:18:33 +00001557 }
1558
1559 sk = sock->sk;
1560
James Chapman0d767512010-04-02 06:19:00 +00001561 if (cfg != NULL)
1562 encap = cfg->encap;
1563
James Chapmanfd558d12010-04-02 06:18:33 +00001564 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001565 switch (encap) {
1566 case L2TP_ENCAPTYPE_UDP:
1567 err = -EPROTONOSUPPORT;
1568 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001569 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001570 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1571 goto err;
1572 }
1573 break;
1574 case L2TP_ENCAPTYPE_IP:
1575 err = -EPROTONOSUPPORT;
1576 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001577 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001578 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1579 goto err;
1580 }
1581 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001582 }
1583
1584 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001585 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001586 if (tunnel != NULL) {
1587 /* This socket has already been prepped */
1588 err = -EBUSY;
1589 goto err;
1590 }
1591
James Chapmanfd558d12010-04-02 06:18:33 +00001592 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1593 if (tunnel == NULL) {
1594 err = -ENOMEM;
1595 goto err;
1596 }
1597
1598 tunnel->version = version;
1599 tunnel->tunnel_id = tunnel_id;
1600 tunnel->peer_tunnel_id = peer_tunnel_id;
1601 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1602
1603 tunnel->magic = L2TP_TUNNEL_MAGIC;
1604 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1605 rwlock_init(&tunnel->hlist_lock);
1606
1607 /* The net we belong to */
1608 tunnel->l2tp_net = net;
1609 pn = l2tp_pernet(net);
1610
James Chapman0d767512010-04-02 06:19:00 +00001611 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001612 tunnel->debug = cfg->debug;
1613
François Cachereule18503f2013-10-02 10:16:02 +02001614#if IS_ENABLED(CONFIG_IPV6)
1615 if (sk->sk_family == PF_INET6) {
1616 struct ipv6_pinfo *np = inet6_sk(sk);
1617
1618 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001619 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001620 struct inet_sock *inet = inet_sk(sk);
1621
1622 tunnel->v4mapped = true;
1623 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001624 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1625 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001626 } else {
1627 tunnel->v4mapped = false;
1628 }
1629 }
1630#endif
1631
James Chapmanfd558d12010-04-02 06:18:33 +00001632 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001633 tunnel->encap = encap;
1634 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001635 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001636
Andy Zhouc8fffce2014-09-16 17:31:19 -07001637 udp_cfg.sk_user_data = tunnel;
1638 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1639 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1640 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1641
1642 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1643 } else {
1644 sk->sk_user_data = tunnel;
1645 }
James Chapmanfd558d12010-04-02 06:18:33 +00001646
1647 /* Hook on the tunnel socket destructor so that we can cleanup
1648 * if the tunnel socket goes away.
1649 */
1650 tunnel->old_sk_destruct = sk->sk_destruct;
1651 sk->sk_destruct = &l2tp_tunnel_destruct;
1652 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001653 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001654 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1655
James Chapmanfd558d12010-04-02 06:18:33 +00001656 sk->sk_allocation = GFP_ATOMIC;
1657
Tom Parkinf8ccac02013-01-31 23:43:00 +00001658 /* Init delete workqueue struct */
1659 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1660
James Chapmanfd558d12010-04-02 06:18:33 +00001661 /* Add tunnel to our list */
1662 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001663 atomic_inc(&l2tp_tunnel_count);
1664
1665 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001666 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001667 */
1668 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001669 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1670 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1671 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001672
1673 err = 0;
1674err:
1675 if (tunnelp)
1676 *tunnelp = tunnel;
1677
James Chapman789a4a22010-04-02 06:19:40 +00001678 /* If tunnel's socket was created by the kernel, it doesn't
1679 * have a file.
1680 */
1681 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001682 sockfd_put(sock);
1683
1684 return err;
1685}
1686EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1687
James Chapman309795f2010-04-02 06:19:10 +00001688/* This function is used by the netlink TUNNEL_DELETE command.
1689 */
1690int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1691{
Alexander Couzens06a15f52015-09-28 11:32:42 +02001692 l2tp_tunnel_inc_refcount(tunnel);
Tom Parkin2b551c62013-03-19 06:11:16 +00001693 l2tp_tunnel_closeall(tunnel);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001694 if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
1695 l2tp_tunnel_dec_refcount(tunnel);
1696 return 1;
1697 }
1698 return 0;
James Chapman309795f2010-04-02 06:19:10 +00001699}
1700EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1701
James Chapmanfd558d12010-04-02 06:18:33 +00001702/* Really kill the session.
1703 */
1704void l2tp_session_free(struct l2tp_session *session)
1705{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001706 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001707
1708 BUG_ON(atomic_read(&session->ref_count) != 0);
1709
Tom Parkinf6e16b22013-03-19 06:11:23 +00001710 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001711 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001712 if (session->session_id != 0)
1713 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001714 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001715 session->tunnel = NULL;
1716 l2tp_tunnel_dec_refcount(tunnel);
1717 }
1718
1719 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001720}
1721EXPORT_SYMBOL_GPL(l2tp_session_free);
1722
Tom Parkinf6e16b22013-03-19 06:11:23 +00001723/* Remove an l2tp session from l2tp_core's hash lists.
1724 * Provides a tidyup interface for pseudowire code which can't just route all
1725 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1726 * callback.
1727 */
1728void __l2tp_session_unhash(struct l2tp_session *session)
1729{
1730 struct l2tp_tunnel *tunnel = session->tunnel;
1731
1732 /* Remove the session from core hashes */
1733 if (tunnel) {
1734 /* Remove from the per-tunnel hash */
1735 write_lock_bh(&tunnel->hlist_lock);
1736 hlist_del_init(&session->hlist);
1737 write_unlock_bh(&tunnel->hlist_lock);
1738
1739 /* For L2TPv3 we have a per-net hash: remove from there, too */
1740 if (tunnel->version != L2TP_HDR_VER_2) {
1741 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1742 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1743 hlist_del_init_rcu(&session->global_hlist);
1744 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1745 synchronize_rcu();
1746 }
1747 }
1748}
1749EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1750
James Chapman309795f2010-04-02 06:19:10 +00001751/* This function is used by the netlink SESSION_DELETE command and by
1752 pseudowire modules.
1753 */
1754int l2tp_session_delete(struct l2tp_session *session)
1755{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001756 if (session->ref)
1757 (*session->ref)(session);
1758 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001759 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001760 if (session->session_close != NULL)
1761 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001762 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001763 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001764 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001765 return 0;
1766}
1767EXPORT_SYMBOL_GPL(l2tp_session_delete);
1768
James Chapmanf7faffa2010-04-02 06:18:49 +00001769/* We come here whenever a session's send_seq, cookie_len or
1770 * l2specific_len parameters are set.
1771 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001772void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001773{
1774 if (version == L2TP_HDR_VER_2) {
1775 session->hdr_len = 6;
1776 if (session->send_seq)
1777 session->hdr_len += 4;
1778 } else {
James Chapman0d767512010-04-02 06:19:00 +00001779 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1780 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1781 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001782 }
1783
1784}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001785EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001786
James Chapmanfd558d12010-04-02 06:18:33 +00001787struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1788{
1789 struct l2tp_session *session;
1790
1791 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1792 if (session != NULL) {
1793 session->magic = L2TP_SESSION_MAGIC;
1794 session->tunnel = tunnel;
1795
1796 session->session_id = session_id;
1797 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001798 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001799 if (tunnel->version == L2TP_HDR_VER_2)
1800 session->nr_max = 0xffff;
1801 else
1802 session->nr_max = 0xffffff;
1803 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001804 session->nr_oos_count_max = 4;
1805
1806 /* Use NR of first received packet */
1807 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001808
1809 sprintf(&session->name[0], "sess %u/%u",
1810 tunnel->tunnel_id, session->session_id);
1811
1812 skb_queue_head_init(&session->reorder_q);
1813
1814 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001815 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001816
1817 /* Inherit debug options from tunnel */
1818 session->debug = tunnel->debug;
1819
1820 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001821 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001822 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001823 session->mtu = cfg->mtu;
1824 session->mru = cfg->mru;
1825 session->send_seq = cfg->send_seq;
1826 session->recv_seq = cfg->recv_seq;
1827 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001828 session->reorder_timeout = cfg->reorder_timeout;
1829 session->offset = cfg->offset;
1830 session->l2specific_type = cfg->l2specific_type;
1831 session->l2specific_len = cfg->l2specific_len;
1832 session->cookie_len = cfg->cookie_len;
1833 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1834 session->peer_cookie_len = cfg->peer_cookie_len;
1835 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001836 }
1837
James Chapmanf7faffa2010-04-02 06:18:49 +00001838 if (tunnel->version == L2TP_HDR_VER_2)
1839 session->build_header = l2tp_build_l2tpv2_header;
1840 else
1841 session->build_header = l2tp_build_l2tpv3_header;
1842
1843 l2tp_session_set_header_len(session, tunnel->version);
1844
James Chapmanfd558d12010-04-02 06:18:33 +00001845 /* Bump the reference count. The session context is deleted
1846 * only when this drops to zero.
1847 */
1848 l2tp_session_inc_refcount(session);
1849 l2tp_tunnel_inc_refcount(tunnel);
1850
1851 /* Ensure tunnel socket isn't deleted */
1852 sock_hold(tunnel->sock);
1853
1854 /* Add session to the tunnel's hash list */
1855 write_lock_bh(&tunnel->hlist_lock);
1856 hlist_add_head(&session->hlist,
1857 l2tp_session_id_hash(tunnel, session_id));
1858 write_unlock_bh(&tunnel->hlist_lock);
1859
James Chapmanf7faffa2010-04-02 06:18:49 +00001860 /* And to the global session list if L2TPv3 */
1861 if (tunnel->version != L2TP_HDR_VER_2) {
1862 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1863
James Chapmane02d4942010-04-02 06:19:16 +00001864 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1865 hlist_add_head_rcu(&session->global_hlist,
1866 l2tp_session_id_hash_2(pn, session_id));
1867 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001868 }
1869
James Chapmanfd558d12010-04-02 06:18:33 +00001870 /* Ignore management session in session count value */
1871 if (session->session_id != 0)
1872 atomic_inc(&l2tp_session_count);
1873 }
1874
1875 return session;
1876}
1877EXPORT_SYMBOL_GPL(l2tp_session_create);
1878
1879/*****************************************************************************
1880 * Init and cleanup
1881 *****************************************************************************/
1882
1883static __net_init int l2tp_init_net(struct net *net)
1884{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001885 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001886 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001887
James Chapmanfd558d12010-04-02 06:18:33 +00001888 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001889 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001890
James Chapmanf7faffa2010-04-02 06:18:49 +00001891 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1892 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1893
James Chapmane02d4942010-04-02 06:19:16 +00001894 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001895
James Chapmanfd558d12010-04-02 06:18:33 +00001896 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001897}
1898
Tom Parkin167eb172013-01-31 23:43:03 +00001899static __net_exit void l2tp_exit_net(struct net *net)
1900{
1901 struct l2tp_net *pn = l2tp_pernet(net);
1902 struct l2tp_tunnel *tunnel = NULL;
1903
1904 rcu_read_lock_bh();
1905 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1906 (void)l2tp_tunnel_delete(tunnel);
1907 }
1908 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001909
1910 flush_workqueue(l2tp_wq);
1911 rcu_barrier();
Tom Parkin167eb172013-01-31 23:43:03 +00001912}
1913
James Chapmanfd558d12010-04-02 06:18:33 +00001914static struct pernet_operations l2tp_net_ops = {
1915 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001916 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001917 .id = &l2tp_net_id,
1918 .size = sizeof(struct l2tp_net),
1919};
1920
1921static int __init l2tp_init(void)
1922{
1923 int rc = 0;
1924
1925 rc = register_pernet_device(&l2tp_net_ops);
1926 if (rc)
1927 goto out;
1928
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001929 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001930 if (!l2tp_wq) {
1931 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001932 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001933 rc = -ENOMEM;
1934 goto out;
1935 }
1936
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001937 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001938
1939out:
1940 return rc;
1941}
1942
1943static void __exit l2tp_exit(void)
1944{
1945 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001946 if (l2tp_wq) {
1947 destroy_workqueue(l2tp_wq);
1948 l2tp_wq = NULL;
1949 }
James Chapmanfd558d12010-04-02 06:18:33 +00001950}
1951
1952module_init(l2tp_init);
1953module_exit(l2tp_exit);
1954
1955MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1956MODULE_DESCRIPTION("L2TP core");
1957MODULE_LICENSE("GPL");
1958MODULE_VERSION(L2TP_DRV_VERSION);
1959