blob: c61a467fd9b8198fd535c50ca2f2d1140930594d [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
Tom Parkinf8ccac02013-01-31 23:43:00 +0000103static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000104
105/* per-net private data for this module */
106static unsigned int l2tp_net_id;
107struct l2tp_net {
108 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000109 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000110 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000111 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000112};
113
Paolo Abenib954f942018-03-12 14:54:24 +0100114#if IS_ENABLED(CONFIG_IPV6)
115static bool l2tp_sk_is_v6(struct sock *sk)
116{
117 return sk->sk_family == PF_INET6 &&
118 !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
119}
120#endif
stephen hemmingerfc130842010-10-21 07:50:46 +0000121
David S. Miller8d8a51e2013-10-08 15:44:26 -0400122static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
123{
124 return sk->sk_user_data;
125}
126
Guillaume Nault9aaef502017-04-12 10:05:29 +0200127static inline struct l2tp_net *l2tp_pernet(const struct net *net)
James Chapmanfd558d12010-04-02 06:18:33 +0000128{
129 BUG_ON(!net);
130
131 return net_generic(net, l2tp_net_id);
132}
133
James Chapmanf7faffa2010-04-02 06:18:49 +0000134/* Session hash global list for L2TPv3.
135 * The session_id SHOULD be random according to RFC3931, but several
136 * L2TP implementations use incrementing session_ids. So we do a real
137 * hash on the session_id, rather than a simple bitmask.
138 */
139static inline struct hlist_head *
140l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
141{
142 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
143
144}
145
James Chapmanfd558d12010-04-02 06:18:33 +0000146/* Session hash list.
147 * The session_id SHOULD be random according to RFC2661, but several
148 * L2TP implementations (Cisco and Microsoft) use incrementing
149 * session_ids. So we do a real hash on the session_id, rather than a
150 * simple bitmask.
151 */
152static inline struct hlist_head *
153l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
154{
155 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
156}
157
James Chapmand00fa9a2018-02-23 17:45:45 +0000158void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
159{
160 sock_put(tunnel->sock);
161 /* the tunnel is freed in the socket destructor */
162}
163EXPORT_SYMBOL(l2tp_tunnel_free);
164
Guillaume Nault54652eb2017-08-25 16:51:40 +0200165/* Lookup a tunnel. A new reference is held on the returned tunnel. */
166struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
167{
168 const struct l2tp_net *pn = l2tp_pernet(net);
169 struct l2tp_tunnel *tunnel;
170
171 rcu_read_lock_bh();
172 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
173 if (tunnel->tunnel_id == tunnel_id) {
174 l2tp_tunnel_inc_refcount(tunnel);
175 rcu_read_unlock_bh();
176
177 return tunnel;
178 }
179 }
180 rcu_read_unlock_bh();
181
182 return NULL;
183}
184EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
185
Guillaume Nault5846c132018-04-12 20:50:33 +0200186struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
187{
188 const struct l2tp_net *pn = l2tp_pernet(net);
189 struct l2tp_tunnel *tunnel;
190 int count = 0;
191
192 rcu_read_lock_bh();
193 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
194 if (++count > nth) {
195 l2tp_tunnel_inc_refcount(tunnel);
196 rcu_read_unlock_bh();
197 return tunnel;
198 }
199 }
200 rcu_read_unlock_bh();
201
202 return NULL;
203}
204EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
205
Guillaume Naulta4346212017-10-31 17:36:42 +0100206/* Lookup a session. A new reference is held on the returned session. */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200207struct l2tp_session *l2tp_session_get(const struct net *net,
Guillaume Nault61b9a042017-03-31 13:02:25 +0200208 struct l2tp_tunnel *tunnel,
Guillaume Naulta4346212017-10-31 17:36:42 +0100209 u32 session_id)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200210{
211 struct hlist_head *session_list;
212 struct l2tp_session *session;
213
214 if (!tunnel) {
215 struct l2tp_net *pn = l2tp_pernet(net);
216
217 session_list = l2tp_session_id_hash_2(pn, session_id);
218
219 rcu_read_lock_bh();
220 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
221 if (session->session_id == session_id) {
222 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200223 rcu_read_unlock_bh();
224
225 return session;
226 }
227 }
228 rcu_read_unlock_bh();
229
230 return NULL;
231 }
232
233 session_list = l2tp_session_id_hash(tunnel, session_id);
234 read_lock_bh(&tunnel->hlist_lock);
235 hlist_for_each_entry(session, session_list, hlist) {
236 if (session->session_id == session_id) {
237 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200238 read_unlock_bh(&tunnel->hlist_lock);
239
240 return session;
241 }
242 }
243 read_unlock_bh(&tunnel->hlist_lock);
244
245 return NULL;
246}
247EXPORT_SYMBOL_GPL(l2tp_session_get);
248
Guillaume Naulta4346212017-10-31 17:36:42 +0100249struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000250{
251 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000252 struct l2tp_session *session;
253 int count = 0;
254
255 read_lock_bh(&tunnel->hlist_lock);
256 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000258 if (++count > nth) {
Guillaume Naulte08293a2017-04-03 12:03:13 +0200259 l2tp_session_inc_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000260 read_unlock_bh(&tunnel->hlist_lock);
261 return session;
262 }
263 }
264 }
265
266 read_unlock_bh(&tunnel->hlist_lock);
267
268 return NULL;
269}
Guillaume Naulte08293a2017-04-03 12:03:13 +0200270EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000271
James Chapman309795f2010-04-02 06:19:10 +0000272/* Lookup a session by interface name.
273 * This is very inefficient but is only used by management interfaces.
274 */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200275struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
Guillaume Naulta4346212017-10-31 17:36:42 +0100276 const char *ifname)
James Chapman309795f2010-04-02 06:19:10 +0000277{
278 struct l2tp_net *pn = l2tp_pernet(net);
279 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000280 struct l2tp_session *session;
281
James Chapmane02d4942010-04-02 06:19:16 +0000282 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000283 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800284 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000285 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200286 l2tp_session_inc_refcount(session);
James Chapmane02d4942010-04-02 06:19:16 +0000287 rcu_read_unlock_bh();
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200288
James Chapman309795f2010-04-02 06:19:10 +0000289 return session;
290 }
291 }
292 }
293
James Chapmane02d4942010-04-02 06:19:16 +0000294 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000295
296 return NULL;
297}
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200298EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000299
Guillaume Nault3953ae72017-10-27 16:51:50 +0200300int l2tp_session_register(struct l2tp_session *session,
301 struct l2tp_tunnel *tunnel)
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200302{
303 struct l2tp_session *session_walk;
304 struct hlist_head *g_head;
305 struct hlist_head *head;
306 struct l2tp_net *pn;
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200307 int err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200308
309 head = l2tp_session_id_hash(tunnel, session->session_id);
310
311 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200312 if (!tunnel->acpt_newsess) {
313 err = -ENODEV;
314 goto err_tlock;
315 }
316
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200317 hlist_for_each_entry(session_walk, head, hlist)
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200318 if (session_walk->session_id == session->session_id) {
319 err = -EEXIST;
320 goto err_tlock;
321 }
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200322
323 if (tunnel->version == L2TP_HDR_VER_3) {
324 pn = l2tp_pernet(tunnel->l2tp_net);
Guillaume Nault363a3412018-06-25 16:07:24 +0200325 g_head = l2tp_session_id_hash_2(pn, session->session_id);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200326
327 spin_lock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200328
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200329 hlist_for_each_entry(session_walk, g_head, global_hlist)
330 if (session_walk->session_id == session->session_id) {
331 err = -EEXIST;
332 goto err_tlock_pnlock;
333 }
334
335 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200336 hlist_add_head_rcu(&session->global_hlist, g_head);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200337
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200338 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200339 } else {
340 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200341 }
342
343 hlist_add_head(&session->hlist, head);
344 write_unlock_bh(&tunnel->hlist_lock);
345
346 return 0;
347
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200348err_tlock_pnlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200349 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200350err_tlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200351 write_unlock_bh(&tunnel->hlist_lock);
352
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200353 return err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200354}
Guillaume Nault3953ae72017-10-27 16:51:50 +0200355EXPORT_SYMBOL_GPL(l2tp_session_register);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200356
James Chapmanfd558d12010-04-02 06:18:33 +0000357/*****************************************************************************
358 * Receive data handling
359 *****************************************************************************/
360
361/* Queue a skb in order. We come here only if the skb has an L2TP sequence
362 * number.
363 */
364static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
365{
366 struct sk_buff *skbp;
367 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000368 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000369
370 spin_lock_bh(&session->reorder_q.lock);
371 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
372 if (L2TP_SKB_CB(skbp)->ns > ns) {
373 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000374 l2tp_dbg(session, L2TP_MSG_SEQ,
375 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
376 session->name, ns, L2TP_SKB_CB(skbp)->ns,
377 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000378 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000379 goto out;
380 }
381 }
382
383 __skb_queue_tail(&session->reorder_q, skb);
384
385out:
386 spin_unlock_bh(&session->reorder_q.lock);
387}
388
389/* Dequeue a single skb.
390 */
391static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
392{
393 struct l2tp_tunnel *tunnel = session->tunnel;
394 int length = L2TP_SKB_CB(skb)->length;
395
396 /* We're about to requeue the skb, so return resources
397 * to its current owner (a socket receive buffer).
398 */
399 skb_orphan(skb);
400
Tom Parkin7b7c0712013-03-19 06:11:22 +0000401 atomic_long_inc(&tunnel->stats.rx_packets);
402 atomic_long_add(length, &tunnel->stats.rx_bytes);
403 atomic_long_inc(&session->stats.rx_packets);
404 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000405
406 if (L2TP_SKB_CB(skb)->has_seq) {
407 /* Bump our Nr */
408 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100409 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000410
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000411 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
412 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000413 }
414
415 /* call private receive handler */
416 if (session->recv_skb != NULL)
417 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
418 else
419 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000420}
421
422/* Dequeue skbs from the session's reorder_q, subject to packet order.
423 * Skbs that have been in the queue for too long are simply discarded.
424 */
425static void l2tp_recv_dequeue(struct l2tp_session *session)
426{
427 struct sk_buff *skb;
428 struct sk_buff *tmp;
429
430 /* If the pkt at the head of the queue has the nr that we
431 * expect to send up next, dequeue it and any other
432 * in-sequence packets behind it.
433 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000434start:
James Chapmanfd558d12010-04-02 06:18:33 +0000435 spin_lock_bh(&session->reorder_q.lock);
436 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
437 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000438 atomic_long_inc(&session->stats.rx_seq_discards);
439 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000440 l2tp_dbg(session, L2TP_MSG_SEQ,
441 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
442 session->name, L2TP_SKB_CB(skb)->ns,
443 L2TP_SKB_CB(skb)->length, session->nr,
444 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000445 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000446 __skb_unlink(skb, &session->reorder_q);
447 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000448 continue;
449 }
450
451 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000452 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000453 l2tp_dbg(session, L2TP_MSG_SEQ,
454 "%s: advancing nr to next pkt: %u -> %u",
455 session->name, session->nr,
456 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000457 session->reorder_skip = 0;
458 session->nr = L2TP_SKB_CB(skb)->ns;
459 }
James Chapmanfd558d12010-04-02 06:18:33 +0000460 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000461 l2tp_dbg(session, L2TP_MSG_SEQ,
462 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
463 session->name, L2TP_SKB_CB(skb)->ns,
464 L2TP_SKB_CB(skb)->length, session->nr,
465 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000466 goto out;
467 }
468 }
469 __skb_unlink(skb, &session->reorder_q);
470
471 /* Process the skb. We release the queue lock while we
472 * do so to let other contexts process the queue.
473 */
474 spin_unlock_bh(&session->reorder_q.lock);
475 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000476 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000477 }
478
479out:
480 spin_unlock_bh(&session->reorder_q.lock);
481}
482
James Chapman8a1631d2013-07-02 20:28:59 +0100483static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
484{
485 u32 nws;
486
487 if (nr >= session->nr)
488 nws = nr - session->nr;
489 else
490 nws = (session->nr_max + 1) - (session->nr - nr);
491
492 return nws < session->nr_window_size;
493}
494
James Chapmanb6dc01a2013-07-02 20:28:58 +0100495/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
496 * acceptable, else non-zero.
497 */
498static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
499{
James Chapman8a1631d2013-07-02 20:28:59 +0100500 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
501 /* Packet sequence number is outside allowed window.
502 * Discard it.
503 */
504 l2tp_dbg(session, L2TP_MSG_SEQ,
505 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
506 session->name, L2TP_SKB_CB(skb)->ns,
507 L2TP_SKB_CB(skb)->length, session->nr);
508 goto discard;
509 }
510
James Chapmanb6dc01a2013-07-02 20:28:58 +0100511 if (session->reorder_timeout != 0) {
512 /* Packet reordering enabled. Add skb to session's
513 * reorder queue, in order of ns.
514 */
515 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100516 goto out;
517 }
518
519 /* Packet reordering disabled. Discard out-of-sequence packets, while
520 * tracking the number if in-sequence packets after the first OOS packet
521 * is seen. After nr_oos_count_max in-sequence packets, reset the
522 * sequence number to re-enable packet reception.
523 */
524 if (L2TP_SKB_CB(skb)->ns == session->nr) {
525 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100526 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100527 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
528 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
529
530 if (nr_oos == nr_next)
531 session->nr_oos_count++;
532 else
533 session->nr_oos_count = 0;
534
535 session->nr_oos = nr_oos;
536 if (session->nr_oos_count > session->nr_oos_count_max) {
537 session->reorder_skip = 1;
538 l2tp_dbg(session, L2TP_MSG_SEQ,
539 "%s: %d oos packets received. Resetting sequence numbers\n",
540 session->name, session->nr_oos_count);
541 }
542 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100543 atomic_long_inc(&session->stats.rx_seq_discards);
544 l2tp_dbg(session, L2TP_MSG_SEQ,
545 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
546 session->name, L2TP_SKB_CB(skb)->ns,
547 L2TP_SKB_CB(skb)->length, session->nr,
548 skb_queue_len(&session->reorder_q));
549 goto discard;
550 }
551 skb_queue_tail(&session->reorder_q, skb);
552 }
553
James Chapmana0dbd822013-07-02 20:29:00 +0100554out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100555 return 0;
556
557discard:
558 return 1;
559}
560
James Chapmanf7faffa2010-04-02 06:18:49 +0000561/* Do receive processing of L2TP data frames. We handle both L2TPv2
562 * and L2TPv3 data frames here.
563 *
564 * L2TPv2 Data Message Header
565 *
566 * 0 1 2 3
567 * 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
568 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
569 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
570 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
571 * | Tunnel ID | Session ID |
572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
573 * | Ns (opt) | Nr (opt) |
574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
575 * | Offset Size (opt) | Offset pad... (opt)
576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577 *
578 * Data frames are marked by T=0. All other fields are the same as
579 * those in L2TP control frames.
580 *
581 * L2TPv3 Data Message Header
582 *
583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * | L2TP Session Header |
585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * | L2-Specific Sublayer |
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * | Tunnel Payload ...
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 *
591 * L2TPv3 Session Header Over IP
592 *
593 * 0 1 2 3
594 * 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
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | Session ID |
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * | Cookie (optional, maximum 64 bits)...
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * |
601 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 *
603 * L2TPv3 L2-Specific Sublayer Format
604 *
605 * 0 1 2 3
606 * 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
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * |x|S|x|x|x|x|x|x| Sequence Number |
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 *
Guillaume Nault23fe8462018-01-05 19:47:14 +0100611 * Cookie value and sublayer format are negotiated with the peer when
612 * the session is set up. Unlike L2TPv2, we do not need to parse the
613 * packet header to determine if optional fields are present.
James Chapmanf7faffa2010-04-02 06:18:49 +0000614 *
615 * Caller must already have parsed the frame and determined that it is
616 * a data (not control) frame before coming here. Fields up to the
617 * session-id have already been parsed and ptr points to the data
618 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000619 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000620void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
621 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
Guillaume Nault2b139e62018-07-25 14:53:33 +0200622 int length)
James Chapmanfd558d12010-04-02 06:18:33 +0000623{
James Chapmanf7faffa2010-04-02 06:18:49 +0000624 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000625 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000626 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000627
James Chapmanf7faffa2010-04-02 06:18:49 +0000628 /* Parse and check optional cookie */
629 if (session->peer_cookie_len > 0) {
630 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000631 l2tp_info(tunnel, L2TP_MSG_DATA,
632 "%s: cookie mismatch (%u/%u). Discarding.\n",
633 tunnel->name, tunnel->tunnel_id,
634 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000635 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000636 goto discard;
637 }
638 ptr += session->peer_cookie_len;
639 }
640
James Chapmanfd558d12010-04-02 06:18:33 +0000641 /* Handle the optional sequence numbers. Sequence numbers are
642 * in different places for L2TPv2 and L2TPv3.
643 *
644 * If we are the LAC, enable/disable sequence numbers under
645 * the control of the LNS. If no sequence numbers present but
646 * we were expecting them, discard frame.
647 */
648 ns = nr = 0;
649 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000650 if (tunnel->version == L2TP_HDR_VER_2) {
651 if (hdrflags & L2TP_HDRFLAG_S) {
652 ns = ntohs(*(__be16 *) ptr);
653 ptr += 2;
654 nr = ntohs(*(__be16 *) ptr);
655 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000656
James Chapmanf7faffa2010-04-02 06:18:49 +0000657 /* Store L2TP info in the skb */
658 L2TP_SKB_CB(skb)->ns = ns;
659 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000660
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000661 l2tp_dbg(session, L2TP_MSG_SEQ,
662 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
663 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000664 }
665 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
666 u32 l2h = ntohl(*(__be32 *) ptr);
667
668 if (l2h & 0x40000000) {
669 ns = l2h & 0x00ffffff;
670
671 /* Store L2TP info in the skb */
672 L2TP_SKB_CB(skb)->ns = ns;
673 L2TP_SKB_CB(skb)->has_seq = 1;
674
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000675 l2tp_dbg(session, L2TP_MSG_SEQ,
676 "%s: recv data ns=%u, session nr=%u\n",
677 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000678 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100679 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000680 }
681
682 if (L2TP_SKB_CB(skb)->has_seq) {
683 /* Received a packet with sequence numbers. If we're the LNS,
684 * check if we sre sending sequence numbers and if not,
685 * configure it so.
686 */
687 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000688 l2tp_info(session, L2TP_MSG_SEQ,
689 "%s: requested to enable seq numbers by LNS\n",
690 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000691 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000692 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000693 }
694 } else {
695 /* No sequence numbers.
696 * If user has configured mandatory sequence numbers, discard.
697 */
698 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000699 l2tp_warn(session, L2TP_MSG_SEQ,
700 "%s: recv data has no seq numbers when required. Discarding.\n",
701 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000702 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000703 goto discard;
704 }
705
706 /* If we're the LAC and we're sending sequence numbers, the
707 * LNS has requested that we no longer send sequence numbers.
708 * If we're the LNS and we're sending sequence numbers, the
709 * LAC is broken. Discard the frame.
710 */
711 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000712 l2tp_info(session, L2TP_MSG_SEQ,
713 "%s: requested to disable seq numbers by LNS\n",
714 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000715 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000716 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000717 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000718 l2tp_warn(session, L2TP_MSG_SEQ,
719 "%s: recv data has no seq numbers when required. Discarding.\n",
720 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000721 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000722 goto discard;
723 }
724 }
725
James Chapman900631e2018-01-03 22:48:06 +0000726 /* Session data offset is defined only for L2TPv2 and is
727 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000728 */
729 if (tunnel->version == L2TP_HDR_VER_2) {
730 /* If offset bit set, skip it. */
731 if (hdrflags & L2TP_HDRFLAG_O) {
732 offset = ntohs(*(__be16 *)ptr);
733 ptr += 2 + offset;
734 }
James Chapman900631e2018-01-03 22:48:06 +0000735 }
James Chapmanfd558d12010-04-02 06:18:33 +0000736
737 offset = ptr - optr;
738 if (!pskb_may_pull(skb, offset))
739 goto discard;
740
741 __skb_pull(skb, offset);
742
James Chapmanfd558d12010-04-02 06:18:33 +0000743 /* Prepare skb for adding to the session's reorder_q. Hold
744 * packets for max reorder_timeout or 1 second if not
745 * reordering.
746 */
747 L2TP_SKB_CB(skb)->length = length;
748 L2TP_SKB_CB(skb)->expires = jiffies +
749 (session->reorder_timeout ? session->reorder_timeout : HZ);
750
751 /* Add packet to the session's receive queue. Reordering is done here, if
752 * enabled. Saved L2TP protocol info is stored in skb->sb[].
753 */
754 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100755 if (l2tp_recv_data_seq(session, skb))
756 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000757 } else {
758 /* No sequence numbers. Add the skb to the tail of the
759 * reorder queue. This ensures that it will be
760 * delivered after all previous sequenced skbs.
761 */
762 skb_queue_tail(&session->reorder_q, skb);
763 }
764
765 /* Try to dequeue as many skbs from reorder_q as we can. */
766 l2tp_recv_dequeue(session);
767
James Chapmanf7faffa2010-04-02 06:18:49 +0000768 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000769
770discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000771 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000772 kfree_skb(skb);
James Chapmanf7faffa2010-04-02 06:18:49 +0000773}
774EXPORT_SYMBOL(l2tp_recv_common);
775
Tom Parkin48f72f92013-03-19 06:11:19 +0000776/* Drop skbs from the session's reorder_q
777 */
Guillaume Nault2e675602018-06-25 16:07:22 +0200778static int l2tp_session_queue_purge(struct l2tp_session *session)
Tom Parkin48f72f92013-03-19 06:11:19 +0000779{
780 struct sk_buff *skb = NULL;
781 BUG_ON(!session);
782 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
783 while ((skb = skb_dequeue(&session->reorder_q))) {
784 atomic_long_inc(&session->stats.rx_errors);
785 kfree_skb(skb);
Tom Parkin48f72f92013-03-19 06:11:19 +0000786 }
787 return 0;
788}
Tom Parkin48f72f92013-03-19 06:11:19 +0000789
James Chapmanf7faffa2010-04-02 06:18:49 +0000790/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
791 * here. The skb is not on a list when we get here.
792 * Returns 0 if the packet was a data packet and was successfully passed on.
793 * Returns 1 if the packet was not a good data packet and could not be
794 * forwarded. All such packets are passed up to userspace to deal with.
795 */
Guillaume Nault2b139e62018-07-25 14:53:33 +0200796static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
James Chapmanf7faffa2010-04-02 06:18:49 +0000797{
798 struct l2tp_session *session = NULL;
799 unsigned char *ptr, *optr;
800 u16 hdrflags;
801 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000802 u16 version;
803 int length;
804
Tom Herbert58d60852014-05-07 16:52:48 -0700805 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000806
807 /* UDP always verifies the packet length. */
808 __skb_pull(skb, sizeof(struct udphdr));
809
810 /* Short packet? */
811 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000812 l2tp_info(tunnel, L2TP_MSG_DATA,
813 "%s: recv short packet (len=%d)\n",
814 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000815 goto error;
816 }
817
James Chapmanf7faffa2010-04-02 06:18:49 +0000818 /* Trace packet contents, if enabled */
819 if (tunnel->debug & L2TP_MSG_DATA) {
820 length = min(32u, skb->len);
821 if (!pskb_may_pull(skb, length))
822 goto error;
823
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000824 pr_debug("%s: recv\n", tunnel->name);
825 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000826 }
827
Eric Dumazete50e7052011-11-08 13:59:44 -0500828 /* Point to L2TP header */
829 optr = ptr = skb->data;
830
James Chapmanf7faffa2010-04-02 06:18:49 +0000831 /* Get L2TP header flags */
832 hdrflags = ntohs(*(__be16 *) ptr);
833
834 /* Check protocol version */
835 version = hdrflags & L2TP_HDR_VER_MASK;
836 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000837 l2tp_info(tunnel, L2TP_MSG_DATA,
838 "%s: recv protocol version mismatch: got %d expected %d\n",
839 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000840 goto error;
841 }
842
843 /* Get length of L2TP packet */
844 length = skb->len;
845
846 /* If type is control packet, it is handled by userspace. */
847 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000848 l2tp_dbg(tunnel, L2TP_MSG_DATA,
849 "%s: recv control packet, len=%d\n",
850 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000851 goto error;
852 }
853
854 /* Skip flags */
855 ptr += 2;
856
857 if (tunnel->version == L2TP_HDR_VER_2) {
858 /* If length is present, skip it */
859 if (hdrflags & L2TP_HDRFLAG_L)
860 ptr += 2;
861
862 /* Extract tunnel and session ID */
863 tunnel_id = ntohs(*(__be16 *) ptr);
864 ptr += 2;
865 session_id = ntohs(*(__be16 *) ptr);
866 ptr += 2;
867 } else {
868 ptr += 2; /* skip reserved bits */
869 tunnel_id = tunnel->tunnel_id;
870 session_id = ntohl(*(__be32 *) ptr);
871 ptr += 4;
872 }
873
874 /* Find the session context */
Guillaume Naulta4346212017-10-31 17:36:42 +0100875 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000876 if (!session || !session->recv_skb) {
Guillaume Naulta4346212017-10-31 17:36:42 +0100877 if (session)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200878 l2tp_session_dec_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200879
James Chapmanf7faffa2010-04-02 06:18:49 +0000880 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000881 l2tp_info(tunnel, L2TP_MSG_DATA,
882 "%s: no session found (%u/%u). Passing up.\n",
883 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000884 goto error;
885 }
886
Guillaume Nault2b139e62018-07-25 14:53:33 +0200887 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200888 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000889
890 return 0;
891
James Chapmanfd558d12010-04-02 06:18:33 +0000892error:
893 /* Put UDP header back */
894 __skb_push(skb, sizeof(struct udphdr));
895
896 return 1;
897}
James Chapmanfd558d12010-04-02 06:18:33 +0000898
899/* UDP encapsulation receive handler. See net/ipv4/udp.c.
900 * Return codes:
901 * 0 : success.
902 * <0: error
903 * >0: skb should be passed up to userspace as UDP.
904 */
905int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
906{
907 struct l2tp_tunnel *tunnel;
908
James Chapmand00fa9a2018-02-23 17:45:45 +0000909 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000910 if (tunnel == NULL)
911 goto pass_up;
912
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000913 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
914 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000915
Guillaume Nault2b139e62018-07-25 14:53:33 +0200916 if (l2tp_udp_recv_core(tunnel, skb))
James Chapmand00fa9a2018-02-23 17:45:45 +0000917 goto pass_up;
James Chapmanfd558d12010-04-02 06:18:33 +0000918
James Chapmanfd558d12010-04-02 06:18:33 +0000919 return 0;
920
James Chapmanfd558d12010-04-02 06:18:33 +0000921pass_up:
922 return 1;
923}
924EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
925
926/************************************************************************
927 * Transmit handling
928 ***********************************************************************/
929
930/* Build an L2TP header for the session into the buffer provided.
931 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000932static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000933{
James Chapmanf7faffa2010-04-02 06:18:49 +0000934 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000935 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000936 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000937 u16 flags = L2TP_HDR_VER_2;
938 u32 tunnel_id = tunnel->peer_tunnel_id;
939 u32 session_id = session->peer_session_id;
940
941 if (session->send_seq)
942 flags |= L2TP_HDRFLAG_S;
943
944 /* Setup L2TP header. */
945 *bufp++ = htons(flags);
946 *bufp++ = htons(tunnel_id);
947 *bufp++ = htons(session_id);
948 if (session->send_seq) {
949 *bufp++ = htons(session->ns);
950 *bufp++ = 0;
951 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000952 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000953 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
954 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000955 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000956
957 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000958}
959
James Chapmanf7faffa2010-04-02 06:18:49 +0000960static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000961{
James Chapman0d767512010-04-02 06:19:00 +0000962 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000963 char *bufp = buf;
964 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000965
James Chapman0d767512010-04-02 06:19:00 +0000966 /* Setup L2TP header. The header differs slightly for UDP and
967 * IP encapsulations. For UDP, there is 4 bytes of flags.
968 */
969 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
970 u16 flags = L2TP_HDR_VER_3;
971 *((__be16 *) bufp) = htons(flags);
972 bufp += 2;
973 *((__be16 *) bufp) = 0;
974 bufp += 2;
975 }
976
James Chapmanf7faffa2010-04-02 06:18:49 +0000977 *((__be32 *) bufp) = htonl(session->peer_session_id);
978 bufp += 4;
979 if (session->cookie_len) {
980 memcpy(bufp, &session->cookie[0], session->cookie_len);
981 bufp += session->cookie_len;
982 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100983 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
984 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000985
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100986 if (session->send_seq) {
987 l2h = 0x40000000 | session->ns;
988 session->ns++;
989 session->ns &= 0xffffff;
990 l2tp_dbg(session, L2TP_MSG_SEQ,
991 "%s: updated ns to %u\n",
992 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +0000993 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100994
995 *((__be32 *)bufp) = htonl(l2h);
996 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +0000997 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000998
999 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001000}
James Chapmanfd558d12010-04-02 06:18:33 +00001001
Guillaume Nault2685fbb2018-06-25 16:07:25 +02001002static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1003 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001004{
1005 struct l2tp_tunnel *tunnel = session->tunnel;
1006 unsigned int len = skb->len;
1007 int error;
1008
1009 /* Debug */
1010 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001011 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001012 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001013 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001014 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001015 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001016
1017 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001018 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1019 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001020
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001021 pr_debug("%s: xmit\n", session->name);
1022 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1023 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001024 }
1025
1026 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001027 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001028#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001029 if (l2tp_sk_is_v6(tunnel->sock))
Eric Dumazetb0270e92014-04-15 12:58:34 -04001030 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001031 else
1032#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001033 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001034
1035 /* Update stats */
1036 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001037 atomic_long_inc(&tunnel->stats.tx_packets);
1038 atomic_long_add(len, &tunnel->stats.tx_bytes);
1039 atomic_long_inc(&session->stats.tx_packets);
1040 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001041 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001042 atomic_long_inc(&tunnel->stats.tx_errors);
1043 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001044 }
James Chapmanfd558d12010-04-02 06:18:33 +00001045}
James Chapmanfd558d12010-04-02 06:18:33 +00001046
James Chapmanfd558d12010-04-02 06:18:33 +00001047/* If caller requires the skb to have a ppp header, the header must be
1048 * inserted in the skb data before calling this function.
1049 */
1050int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1051{
1052 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001053 struct l2tp_tunnel *tunnel = session->tunnel;
1054 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001055 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001056 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001057 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001058 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001059 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1060 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001061 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001062
1063 /* Check that there's enough headroom in the skb to insert IP,
1064 * UDP and L2TP headers. If not enough, expand it to
1065 * make room. Adjust truesize.
1066 */
1067 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001068 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001069 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001070 kfree_skb(skb);
1071 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001072 }
James Chapmanfd558d12010-04-02 06:18:33 +00001073
James Chapmanfd558d12010-04-02 06:18:33 +00001074 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001075 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001076
James Chapman0d767512010-04-02 06:19:00 +00001077 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001078 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1079 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1080 IPSKB_REROUTED);
1081 nf_reset(skb);
1082
David S. Miller6af88da2011-05-08 13:45:20 -07001083 bh_lock_sock(sk);
1084 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001085 kfree_skb(skb);
1086 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001087 goto out_unlock;
1088 }
1089
Paolo Abenib954f942018-03-12 14:54:24 +01001090 /* The user-space may change the connection status for the user-space
1091 * provided socket at run time: we must check it under the socket lock
1092 */
1093 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1094 kfree_skb(skb);
1095 ret = NET_XMIT_DROP;
1096 goto out_unlock;
1097 }
1098
James Chapmanfd558d12010-04-02 06:18:33 +00001099 /* Get routing info from the tunnel socket */
1100 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001101 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001102
David S. Millerd9d8da82011-05-06 22:23:20 -07001103 inet = inet_sk(sk);
1104 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001105 switch (tunnel->encap) {
1106 case L2TP_ENCAPTYPE_UDP:
1107 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001108 __skb_push(skb, sizeof(*uh));
1109 skb_reset_transport_header(skb);
1110 uh = udp_hdr(skb);
1111 uh->source = inet->inet_sport;
1112 uh->dest = inet->inet_dport;
1113 udp_len = uhlen + hdr_len + data_len;
1114 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001115
1116 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001117#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001118 if (l2tp_sk_is_v6(sk))
Tom Herbert77157e12014-06-04 17:19:56 -07001119 udp6_set_csum(udp_get_no_check6_tx(sk),
1120 skb, &inet6_sk(sk)->saddr,
1121 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001122 else
1123#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001124 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1125 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001126 break;
1127
1128 case L2TP_ENCAPTYPE_IP:
1129 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001130 }
1131
David S. Millerd9d8da82011-05-06 22:23:20 -07001132 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001133out_unlock:
1134 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001135
Eric Dumazetb8c84302012-06-28 20:15:13 +00001136 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001137}
1138EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1139
1140/*****************************************************************************
1141 * Tinnel and session create/destroy.
1142 *****************************************************************************/
1143
1144/* Tunnel socket destruct hook.
1145 * The tunnel context is deleted only when all session sockets have been
1146 * closed.
1147 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001148static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001149{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001150 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001151
James Chapmanfd558d12010-04-02 06:18:33 +00001152 if (tunnel == NULL)
1153 goto end;
1154
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001155 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001156
Tom Parkinf8ccac02013-01-31 23:43:00 +00001157 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001158 switch (tunnel->encap) {
1159 case L2TP_ENCAPTYPE_UDP:
1160 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1161 (udp_sk(sk))->encap_type = 0;
1162 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001163 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001164 break;
1165 case L2TP_ENCAPTYPE_IP:
1166 break;
1167 }
James Chapmanfd558d12010-04-02 06:18:33 +00001168
1169 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001170 sk->sk_destruct = tunnel->old_sk_destruct;
1171 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001172
James Chapmanfd558d12010-04-02 06:18:33 +00001173 /* Call the original destructor */
1174 if (sk->sk_destruct)
1175 (*sk->sk_destruct)(sk);
James Chapmand00fa9a2018-02-23 17:45:45 +00001176
1177 kfree_rcu(tunnel, rcu);
James Chapmanfd558d12010-04-02 06:18:33 +00001178end:
1179 return;
1180}
James Chapmanfd558d12010-04-02 06:18:33 +00001181
1182/* When the tunnel is closed, all the attached sessions need to go too.
1183 */
Guillaume Naultd08532b2018-06-25 16:07:23 +02001184static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001185{
1186 int hash;
1187 struct hlist_node *walk;
1188 struct hlist_node *tmp;
1189 struct l2tp_session *session;
1190
1191 BUG_ON(tunnel == NULL);
1192
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001193 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1194 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001195
1196 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001197 tunnel->acpt_newsess = false;
James Chapmanfd558d12010-04-02 06:18:33 +00001198 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1199again:
1200 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1201 session = hlist_entry(walk, struct l2tp_session, hlist);
1202
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001203 l2tp_info(session, L2TP_MSG_CONTROL,
1204 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001205
1206 hlist_del_init(&session->hlist);
1207
Guillaume Naultb228a942017-09-22 15:39:24 +02001208 if (test_and_set_bit(0, &session->dead))
1209 goto again;
1210
James Chapmanfd558d12010-04-02 06:18:33 +00001211 write_unlock_bh(&tunnel->hlist_lock);
1212
Tom Parkinf6e16b22013-03-19 06:11:23 +00001213 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001214 l2tp_session_queue_purge(session);
1215
James Chapmanfd558d12010-04-02 06:18:33 +00001216 if (session->session_close != NULL)
1217 (*session->session_close)(session);
1218
Tom Parkin9980d002013-03-19 06:11:13 +00001219 l2tp_session_dec_refcount(session);
1220
James Chapmanfd558d12010-04-02 06:18:33 +00001221 write_lock_bh(&tunnel->hlist_lock);
1222
1223 /* Now restart from the beginning of this hash
1224 * chain. We always remove a session from the
1225 * list so we are guaranteed to make forward
1226 * progress.
1227 */
1228 goto again;
1229 }
1230 }
1231 write_unlock_bh(&tunnel->hlist_lock);
1232}
James Chapmanfd558d12010-04-02 06:18:33 +00001233
Tom Parkin9980d002013-03-19 06:11:13 +00001234/* Tunnel socket destroy hook for UDP encapsulation */
1235static void l2tp_udp_encap_destroy(struct sock *sk)
1236{
James Chapmand00fa9a2018-02-23 17:45:45 +00001237 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1238
1239 if (tunnel)
1240 l2tp_tunnel_delete(tunnel);
Tom Parkin9980d002013-03-19 06:11:13 +00001241}
1242
Tom Parkinf8ccac02013-01-31 23:43:00 +00001243/* Workqueue tunnel deletion function */
1244static void l2tp_tunnel_del_work(struct work_struct *work)
1245{
James Chapmand00fa9a2018-02-23 17:45:45 +00001246 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1247 del_work);
1248 struct sock *sk = tunnel->sock;
1249 struct socket *sock = sk->sk_socket;
James Chapman28f5bfb2018-02-23 17:45:47 +00001250 struct l2tp_net *pn;
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001251
1252 l2tp_tunnel_closeall(tunnel);
1253
James Chapman76a6abd2018-02-23 17:45:43 +00001254 /* If the tunnel socket was created within the kernel, use
Tom Parkin02d13ed2013-03-19 06:11:18 +00001255 * the sk API to release it here.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001256 */
James Chapman76a6abd2018-02-23 17:45:43 +00001257 if (tunnel->fd < 0) {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001258 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001259 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001260 sock_release(sock);
1261 }
Tom Parkin167eb172013-01-31 23:43:03 +00001262 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001263
James Chapman28f5bfb2018-02-23 17:45:47 +00001264 /* Remove the tunnel struct from the tunnel list */
1265 pn = l2tp_pernet(tunnel->l2tp_net);
1266 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1267 list_del_rcu(&tunnel->list);
1268 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1269
James Chapmand00fa9a2018-02-23 17:45:45 +00001270 /* drop initial ref */
1271 l2tp_tunnel_dec_refcount(tunnel);
1272
1273 /* drop workqueue ref */
Alexander Couzens06a15f52015-09-28 11:32:42 +02001274 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001275}
James Chapmanfd558d12010-04-02 06:18:33 +00001276
James Chapman789a4a22010-04-02 06:19:40 +00001277/* Create a socket for the tunnel, if one isn't set up by
1278 * userspace. This is used for static tunnels where there is no
1279 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001280 *
1281 * Since we don't want these sockets to keep a namespace alive by
1282 * themselves, we drop the socket's namespace refcount after creation.
1283 * These sockets are freed when the namespace exits using the pernet
1284 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001285 */
Tom Parkin167eb172013-01-31 23:43:03 +00001286static int l2tp_tunnel_sock_create(struct net *net,
1287 u32 tunnel_id,
1288 u32 peer_tunnel_id,
1289 struct l2tp_tunnel_cfg *cfg,
1290 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001291{
1292 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001293 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001294 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001295
1296 switch (cfg->encap) {
1297 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001298 memset(&udp_conf, 0, sizeof(udp_conf));
1299
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001300#if IS_ENABLED(CONFIG_IPV6)
1301 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001302 udp_conf.family = AF_INET6;
1303 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1304 sizeof(udp_conf.local_ip6));
1305 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1306 sizeof(udp_conf.peer_ip6));
1307 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001308 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001309 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001310 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001311 } else
1312#endif
1313 {
Tom Herbert85644b42014-07-13 19:49:48 -07001314 udp_conf.family = AF_INET;
1315 udp_conf.local_ip = cfg->local_ip;
1316 udp_conf.peer_ip = cfg->peer_ip;
1317 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001318 }
James Chapman789a4a22010-04-02 06:19:40 +00001319
Tom Herbert85644b42014-07-13 19:49:48 -07001320 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1321 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1322
1323 err = udp_sock_create(net, &udp_conf, &sock);
1324 if (err < 0)
1325 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001326
1327 break;
1328
1329 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001330#if IS_ENABLED(CONFIG_IPV6)
1331 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001332 struct sockaddr_l2tpip6 ip6_addr = {0};
1333
Eric W. Biederman26abe142015-05-08 21:10:31 -05001334 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001335 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001336 if (err < 0)
1337 goto out;
1338
James Chapman5dac94e2012-04-29 21:48:55 +00001339 ip6_addr.l2tp_family = AF_INET6;
1340 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1341 sizeof(ip6_addr.l2tp_addr));
1342 ip6_addr.l2tp_conn_id = tunnel_id;
1343 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1344 sizeof(ip6_addr));
1345 if (err < 0)
1346 goto out;
1347
1348 ip6_addr.l2tp_family = AF_INET6;
1349 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1350 sizeof(ip6_addr.l2tp_addr));
1351 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1352 err = kernel_connect(sock,
1353 (struct sockaddr *) &ip6_addr,
1354 sizeof(ip6_addr), 0);
1355 if (err < 0)
1356 goto out;
1357 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001358#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001359 {
Tom Herbert85644b42014-07-13 19:49:48 -07001360 struct sockaddr_l2tpip ip_addr = {0};
1361
Eric W. Biederman26abe142015-05-08 21:10:31 -05001362 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001363 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001364 if (err < 0)
1365 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001366
James Chapman5dac94e2012-04-29 21:48:55 +00001367 ip_addr.l2tp_family = AF_INET;
1368 ip_addr.l2tp_addr = cfg->local_ip;
1369 ip_addr.l2tp_conn_id = tunnel_id;
1370 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1371 sizeof(ip_addr));
1372 if (err < 0)
1373 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001374
James Chapman5dac94e2012-04-29 21:48:55 +00001375 ip_addr.l2tp_family = AF_INET;
1376 ip_addr.l2tp_addr = cfg->peer_ip;
1377 ip_addr.l2tp_conn_id = peer_tunnel_id;
1378 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1379 sizeof(ip_addr), 0);
1380 if (err < 0)
1381 goto out;
1382 }
James Chapman789a4a22010-04-02 06:19:40 +00001383 break;
1384
1385 default:
1386 goto out;
1387 }
1388
1389out:
Tom Parkin167eb172013-01-31 23:43:03 +00001390 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001391 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001392 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001393 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001394 *sockp = NULL;
1395 }
1396
1397 return err;
1398}
1399
Eric Dumazet37159ef2012-09-04 07:18:57 +00001400static struct lock_class_key l2tp_socket_class;
1401
James Chapmanfd558d12010-04-02 06:18:33 +00001402int 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)
1403{
1404 struct l2tp_tunnel *tunnel = NULL;
1405 int err;
James Chapman0d767512010-04-02 06:19:00 +00001406 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001407
James Chapman0d767512010-04-02 06:19:00 +00001408 if (cfg != NULL)
1409 encap = cfg->encap;
1410
James Chapmanfd558d12010-04-02 06:18:33 +00001411 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1412 if (tunnel == NULL) {
1413 err = -ENOMEM;
1414 goto err;
1415 }
1416
1417 tunnel->version = version;
1418 tunnel->tunnel_id = tunnel_id;
1419 tunnel->peer_tunnel_id = peer_tunnel_id;
1420 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1421
1422 tunnel->magic = L2TP_TUNNEL_MAGIC;
1423 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1424 rwlock_init(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001425 tunnel->acpt_newsess = true;
James Chapmanfd558d12010-04-02 06:18:33 +00001426
James Chapman0d767512010-04-02 06:19:00 +00001427 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001428 tunnel->debug = cfg->debug;
1429
James Chapman0d767512010-04-02 06:19:00 +00001430 tunnel->encap = encap;
James Chapmanfd558d12010-04-02 06:18:33 +00001431
James Chapmand00fa9a2018-02-23 17:45:45 +00001432 refcount_set(&tunnel->ref_count, 1);
James Chapmand00fa9a2018-02-23 17:45:45 +00001433 tunnel->fd = fd;
1434
Tom Parkinf8ccac02013-01-31 23:43:00 +00001435 /* Init delete workqueue struct */
1436 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1437
James Chapmanfd558d12010-04-02 06:18:33 +00001438 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001439
1440 err = 0;
1441err:
1442 if (tunnelp)
1443 *tunnelp = tunnel;
1444
James Chapmanfd558d12010-04-02 06:18:33 +00001445 return err;
1446}
1447EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1448
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001449static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1450 enum l2tp_encap_type encap)
1451{
1452 if (!net_eq(sock_net(sk), net))
1453 return -EINVAL;
1454
1455 if (sk->sk_type != SOCK_DGRAM)
1456 return -EPROTONOSUPPORT;
1457
1458 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1459 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1460 return -EPROTONOSUPPORT;
1461
1462 if (sk->sk_user_data)
1463 return -EBUSY;
1464
1465 return 0;
1466}
1467
1468int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1469 struct l2tp_tunnel_cfg *cfg)
1470{
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001471 struct l2tp_tunnel *tunnel_walk;
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001472 struct l2tp_net *pn;
1473 struct socket *sock;
1474 struct sock *sk;
1475 int ret;
1476
1477 if (tunnel->fd < 0) {
1478 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1479 tunnel->peer_tunnel_id, cfg,
1480 &sock);
1481 if (ret < 0)
1482 goto err;
1483 } else {
1484 sock = sockfd_lookup(tunnel->fd, &ret);
1485 if (!sock)
1486 goto err;
1487
1488 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1489 if (ret < 0)
1490 goto err_sock;
1491 }
1492
1493 sk = sock->sk;
1494
1495 sock_hold(sk);
1496 tunnel->sock = sk;
1497 tunnel->l2tp_net = net;
1498
1499 pn = l2tp_pernet(net);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001500
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001501 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001502 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1503 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1504 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1505
1506 ret = -EEXIST;
1507 goto err_sock;
1508 }
1509 }
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001510 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1511 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1512
1513 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1514 struct udp_tunnel_sock_cfg udp_cfg = {
1515 .sk_user_data = tunnel,
1516 .encap_type = UDP_ENCAP_L2TPINUDP,
1517 .encap_rcv = l2tp_udp_encap_recv,
1518 .encap_destroy = l2tp_udp_encap_destroy,
1519 };
1520
1521 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1522 } else {
1523 sk->sk_user_data = tunnel;
1524 }
1525
1526 tunnel->old_sk_destruct = sk->sk_destruct;
1527 sk->sk_destruct = &l2tp_tunnel_destruct;
1528 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1529 "l2tp_sock");
1530 sk->sk_allocation = GFP_ATOMIC;
1531
1532 if (tunnel->fd >= 0)
1533 sockfd_put(sock);
1534
1535 return 0;
1536
1537err_sock:
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001538 if (tunnel->fd < 0)
1539 sock_release(sock);
1540 else
1541 sockfd_put(sock);
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001542err:
1543 return ret;
1544}
1545EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1546
James Chapman309795f2010-04-02 06:19:10 +00001547/* This function is used by the netlink TUNNEL_DELETE command.
1548 */
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001549void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001550{
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001551 if (!test_and_set_bit(0, &tunnel->dead)) {
1552 l2tp_tunnel_inc_refcount(tunnel);
1553 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001554 }
James Chapman309795f2010-04-02 06:19:10 +00001555}
1556EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1557
James Chapmanfd558d12010-04-02 06:18:33 +00001558/* Really kill the session.
1559 */
1560void l2tp_session_free(struct l2tp_session *session)
1561{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001562 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001563
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001564 BUG_ON(refcount_read(&session->ref_count) != 0);
James Chapmanfd558d12010-04-02 06:18:33 +00001565
Tom Parkinf6e16b22013-03-19 06:11:23 +00001566 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001567 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001568 l2tp_tunnel_dec_refcount(tunnel);
1569 }
1570
1571 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001572}
1573EXPORT_SYMBOL_GPL(l2tp_session_free);
1574
Tom Parkinf6e16b22013-03-19 06:11:23 +00001575/* Remove an l2tp session from l2tp_core's hash lists.
1576 * Provides a tidyup interface for pseudowire code which can't just route all
1577 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1578 * callback.
1579 */
1580void __l2tp_session_unhash(struct l2tp_session *session)
1581{
1582 struct l2tp_tunnel *tunnel = session->tunnel;
1583
1584 /* Remove the session from core hashes */
1585 if (tunnel) {
1586 /* Remove from the per-tunnel hash */
1587 write_lock_bh(&tunnel->hlist_lock);
1588 hlist_del_init(&session->hlist);
1589 write_unlock_bh(&tunnel->hlist_lock);
1590
1591 /* For L2TPv3 we have a per-net hash: remove from there, too */
1592 if (tunnel->version != L2TP_HDR_VER_2) {
1593 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1594 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1595 hlist_del_init_rcu(&session->global_hlist);
1596 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1597 synchronize_rcu();
1598 }
1599 }
1600}
1601EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1602
James Chapman309795f2010-04-02 06:19:10 +00001603/* This function is used by the netlink SESSION_DELETE command and by
1604 pseudowire modules.
1605 */
1606int l2tp_session_delete(struct l2tp_session *session)
1607{
Guillaume Naultb228a942017-09-22 15:39:24 +02001608 if (test_and_set_bit(0, &session->dead))
1609 return 0;
1610
Tom Parkinf6e16b22013-03-19 06:11:23 +00001611 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001612 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001613 if (session->session_close != NULL)
1614 (*session->session_close)(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001615
James Chapman309795f2010-04-02 06:19:10 +00001616 l2tp_session_dec_refcount(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001617
James Chapman309795f2010-04-02 06:19:10 +00001618 return 0;
1619}
1620EXPORT_SYMBOL_GPL(l2tp_session_delete);
1621
James Chapmanf7faffa2010-04-02 06:18:49 +00001622/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001623 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001624 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001625void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001626{
1627 if (version == L2TP_HDR_VER_2) {
1628 session->hdr_len = 6;
1629 if (session->send_seq)
1630 session->hdr_len += 4;
1631 } else {
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001632 session->hdr_len = 4 + session->cookie_len;
1633 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001634 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1635 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001636 }
1637
1638}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001639EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001640
James Chapmanfd558d12010-04-02 06:18:33 +00001641struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1642{
1643 struct l2tp_session *session;
1644
1645 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1646 if (session != NULL) {
1647 session->magic = L2TP_SESSION_MAGIC;
1648 session->tunnel = tunnel;
1649
1650 session->session_id = session_id;
1651 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001652 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001653 if (tunnel->version == L2TP_HDR_VER_2)
1654 session->nr_max = 0xffff;
1655 else
1656 session->nr_max = 0xffffff;
1657 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001658 session->nr_oos_count_max = 4;
1659
1660 /* Use NR of first received packet */
1661 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001662
1663 sprintf(&session->name[0], "sess %u/%u",
1664 tunnel->tunnel_id, session->session_id);
1665
1666 skb_queue_head_init(&session->reorder_q);
1667
1668 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001669 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001670
1671 /* Inherit debug options from tunnel */
1672 session->debug = tunnel->debug;
1673
1674 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001675 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001676 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001677 session->mtu = cfg->mtu;
James Chapmanfd558d12010-04-02 06:18:33 +00001678 session->send_seq = cfg->send_seq;
1679 session->recv_seq = cfg->recv_seq;
1680 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001681 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001682 session->l2specific_type = cfg->l2specific_type;
James Chapmanf7faffa2010-04-02 06:18:49 +00001683 session->cookie_len = cfg->cookie_len;
1684 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1685 session->peer_cookie_len = cfg->peer_cookie_len;
1686 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001687 }
1688
James Chapmanf7faffa2010-04-02 06:18:49 +00001689 if (tunnel->version == L2TP_HDR_VER_2)
1690 session->build_header = l2tp_build_l2tpv2_header;
1691 else
1692 session->build_header = l2tp_build_l2tpv3_header;
1693
1694 l2tp_session_set_header_len(session, tunnel->version);
1695
Guillaume Nault9ee369a2017-08-25 16:22:17 +02001696 refcount_set(&session->ref_count, 1);
1697
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001698 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001699 }
1700
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001701 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001702}
1703EXPORT_SYMBOL_GPL(l2tp_session_create);
1704
1705/*****************************************************************************
1706 * Init and cleanup
1707 *****************************************************************************/
1708
1709static __net_init int l2tp_init_net(struct net *net)
1710{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001711 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001712 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001713
James Chapmanfd558d12010-04-02 06:18:33 +00001714 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001715 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001716
James Chapmanf7faffa2010-04-02 06:18:49 +00001717 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1718 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1719
James Chapmane02d4942010-04-02 06:19:16 +00001720 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001721
James Chapmanfd558d12010-04-02 06:18:33 +00001722 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001723}
1724
Tom Parkin167eb172013-01-31 23:43:03 +00001725static __net_exit void l2tp_exit_net(struct net *net)
1726{
1727 struct l2tp_net *pn = l2tp_pernet(net);
1728 struct l2tp_tunnel *tunnel = NULL;
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001729 int hash;
Tom Parkin167eb172013-01-31 23:43:03 +00001730
1731 rcu_read_lock_bh();
1732 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slaby4dc12ff2017-10-25 15:57:55 +02001733 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001734 }
1735 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001736
1737 flush_workqueue(l2tp_wq);
1738 rcu_barrier();
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001739
1740 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1741 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
Tom Parkin167eb172013-01-31 23:43:03 +00001742}
1743
James Chapmanfd558d12010-04-02 06:18:33 +00001744static struct pernet_operations l2tp_net_ops = {
1745 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001746 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001747 .id = &l2tp_net_id,
1748 .size = sizeof(struct l2tp_net),
1749};
1750
1751static int __init l2tp_init(void)
1752{
1753 int rc = 0;
1754
1755 rc = register_pernet_device(&l2tp_net_ops);
1756 if (rc)
1757 goto out;
1758
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001759 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001760 if (!l2tp_wq) {
1761 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001762 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001763 rc = -ENOMEM;
1764 goto out;
1765 }
1766
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001767 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001768
1769out:
1770 return rc;
1771}
1772
1773static void __exit l2tp_exit(void)
1774{
1775 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001776 if (l2tp_wq) {
1777 destroy_workqueue(l2tp_wq);
1778 l2tp_wq = NULL;
1779 }
James Chapmanfd558d12010-04-02 06:18:33 +00001780}
1781
1782module_init(l2tp_init);
1783module_exit(l2tp_exit);
1784
1785MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1786MODULE_DESCRIPTION("L2TP core");
1787MODULE_LICENSE("GPL");
1788MODULE_VERSION(L2TP_DRV_VERSION);