blob: c8c4183f0f379dc74a66542e124456047e7db32b [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);
325 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
326 session->session_id);
327
328 spin_lock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200329
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200330 hlist_for_each_entry(session_walk, g_head, global_hlist)
331 if (session_walk->session_id == session->session_id) {
332 err = -EEXIST;
333 goto err_tlock_pnlock;
334 }
335
336 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200337 hlist_add_head_rcu(&session->global_hlist, g_head);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200338
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200339 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200340 } else {
341 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200342 }
343
344 hlist_add_head(&session->hlist, head);
345 write_unlock_bh(&tunnel->hlist_lock);
346
347 return 0;
348
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200349err_tlock_pnlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200350 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200351err_tlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200352 write_unlock_bh(&tunnel->hlist_lock);
353
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200354 return err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200355}
Guillaume Nault3953ae72017-10-27 16:51:50 +0200356EXPORT_SYMBOL_GPL(l2tp_session_register);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200357
Guillaume Nault2f858b92017-04-12 10:05:30 +0200358struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000359{
360 struct l2tp_net *pn = l2tp_pernet(net);
361 struct l2tp_tunnel *tunnel;
362 int count = 0;
363
James Chapmane02d4942010-04-02 06:19:16 +0000364 rcu_read_lock_bh();
365 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000366 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000367 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000368 return tunnel;
369 }
370 }
371
James Chapmane02d4942010-04-02 06:19:16 +0000372 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000373
374 return NULL;
375}
376EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
377
378/*****************************************************************************
379 * Receive data handling
380 *****************************************************************************/
381
382/* Queue a skb in order. We come here only if the skb has an L2TP sequence
383 * number.
384 */
385static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
386{
387 struct sk_buff *skbp;
388 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000389 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000390
391 spin_lock_bh(&session->reorder_q.lock);
392 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
393 if (L2TP_SKB_CB(skbp)->ns > ns) {
394 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000395 l2tp_dbg(session, L2TP_MSG_SEQ,
396 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
397 session->name, ns, L2TP_SKB_CB(skbp)->ns,
398 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000399 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000400 goto out;
401 }
402 }
403
404 __skb_queue_tail(&session->reorder_q, skb);
405
406out:
407 spin_unlock_bh(&session->reorder_q.lock);
408}
409
410/* Dequeue a single skb.
411 */
412static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
413{
414 struct l2tp_tunnel *tunnel = session->tunnel;
415 int length = L2TP_SKB_CB(skb)->length;
416
417 /* We're about to requeue the skb, so return resources
418 * to its current owner (a socket receive buffer).
419 */
420 skb_orphan(skb);
421
Tom Parkin7b7c0712013-03-19 06:11:22 +0000422 atomic_long_inc(&tunnel->stats.rx_packets);
423 atomic_long_add(length, &tunnel->stats.rx_bytes);
424 atomic_long_inc(&session->stats.rx_packets);
425 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000426
427 if (L2TP_SKB_CB(skb)->has_seq) {
428 /* Bump our Nr */
429 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100430 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000431
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000432 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
433 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000434 }
435
436 /* call private receive handler */
437 if (session->recv_skb != NULL)
438 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
439 else
440 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000441}
442
443/* Dequeue skbs from the session's reorder_q, subject to packet order.
444 * Skbs that have been in the queue for too long are simply discarded.
445 */
446static void l2tp_recv_dequeue(struct l2tp_session *session)
447{
448 struct sk_buff *skb;
449 struct sk_buff *tmp;
450
451 /* If the pkt at the head of the queue has the nr that we
452 * expect to send up next, dequeue it and any other
453 * in-sequence packets behind it.
454 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000455start:
James Chapmanfd558d12010-04-02 06:18:33 +0000456 spin_lock_bh(&session->reorder_q.lock);
457 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
458 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000459 atomic_long_inc(&session->stats.rx_seq_discards);
460 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000461 l2tp_dbg(session, L2TP_MSG_SEQ,
462 "%s: oos pkt %u len %d discarded (too old), 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 Chapman38d40b32012-05-09 23:43:08 +0000466 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000467 __skb_unlink(skb, &session->reorder_q);
468 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000469 continue;
470 }
471
472 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000473 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000474 l2tp_dbg(session, L2TP_MSG_SEQ,
475 "%s: advancing nr to next pkt: %u -> %u",
476 session->name, session->nr,
477 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000478 session->reorder_skip = 0;
479 session->nr = L2TP_SKB_CB(skb)->ns;
480 }
James Chapmanfd558d12010-04-02 06:18:33 +0000481 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000482 l2tp_dbg(session, L2TP_MSG_SEQ,
483 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
484 session->name, L2TP_SKB_CB(skb)->ns,
485 L2TP_SKB_CB(skb)->length, session->nr,
486 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000487 goto out;
488 }
489 }
490 __skb_unlink(skb, &session->reorder_q);
491
492 /* Process the skb. We release the queue lock while we
493 * do so to let other contexts process the queue.
494 */
495 spin_unlock_bh(&session->reorder_q.lock);
496 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000497 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000498 }
499
500out:
501 spin_unlock_bh(&session->reorder_q.lock);
502}
503
James Chapman8a1631d2013-07-02 20:28:59 +0100504static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
505{
506 u32 nws;
507
508 if (nr >= session->nr)
509 nws = nr - session->nr;
510 else
511 nws = (session->nr_max + 1) - (session->nr - nr);
512
513 return nws < session->nr_window_size;
514}
515
James Chapmanb6dc01a2013-07-02 20:28:58 +0100516/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
517 * acceptable, else non-zero.
518 */
519static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
520{
James Chapman8a1631d2013-07-02 20:28:59 +0100521 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
522 /* Packet sequence number is outside allowed window.
523 * Discard it.
524 */
525 l2tp_dbg(session, L2TP_MSG_SEQ,
526 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
527 session->name, L2TP_SKB_CB(skb)->ns,
528 L2TP_SKB_CB(skb)->length, session->nr);
529 goto discard;
530 }
531
James Chapmanb6dc01a2013-07-02 20:28:58 +0100532 if (session->reorder_timeout != 0) {
533 /* Packet reordering enabled. Add skb to session's
534 * reorder queue, in order of ns.
535 */
536 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100537 goto out;
538 }
539
540 /* Packet reordering disabled. Discard out-of-sequence packets, while
541 * tracking the number if in-sequence packets after the first OOS packet
542 * is seen. After nr_oos_count_max in-sequence packets, reset the
543 * sequence number to re-enable packet reception.
544 */
545 if (L2TP_SKB_CB(skb)->ns == session->nr) {
546 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100547 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100548 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
549 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
550
551 if (nr_oos == nr_next)
552 session->nr_oos_count++;
553 else
554 session->nr_oos_count = 0;
555
556 session->nr_oos = nr_oos;
557 if (session->nr_oos_count > session->nr_oos_count_max) {
558 session->reorder_skip = 1;
559 l2tp_dbg(session, L2TP_MSG_SEQ,
560 "%s: %d oos packets received. Resetting sequence numbers\n",
561 session->name, session->nr_oos_count);
562 }
563 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100564 atomic_long_inc(&session->stats.rx_seq_discards);
565 l2tp_dbg(session, L2TP_MSG_SEQ,
566 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
567 session->name, L2TP_SKB_CB(skb)->ns,
568 L2TP_SKB_CB(skb)->length, session->nr,
569 skb_queue_len(&session->reorder_q));
570 goto discard;
571 }
572 skb_queue_tail(&session->reorder_q, skb);
573 }
574
James Chapmana0dbd822013-07-02 20:29:00 +0100575out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100576 return 0;
577
578discard:
579 return 1;
580}
581
James Chapmanf7faffa2010-04-02 06:18:49 +0000582/* Do receive processing of L2TP data frames. We handle both L2TPv2
583 * and L2TPv3 data frames here.
584 *
585 * L2TPv2 Data Message Header
586 *
587 * 0 1 2 3
588 * 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
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
591 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 * | Tunnel ID | Session ID |
593 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594 * | Ns (opt) | Nr (opt) |
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | Offset Size (opt) | Offset pad... (opt)
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 *
599 * Data frames are marked by T=0. All other fields are the same as
600 * those in L2TP control frames.
601 *
602 * L2TPv3 Data Message Header
603 *
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 * | L2TP Session Header |
606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607 * | L2-Specific Sublayer |
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * | Tunnel Payload ...
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 *
612 * L2TPv3 Session Header Over IP
613 *
614 * 0 1 2 3
615 * 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
616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
617 * | Session ID |
618 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
619 * | Cookie (optional, maximum 64 bits)...
620 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
621 * |
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 *
624 * L2TPv3 L2-Specific Sublayer Format
625 *
626 * 0 1 2 3
627 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
628 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
629 * |x|S|x|x|x|x|x|x| Sequence Number |
630 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 *
Guillaume Nault23fe8462018-01-05 19:47:14 +0100632 * Cookie value and sublayer format are negotiated with the peer when
633 * the session is set up. Unlike L2TPv2, we do not need to parse the
634 * packet header to determine if optional fields are present.
James Chapmanf7faffa2010-04-02 06:18:49 +0000635 *
636 * Caller must already have parsed the frame and determined that it is
637 * a data (not control) frame before coming here. Fields up to the
638 * session-id have already been parsed and ptr points to the data
639 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000640 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000641void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
642 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
643 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000644{
James Chapmanf7faffa2010-04-02 06:18:49 +0000645 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000646 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000647 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000648
James Chapmanf7faffa2010-04-02 06:18:49 +0000649 /* Parse and check optional cookie */
650 if (session->peer_cookie_len > 0) {
651 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000652 l2tp_info(tunnel, L2TP_MSG_DATA,
653 "%s: cookie mismatch (%u/%u). Discarding.\n",
654 tunnel->name, tunnel->tunnel_id,
655 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000656 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000657 goto discard;
658 }
659 ptr += session->peer_cookie_len;
660 }
661
James Chapmanfd558d12010-04-02 06:18:33 +0000662 /* Handle the optional sequence numbers. Sequence numbers are
663 * in different places for L2TPv2 and L2TPv3.
664 *
665 * If we are the LAC, enable/disable sequence numbers under
666 * the control of the LNS. If no sequence numbers present but
667 * we were expecting them, discard frame.
668 */
669 ns = nr = 0;
670 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000671 if (tunnel->version == L2TP_HDR_VER_2) {
672 if (hdrflags & L2TP_HDRFLAG_S) {
673 ns = ntohs(*(__be16 *) ptr);
674 ptr += 2;
675 nr = ntohs(*(__be16 *) ptr);
676 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000677
James Chapmanf7faffa2010-04-02 06:18:49 +0000678 /* Store L2TP info in the skb */
679 L2TP_SKB_CB(skb)->ns = ns;
680 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000681
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000682 l2tp_dbg(session, L2TP_MSG_SEQ,
683 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
684 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000685 }
686 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
687 u32 l2h = ntohl(*(__be32 *) ptr);
688
689 if (l2h & 0x40000000) {
690 ns = l2h & 0x00ffffff;
691
692 /* Store L2TP info in the skb */
693 L2TP_SKB_CB(skb)->ns = ns;
694 L2TP_SKB_CB(skb)->has_seq = 1;
695
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000696 l2tp_dbg(session, L2TP_MSG_SEQ,
697 "%s: recv data ns=%u, session nr=%u\n",
698 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000699 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100700 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000701 }
702
703 if (L2TP_SKB_CB(skb)->has_seq) {
704 /* Received a packet with sequence numbers. If we're the LNS,
705 * check if we sre sending sequence numbers and if not,
706 * configure it so.
707 */
708 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000709 l2tp_info(session, L2TP_MSG_SEQ,
710 "%s: requested to enable seq numbers by LNS\n",
711 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000712 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000713 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000714 }
715 } else {
716 /* No sequence numbers.
717 * If user has configured mandatory sequence numbers, discard.
718 */
719 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000720 l2tp_warn(session, L2TP_MSG_SEQ,
721 "%s: recv data has no seq numbers when required. Discarding.\n",
722 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000723 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000724 goto discard;
725 }
726
727 /* If we're the LAC and we're sending sequence numbers, the
728 * LNS has requested that we no longer send sequence numbers.
729 * If we're the LNS and we're sending sequence numbers, the
730 * LAC is broken. Discard the frame.
731 */
732 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000733 l2tp_info(session, L2TP_MSG_SEQ,
734 "%s: requested to disable seq numbers by LNS\n",
735 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000736 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000737 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000738 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000739 l2tp_warn(session, L2TP_MSG_SEQ,
740 "%s: recv data has no seq numbers when required. Discarding.\n",
741 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000742 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000743 goto discard;
744 }
745 }
746
James Chapman900631e2018-01-03 22:48:06 +0000747 /* Session data offset is defined only for L2TPv2 and is
748 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000749 */
750 if (tunnel->version == L2TP_HDR_VER_2) {
751 /* If offset bit set, skip it. */
752 if (hdrflags & L2TP_HDRFLAG_O) {
753 offset = ntohs(*(__be16 *)ptr);
754 ptr += 2 + offset;
755 }
James Chapman900631e2018-01-03 22:48:06 +0000756 }
James Chapmanfd558d12010-04-02 06:18:33 +0000757
758 offset = ptr - optr;
759 if (!pskb_may_pull(skb, offset))
760 goto discard;
761
762 __skb_pull(skb, offset);
763
764 /* If caller wants to process the payload before we queue the
765 * packet, do so now.
766 */
767 if (payload_hook)
768 if ((*payload_hook)(skb))
769 goto discard;
770
771 /* Prepare skb for adding to the session's reorder_q. Hold
772 * packets for max reorder_timeout or 1 second if not
773 * reordering.
774 */
775 L2TP_SKB_CB(skb)->length = length;
776 L2TP_SKB_CB(skb)->expires = jiffies +
777 (session->reorder_timeout ? session->reorder_timeout : HZ);
778
779 /* Add packet to the session's receive queue. Reordering is done here, if
780 * enabled. Saved L2TP protocol info is stored in skb->sb[].
781 */
782 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100783 if (l2tp_recv_data_seq(session, skb))
784 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000785 } else {
786 /* No sequence numbers. Add the skb to the tail of the
787 * reorder queue. This ensures that it will be
788 * delivered after all previous sequenced skbs.
789 */
790 skb_queue_tail(&session->reorder_q, skb);
791 }
792
793 /* Try to dequeue as many skbs from reorder_q as we can. */
794 l2tp_recv_dequeue(session);
795
James Chapmanf7faffa2010-04-02 06:18:49 +0000796 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000797
798discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000799 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000800 kfree_skb(skb);
James Chapmanf7faffa2010-04-02 06:18:49 +0000801}
802EXPORT_SYMBOL(l2tp_recv_common);
803
Tom Parkin48f72f92013-03-19 06:11:19 +0000804/* Drop skbs from the session's reorder_q
805 */
806int l2tp_session_queue_purge(struct l2tp_session *session)
807{
808 struct sk_buff *skb = NULL;
809 BUG_ON(!session);
810 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
811 while ((skb = skb_dequeue(&session->reorder_q))) {
812 atomic_long_inc(&session->stats.rx_errors);
813 kfree_skb(skb);
Tom Parkin48f72f92013-03-19 06:11:19 +0000814 }
815 return 0;
816}
817EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
818
James Chapmanf7faffa2010-04-02 06:18:49 +0000819/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
820 * here. The skb is not on a list when we get here.
821 * Returns 0 if the packet was a data packet and was successfully passed on.
822 * Returns 1 if the packet was not a good data packet and could not be
823 * forwarded. All such packets are passed up to userspace to deal with.
824 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000825static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
826 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000827{
828 struct l2tp_session *session = NULL;
829 unsigned char *ptr, *optr;
830 u16 hdrflags;
831 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000832 u16 version;
833 int length;
834
Tom Herbert58d60852014-05-07 16:52:48 -0700835 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000836
837 /* UDP always verifies the packet length. */
838 __skb_pull(skb, sizeof(struct udphdr));
839
840 /* Short packet? */
841 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000842 l2tp_info(tunnel, L2TP_MSG_DATA,
843 "%s: recv short packet (len=%d)\n",
844 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000845 goto error;
846 }
847
James Chapmanf7faffa2010-04-02 06:18:49 +0000848 /* Trace packet contents, if enabled */
849 if (tunnel->debug & L2TP_MSG_DATA) {
850 length = min(32u, skb->len);
851 if (!pskb_may_pull(skb, length))
852 goto error;
853
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000854 pr_debug("%s: recv\n", tunnel->name);
855 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000856 }
857
Eric Dumazete50e7052011-11-08 13:59:44 -0500858 /* Point to L2TP header */
859 optr = ptr = skb->data;
860
James Chapmanf7faffa2010-04-02 06:18:49 +0000861 /* Get L2TP header flags */
862 hdrflags = ntohs(*(__be16 *) ptr);
863
864 /* Check protocol version */
865 version = hdrflags & L2TP_HDR_VER_MASK;
866 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000867 l2tp_info(tunnel, L2TP_MSG_DATA,
868 "%s: recv protocol version mismatch: got %d expected %d\n",
869 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000870 goto error;
871 }
872
873 /* Get length of L2TP packet */
874 length = skb->len;
875
876 /* If type is control packet, it is handled by userspace. */
877 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000878 l2tp_dbg(tunnel, L2TP_MSG_DATA,
879 "%s: recv control packet, len=%d\n",
880 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000881 goto error;
882 }
883
884 /* Skip flags */
885 ptr += 2;
886
887 if (tunnel->version == L2TP_HDR_VER_2) {
888 /* If length is present, skip it */
889 if (hdrflags & L2TP_HDRFLAG_L)
890 ptr += 2;
891
892 /* Extract tunnel and session ID */
893 tunnel_id = ntohs(*(__be16 *) ptr);
894 ptr += 2;
895 session_id = ntohs(*(__be16 *) ptr);
896 ptr += 2;
897 } else {
898 ptr += 2; /* skip reserved bits */
899 tunnel_id = tunnel->tunnel_id;
900 session_id = ntohl(*(__be32 *) ptr);
901 ptr += 4;
902 }
903
904 /* Find the session context */
Guillaume Naulta4346212017-10-31 17:36:42 +0100905 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000906 if (!session || !session->recv_skb) {
Guillaume Naulta4346212017-10-31 17:36:42 +0100907 if (session)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200908 l2tp_session_dec_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200909
James Chapmanf7faffa2010-04-02 06:18:49 +0000910 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000911 l2tp_info(tunnel, L2TP_MSG_DATA,
912 "%s: no session found (%u/%u). Passing up.\n",
913 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000914 goto error;
915 }
916
917 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200918 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000919
920 return 0;
921
James Chapmanfd558d12010-04-02 06:18:33 +0000922error:
923 /* Put UDP header back */
924 __skb_push(skb, sizeof(struct udphdr));
925
926 return 1;
927}
James Chapmanfd558d12010-04-02 06:18:33 +0000928
929/* UDP encapsulation receive handler. See net/ipv4/udp.c.
930 * Return codes:
931 * 0 : success.
932 * <0: error
933 * >0: skb should be passed up to userspace as UDP.
934 */
935int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
936{
937 struct l2tp_tunnel *tunnel;
938
James Chapmand00fa9a2018-02-23 17:45:45 +0000939 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000940 if (tunnel == NULL)
941 goto pass_up;
942
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000943 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
944 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000945
946 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
James Chapmand00fa9a2018-02-23 17:45:45 +0000947 goto pass_up;
James Chapmanfd558d12010-04-02 06:18:33 +0000948
James Chapmanfd558d12010-04-02 06:18:33 +0000949 return 0;
950
James Chapmanfd558d12010-04-02 06:18:33 +0000951pass_up:
952 return 1;
953}
954EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
955
956/************************************************************************
957 * Transmit handling
958 ***********************************************************************/
959
960/* Build an L2TP header for the session into the buffer provided.
961 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000962static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000963{
James Chapmanf7faffa2010-04-02 06:18:49 +0000964 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000965 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000966 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000967 u16 flags = L2TP_HDR_VER_2;
968 u32 tunnel_id = tunnel->peer_tunnel_id;
969 u32 session_id = session->peer_session_id;
970
971 if (session->send_seq)
972 flags |= L2TP_HDRFLAG_S;
973
974 /* Setup L2TP header. */
975 *bufp++ = htons(flags);
976 *bufp++ = htons(tunnel_id);
977 *bufp++ = htons(session_id);
978 if (session->send_seq) {
979 *bufp++ = htons(session->ns);
980 *bufp++ = 0;
981 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000982 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000983 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
984 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000985 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000986
987 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000988}
989
James Chapmanf7faffa2010-04-02 06:18:49 +0000990static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000991{
James Chapman0d767512010-04-02 06:19:00 +0000992 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000993 char *bufp = buf;
994 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000995
James Chapman0d767512010-04-02 06:19:00 +0000996 /* Setup L2TP header. The header differs slightly for UDP and
997 * IP encapsulations. For UDP, there is 4 bytes of flags.
998 */
999 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1000 u16 flags = L2TP_HDR_VER_3;
1001 *((__be16 *) bufp) = htons(flags);
1002 bufp += 2;
1003 *((__be16 *) bufp) = 0;
1004 bufp += 2;
1005 }
1006
James Chapmanf7faffa2010-04-02 06:18:49 +00001007 *((__be32 *) bufp) = htonl(session->peer_session_id);
1008 bufp += 4;
1009 if (session->cookie_len) {
1010 memcpy(bufp, &session->cookie[0], session->cookie_len);
1011 bufp += session->cookie_len;
1012 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001013 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1014 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +00001015
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001016 if (session->send_seq) {
1017 l2h = 0x40000000 | session->ns;
1018 session->ns++;
1019 session->ns &= 0xffffff;
1020 l2tp_dbg(session, L2TP_MSG_SEQ,
1021 "%s: updated ns to %u\n",
1022 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001023 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001024
1025 *((__be32 *)bufp) = htonl(l2h);
1026 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001027 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001028
1029 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001030}
James Chapmanfd558d12010-04-02 06:18:33 +00001031
stephen hemmingerfc130842010-10-21 07:50:46 +00001032static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001033 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001034{
1035 struct l2tp_tunnel *tunnel = session->tunnel;
1036 unsigned int len = skb->len;
1037 int error;
1038
1039 /* Debug */
1040 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001041 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001042 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001043 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001044 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001045 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001046
1047 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001048 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1049 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001050
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001051 pr_debug("%s: xmit\n", session->name);
1052 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1053 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001054 }
1055
1056 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001057 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001058#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001059 if (l2tp_sk_is_v6(tunnel->sock))
Eric Dumazetb0270e92014-04-15 12:58:34 -04001060 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001061 else
1062#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001063 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001064
1065 /* Update stats */
1066 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001067 atomic_long_inc(&tunnel->stats.tx_packets);
1068 atomic_long_add(len, &tunnel->stats.tx_bytes);
1069 atomic_long_inc(&session->stats.tx_packets);
1070 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001071 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001072 atomic_long_inc(&tunnel->stats.tx_errors);
1073 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001074 }
1075
1076 return 0;
1077}
James Chapmanfd558d12010-04-02 06:18:33 +00001078
James Chapmanfd558d12010-04-02 06:18:33 +00001079/* If caller requires the skb to have a ppp header, the header must be
1080 * inserted in the skb data before calling this function.
1081 */
1082int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1083{
1084 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001085 struct l2tp_tunnel *tunnel = session->tunnel;
1086 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001087 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001088 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001089 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001090 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001091 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1092 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001093 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001094
1095 /* Check that there's enough headroom in the skb to insert IP,
1096 * UDP and L2TP headers. If not enough, expand it to
1097 * make room. Adjust truesize.
1098 */
1099 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001100 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001101 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001102 kfree_skb(skb);
1103 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001104 }
James Chapmanfd558d12010-04-02 06:18:33 +00001105
James Chapmanfd558d12010-04-02 06:18:33 +00001106 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001107 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001108
James Chapman0d767512010-04-02 06:19:00 +00001109 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001110 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1111 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1112 IPSKB_REROUTED);
1113 nf_reset(skb);
1114
David S. Miller6af88da2011-05-08 13:45:20 -07001115 bh_lock_sock(sk);
1116 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001117 kfree_skb(skb);
1118 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001119 goto out_unlock;
1120 }
1121
Paolo Abenib954f942018-03-12 14:54:24 +01001122 /* The user-space may change the connection status for the user-space
1123 * provided socket at run time: we must check it under the socket lock
1124 */
1125 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1126 kfree_skb(skb);
1127 ret = NET_XMIT_DROP;
1128 goto out_unlock;
1129 }
1130
James Chapmanfd558d12010-04-02 06:18:33 +00001131 /* Get routing info from the tunnel socket */
1132 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001133 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001134
David S. Millerd9d8da82011-05-06 22:23:20 -07001135 inet = inet_sk(sk);
1136 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001137 switch (tunnel->encap) {
1138 case L2TP_ENCAPTYPE_UDP:
1139 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001140 __skb_push(skb, sizeof(*uh));
1141 skb_reset_transport_header(skb);
1142 uh = udp_hdr(skb);
1143 uh->source = inet->inet_sport;
1144 uh->dest = inet->inet_dport;
1145 udp_len = uhlen + hdr_len + data_len;
1146 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001147
1148 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001149#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001150 if (l2tp_sk_is_v6(sk))
Tom Herbert77157e12014-06-04 17:19:56 -07001151 udp6_set_csum(udp_get_no_check6_tx(sk),
1152 skb, &inet6_sk(sk)->saddr,
1153 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001154 else
1155#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001156 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1157 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001158 break;
1159
1160 case L2TP_ENCAPTYPE_IP:
1161 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001162 }
1163
David S. Millerd9d8da82011-05-06 22:23:20 -07001164 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001165out_unlock:
1166 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001167
Eric Dumazetb8c84302012-06-28 20:15:13 +00001168 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001169}
1170EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1171
1172/*****************************************************************************
1173 * Tinnel and session create/destroy.
1174 *****************************************************************************/
1175
1176/* Tunnel socket destruct hook.
1177 * The tunnel context is deleted only when all session sockets have been
1178 * closed.
1179 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001180static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001181{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001182 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001183
James Chapmanfd558d12010-04-02 06:18:33 +00001184 if (tunnel == NULL)
1185 goto end;
1186
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001187 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001188
Tom Parkinf8ccac02013-01-31 23:43:00 +00001189 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001190 switch (tunnel->encap) {
1191 case L2TP_ENCAPTYPE_UDP:
1192 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1193 (udp_sk(sk))->encap_type = 0;
1194 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001195 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001196 break;
1197 case L2TP_ENCAPTYPE_IP:
1198 break;
1199 }
James Chapmanfd558d12010-04-02 06:18:33 +00001200
1201 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001202 sk->sk_destruct = tunnel->old_sk_destruct;
1203 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001204
James Chapmanfd558d12010-04-02 06:18:33 +00001205 /* Call the original destructor */
1206 if (sk->sk_destruct)
1207 (*sk->sk_destruct)(sk);
James Chapmand00fa9a2018-02-23 17:45:45 +00001208
1209 kfree_rcu(tunnel, rcu);
James Chapmanfd558d12010-04-02 06:18:33 +00001210end:
1211 return;
1212}
James Chapmanfd558d12010-04-02 06:18:33 +00001213
1214/* When the tunnel is closed, all the attached sessions need to go too.
1215 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001216void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001217{
1218 int hash;
1219 struct hlist_node *walk;
1220 struct hlist_node *tmp;
1221 struct l2tp_session *session;
1222
1223 BUG_ON(tunnel == NULL);
1224
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001225 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1226 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001227
1228 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001229 tunnel->acpt_newsess = false;
James Chapmanfd558d12010-04-02 06:18:33 +00001230 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1231again:
1232 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1233 session = hlist_entry(walk, struct l2tp_session, hlist);
1234
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001235 l2tp_info(session, L2TP_MSG_CONTROL,
1236 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001237
1238 hlist_del_init(&session->hlist);
1239
Guillaume Naultb228a942017-09-22 15:39:24 +02001240 if (test_and_set_bit(0, &session->dead))
1241 goto again;
1242
James Chapmanfd558d12010-04-02 06:18:33 +00001243 write_unlock_bh(&tunnel->hlist_lock);
1244
Tom Parkinf6e16b22013-03-19 06:11:23 +00001245 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001246 l2tp_session_queue_purge(session);
1247
James Chapmanfd558d12010-04-02 06:18:33 +00001248 if (session->session_close != NULL)
1249 (*session->session_close)(session);
1250
Tom Parkin9980d002013-03-19 06:11:13 +00001251 l2tp_session_dec_refcount(session);
1252
James Chapmanfd558d12010-04-02 06:18:33 +00001253 write_lock_bh(&tunnel->hlist_lock);
1254
1255 /* Now restart from the beginning of this hash
1256 * chain. We always remove a session from the
1257 * list so we are guaranteed to make forward
1258 * progress.
1259 */
1260 goto again;
1261 }
1262 }
1263 write_unlock_bh(&tunnel->hlist_lock);
1264}
Tom Parkine34f4c72013-03-19 06:11:14 +00001265EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001266
Tom Parkin9980d002013-03-19 06:11:13 +00001267/* Tunnel socket destroy hook for UDP encapsulation */
1268static void l2tp_udp_encap_destroy(struct sock *sk)
1269{
James Chapmand00fa9a2018-02-23 17:45:45 +00001270 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1271
1272 if (tunnel)
1273 l2tp_tunnel_delete(tunnel);
Tom Parkin9980d002013-03-19 06:11:13 +00001274}
1275
Tom Parkinf8ccac02013-01-31 23:43:00 +00001276/* Workqueue tunnel deletion function */
1277static void l2tp_tunnel_del_work(struct work_struct *work)
1278{
James Chapmand00fa9a2018-02-23 17:45:45 +00001279 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1280 del_work);
1281 struct sock *sk = tunnel->sock;
1282 struct socket *sock = sk->sk_socket;
James Chapman28f5bfb2018-02-23 17:45:47 +00001283 struct l2tp_net *pn;
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001284
1285 l2tp_tunnel_closeall(tunnel);
1286
James Chapman76a6abd2018-02-23 17:45:43 +00001287 /* If the tunnel socket was created within the kernel, use
Tom Parkin02d13ed2013-03-19 06:11:18 +00001288 * the sk API to release it here.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001289 */
James Chapman76a6abd2018-02-23 17:45:43 +00001290 if (tunnel->fd < 0) {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001291 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001292 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001293 sock_release(sock);
1294 }
Tom Parkin167eb172013-01-31 23:43:03 +00001295 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001296
James Chapman28f5bfb2018-02-23 17:45:47 +00001297 /* Remove the tunnel struct from the tunnel list */
1298 pn = l2tp_pernet(tunnel->l2tp_net);
1299 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1300 list_del_rcu(&tunnel->list);
1301 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1302
James Chapmand00fa9a2018-02-23 17:45:45 +00001303 /* drop initial ref */
1304 l2tp_tunnel_dec_refcount(tunnel);
1305
1306 /* drop workqueue ref */
Alexander Couzens06a15f52015-09-28 11:32:42 +02001307 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001308}
James Chapmanfd558d12010-04-02 06:18:33 +00001309
James Chapman789a4a22010-04-02 06:19:40 +00001310/* Create a socket for the tunnel, if one isn't set up by
1311 * userspace. This is used for static tunnels where there is no
1312 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001313 *
1314 * Since we don't want these sockets to keep a namespace alive by
1315 * themselves, we drop the socket's namespace refcount after creation.
1316 * These sockets are freed when the namespace exits using the pernet
1317 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001318 */
Tom Parkin167eb172013-01-31 23:43:03 +00001319static int l2tp_tunnel_sock_create(struct net *net,
1320 u32 tunnel_id,
1321 u32 peer_tunnel_id,
1322 struct l2tp_tunnel_cfg *cfg,
1323 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001324{
1325 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001326 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001327 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001328
1329 switch (cfg->encap) {
1330 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001331 memset(&udp_conf, 0, sizeof(udp_conf));
1332
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001333#if IS_ENABLED(CONFIG_IPV6)
1334 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001335 udp_conf.family = AF_INET6;
1336 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1337 sizeof(udp_conf.local_ip6));
1338 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1339 sizeof(udp_conf.peer_ip6));
1340 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001341 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001342 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001343 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001344 } else
1345#endif
1346 {
Tom Herbert85644b42014-07-13 19:49:48 -07001347 udp_conf.family = AF_INET;
1348 udp_conf.local_ip = cfg->local_ip;
1349 udp_conf.peer_ip = cfg->peer_ip;
1350 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001351 }
James Chapman789a4a22010-04-02 06:19:40 +00001352
Tom Herbert85644b42014-07-13 19:49:48 -07001353 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1354 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1355
1356 err = udp_sock_create(net, &udp_conf, &sock);
1357 if (err < 0)
1358 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001359
1360 break;
1361
1362 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001363#if IS_ENABLED(CONFIG_IPV6)
1364 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001365 struct sockaddr_l2tpip6 ip6_addr = {0};
1366
Eric W. Biederman26abe142015-05-08 21:10:31 -05001367 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001368 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001369 if (err < 0)
1370 goto out;
1371
James Chapman5dac94e2012-04-29 21:48:55 +00001372 ip6_addr.l2tp_family = AF_INET6;
1373 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1374 sizeof(ip6_addr.l2tp_addr));
1375 ip6_addr.l2tp_conn_id = tunnel_id;
1376 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1377 sizeof(ip6_addr));
1378 if (err < 0)
1379 goto out;
1380
1381 ip6_addr.l2tp_family = AF_INET6;
1382 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1383 sizeof(ip6_addr.l2tp_addr));
1384 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1385 err = kernel_connect(sock,
1386 (struct sockaddr *) &ip6_addr,
1387 sizeof(ip6_addr), 0);
1388 if (err < 0)
1389 goto out;
1390 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001391#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001392 {
Tom Herbert85644b42014-07-13 19:49:48 -07001393 struct sockaddr_l2tpip ip_addr = {0};
1394
Eric W. Biederman26abe142015-05-08 21:10:31 -05001395 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001396 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001397 if (err < 0)
1398 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001399
James Chapman5dac94e2012-04-29 21:48:55 +00001400 ip_addr.l2tp_family = AF_INET;
1401 ip_addr.l2tp_addr = cfg->local_ip;
1402 ip_addr.l2tp_conn_id = tunnel_id;
1403 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1404 sizeof(ip_addr));
1405 if (err < 0)
1406 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001407
James Chapman5dac94e2012-04-29 21:48:55 +00001408 ip_addr.l2tp_family = AF_INET;
1409 ip_addr.l2tp_addr = cfg->peer_ip;
1410 ip_addr.l2tp_conn_id = peer_tunnel_id;
1411 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1412 sizeof(ip_addr), 0);
1413 if (err < 0)
1414 goto out;
1415 }
James Chapman789a4a22010-04-02 06:19:40 +00001416 break;
1417
1418 default:
1419 goto out;
1420 }
1421
1422out:
Tom Parkin167eb172013-01-31 23:43:03 +00001423 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001424 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001425 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001426 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001427 *sockp = NULL;
1428 }
1429
1430 return err;
1431}
1432
Eric Dumazet37159ef2012-09-04 07:18:57 +00001433static struct lock_class_key l2tp_socket_class;
1434
James Chapmanfd558d12010-04-02 06:18:33 +00001435int 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)
1436{
1437 struct l2tp_tunnel *tunnel = NULL;
1438 int err;
James Chapman0d767512010-04-02 06:19:00 +00001439 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001440
James Chapman0d767512010-04-02 06:19:00 +00001441 if (cfg != NULL)
1442 encap = cfg->encap;
1443
James Chapmanfd558d12010-04-02 06:18:33 +00001444 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1445 if (tunnel == NULL) {
1446 err = -ENOMEM;
1447 goto err;
1448 }
1449
1450 tunnel->version = version;
1451 tunnel->tunnel_id = tunnel_id;
1452 tunnel->peer_tunnel_id = peer_tunnel_id;
1453 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1454
1455 tunnel->magic = L2TP_TUNNEL_MAGIC;
1456 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1457 rwlock_init(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001458 tunnel->acpt_newsess = true;
James Chapmanfd558d12010-04-02 06:18:33 +00001459
James Chapman0d767512010-04-02 06:19:00 +00001460 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001461 tunnel->debug = cfg->debug;
1462
James Chapman0d767512010-04-02 06:19:00 +00001463 tunnel->encap = encap;
James Chapmanfd558d12010-04-02 06:18:33 +00001464
James Chapmand00fa9a2018-02-23 17:45:45 +00001465 refcount_set(&tunnel->ref_count, 1);
James Chapmand00fa9a2018-02-23 17:45:45 +00001466 tunnel->fd = fd;
1467
Tom Parkinf8ccac02013-01-31 23:43:00 +00001468 /* Init delete workqueue struct */
1469 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1470
James Chapmanfd558d12010-04-02 06:18:33 +00001471 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001472
1473 err = 0;
1474err:
1475 if (tunnelp)
1476 *tunnelp = tunnel;
1477
James Chapmanfd558d12010-04-02 06:18:33 +00001478 return err;
1479}
1480EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1481
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001482static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1483 enum l2tp_encap_type encap)
1484{
1485 if (!net_eq(sock_net(sk), net))
1486 return -EINVAL;
1487
1488 if (sk->sk_type != SOCK_DGRAM)
1489 return -EPROTONOSUPPORT;
1490
1491 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1492 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1493 return -EPROTONOSUPPORT;
1494
1495 if (sk->sk_user_data)
1496 return -EBUSY;
1497
1498 return 0;
1499}
1500
1501int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1502 struct l2tp_tunnel_cfg *cfg)
1503{
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001504 struct l2tp_tunnel *tunnel_walk;
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001505 struct l2tp_net *pn;
1506 struct socket *sock;
1507 struct sock *sk;
1508 int ret;
1509
1510 if (tunnel->fd < 0) {
1511 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1512 tunnel->peer_tunnel_id, cfg,
1513 &sock);
1514 if (ret < 0)
1515 goto err;
1516 } else {
1517 sock = sockfd_lookup(tunnel->fd, &ret);
1518 if (!sock)
1519 goto err;
1520
1521 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1522 if (ret < 0)
1523 goto err_sock;
1524 }
1525
1526 sk = sock->sk;
1527
1528 sock_hold(sk);
1529 tunnel->sock = sk;
1530 tunnel->l2tp_net = net;
1531
1532 pn = l2tp_pernet(net);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001533
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001534 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001535 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1536 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1537 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1538
1539 ret = -EEXIST;
1540 goto err_sock;
1541 }
1542 }
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001543 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1544 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1545
1546 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1547 struct udp_tunnel_sock_cfg udp_cfg = {
1548 .sk_user_data = tunnel,
1549 .encap_type = UDP_ENCAP_L2TPINUDP,
1550 .encap_rcv = l2tp_udp_encap_recv,
1551 .encap_destroy = l2tp_udp_encap_destroy,
1552 };
1553
1554 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1555 } else {
1556 sk->sk_user_data = tunnel;
1557 }
1558
1559 tunnel->old_sk_destruct = sk->sk_destruct;
1560 sk->sk_destruct = &l2tp_tunnel_destruct;
1561 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1562 "l2tp_sock");
1563 sk->sk_allocation = GFP_ATOMIC;
1564
1565 if (tunnel->fd >= 0)
1566 sockfd_put(sock);
1567
1568 return 0;
1569
1570err_sock:
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001571 if (tunnel->fd < 0)
1572 sock_release(sock);
1573 else
1574 sockfd_put(sock);
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001575err:
1576 return ret;
1577}
1578EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1579
James Chapman309795f2010-04-02 06:19:10 +00001580/* This function is used by the netlink TUNNEL_DELETE command.
1581 */
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001582void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001583{
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001584 if (!test_and_set_bit(0, &tunnel->dead)) {
1585 l2tp_tunnel_inc_refcount(tunnel);
1586 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001587 }
James Chapman309795f2010-04-02 06:19:10 +00001588}
1589EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1590
James Chapmanfd558d12010-04-02 06:18:33 +00001591/* Really kill the session.
1592 */
1593void l2tp_session_free(struct l2tp_session *session)
1594{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001595 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001596
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001597 BUG_ON(refcount_read(&session->ref_count) != 0);
James Chapmanfd558d12010-04-02 06:18:33 +00001598
Tom Parkinf6e16b22013-03-19 06:11:23 +00001599 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001600 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001601 l2tp_tunnel_dec_refcount(tunnel);
1602 }
1603
1604 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001605}
1606EXPORT_SYMBOL_GPL(l2tp_session_free);
1607
Tom Parkinf6e16b22013-03-19 06:11:23 +00001608/* Remove an l2tp session from l2tp_core's hash lists.
1609 * Provides a tidyup interface for pseudowire code which can't just route all
1610 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1611 * callback.
1612 */
1613void __l2tp_session_unhash(struct l2tp_session *session)
1614{
1615 struct l2tp_tunnel *tunnel = session->tunnel;
1616
1617 /* Remove the session from core hashes */
1618 if (tunnel) {
1619 /* Remove from the per-tunnel hash */
1620 write_lock_bh(&tunnel->hlist_lock);
1621 hlist_del_init(&session->hlist);
1622 write_unlock_bh(&tunnel->hlist_lock);
1623
1624 /* For L2TPv3 we have a per-net hash: remove from there, too */
1625 if (tunnel->version != L2TP_HDR_VER_2) {
1626 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1627 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1628 hlist_del_init_rcu(&session->global_hlist);
1629 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1630 synchronize_rcu();
1631 }
1632 }
1633}
1634EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1635
James Chapman309795f2010-04-02 06:19:10 +00001636/* This function is used by the netlink SESSION_DELETE command and by
1637 pseudowire modules.
1638 */
1639int l2tp_session_delete(struct l2tp_session *session)
1640{
Guillaume Naultb228a942017-09-22 15:39:24 +02001641 if (test_and_set_bit(0, &session->dead))
1642 return 0;
1643
Tom Parkinf6e16b22013-03-19 06:11:23 +00001644 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001645 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001646 if (session->session_close != NULL)
1647 (*session->session_close)(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001648
James Chapman309795f2010-04-02 06:19:10 +00001649 l2tp_session_dec_refcount(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001650
James Chapman309795f2010-04-02 06:19:10 +00001651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(l2tp_session_delete);
1654
James Chapmanf7faffa2010-04-02 06:18:49 +00001655/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001656 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001657 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001658void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001659{
1660 if (version == L2TP_HDR_VER_2) {
1661 session->hdr_len = 6;
1662 if (session->send_seq)
1663 session->hdr_len += 4;
1664 } else {
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001665 session->hdr_len = 4 + session->cookie_len;
1666 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001667 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1668 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001669 }
1670
1671}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001672EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001673
James Chapmanfd558d12010-04-02 06:18:33 +00001674struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1675{
1676 struct l2tp_session *session;
1677
1678 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1679 if (session != NULL) {
1680 session->magic = L2TP_SESSION_MAGIC;
1681 session->tunnel = tunnel;
1682
1683 session->session_id = session_id;
1684 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001685 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001686 if (tunnel->version == L2TP_HDR_VER_2)
1687 session->nr_max = 0xffff;
1688 else
1689 session->nr_max = 0xffffff;
1690 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001691 session->nr_oos_count_max = 4;
1692
1693 /* Use NR of first received packet */
1694 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001695
1696 sprintf(&session->name[0], "sess %u/%u",
1697 tunnel->tunnel_id, session->session_id);
1698
1699 skb_queue_head_init(&session->reorder_q);
1700
1701 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001702 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001703
1704 /* Inherit debug options from tunnel */
1705 session->debug = tunnel->debug;
1706
1707 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001708 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001709 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001710 session->mtu = cfg->mtu;
1711 session->mru = cfg->mru;
1712 session->send_seq = cfg->send_seq;
1713 session->recv_seq = cfg->recv_seq;
1714 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001715 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001716 session->l2specific_type = cfg->l2specific_type;
James Chapmanf7faffa2010-04-02 06:18:49 +00001717 session->cookie_len = cfg->cookie_len;
1718 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1719 session->peer_cookie_len = cfg->peer_cookie_len;
1720 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001721 }
1722
James Chapmanf7faffa2010-04-02 06:18:49 +00001723 if (tunnel->version == L2TP_HDR_VER_2)
1724 session->build_header = l2tp_build_l2tpv2_header;
1725 else
1726 session->build_header = l2tp_build_l2tpv3_header;
1727
1728 l2tp_session_set_header_len(session, tunnel->version);
1729
Guillaume Nault9ee369a2017-08-25 16:22:17 +02001730 refcount_set(&session->ref_count, 1);
1731
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001732 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001733 }
1734
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001735 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001736}
1737EXPORT_SYMBOL_GPL(l2tp_session_create);
1738
1739/*****************************************************************************
1740 * Init and cleanup
1741 *****************************************************************************/
1742
1743static __net_init int l2tp_init_net(struct net *net)
1744{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001745 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001746 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001747
James Chapmanfd558d12010-04-02 06:18:33 +00001748 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001749 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001750
James Chapmanf7faffa2010-04-02 06:18:49 +00001751 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1752 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1753
James Chapmane02d4942010-04-02 06:19:16 +00001754 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001755
James Chapmanfd558d12010-04-02 06:18:33 +00001756 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001757}
1758
Tom Parkin167eb172013-01-31 23:43:03 +00001759static __net_exit void l2tp_exit_net(struct net *net)
1760{
1761 struct l2tp_net *pn = l2tp_pernet(net);
1762 struct l2tp_tunnel *tunnel = NULL;
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001763 int hash;
Tom Parkin167eb172013-01-31 23:43:03 +00001764
1765 rcu_read_lock_bh();
1766 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slaby4dc12ff2017-10-25 15:57:55 +02001767 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001768 }
1769 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001770
1771 flush_workqueue(l2tp_wq);
1772 rcu_barrier();
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001773
1774 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1775 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
Tom Parkin167eb172013-01-31 23:43:03 +00001776}
1777
James Chapmanfd558d12010-04-02 06:18:33 +00001778static struct pernet_operations l2tp_net_ops = {
1779 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001780 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001781 .id = &l2tp_net_id,
1782 .size = sizeof(struct l2tp_net),
1783};
1784
1785static int __init l2tp_init(void)
1786{
1787 int rc = 0;
1788
1789 rc = register_pernet_device(&l2tp_net_ops);
1790 if (rc)
1791 goto out;
1792
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001793 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001794 if (!l2tp_wq) {
1795 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001796 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001797 rc = -ENOMEM;
1798 goto out;
1799 }
1800
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001801 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001802
1803out:
1804 return rc;
1805}
1806
1807static void __exit l2tp_exit(void)
1808{
1809 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001810 if (l2tp_wq) {
1811 destroy_workqueue(l2tp_wq);
1812 l2tp_wq = NULL;
1813 }
James Chapmanfd558d12010-04-02 06:18:33 +00001814}
1815
1816module_init(l2tp_init);
1817module_exit(l2tp_exit);
1818
1819MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1820MODULE_DESCRIPTION("L2TP core");
1821MODULE_LICENSE("GPL");
1822MODULE_VERSION(L2TP_DRV_VERSION);
1823