blob: 40261cb68e83686c73a3567062fbf28f4a3d3146 [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
James Chapmanfd558d12010-04-02 06:18:33 +0000358/*****************************************************************************
359 * Receive data handling
360 *****************************************************************************/
361
362/* Queue a skb in order. We come here only if the skb has an L2TP sequence
363 * number.
364 */
365static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
366{
367 struct sk_buff *skbp;
368 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000369 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000370
371 spin_lock_bh(&session->reorder_q.lock);
372 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
373 if (L2TP_SKB_CB(skbp)->ns > ns) {
374 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000375 l2tp_dbg(session, L2TP_MSG_SEQ,
376 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
377 session->name, ns, L2TP_SKB_CB(skbp)->ns,
378 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000379 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000380 goto out;
381 }
382 }
383
384 __skb_queue_tail(&session->reorder_q, skb);
385
386out:
387 spin_unlock_bh(&session->reorder_q.lock);
388}
389
390/* Dequeue a single skb.
391 */
392static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
393{
394 struct l2tp_tunnel *tunnel = session->tunnel;
395 int length = L2TP_SKB_CB(skb)->length;
396
397 /* We're about to requeue the skb, so return resources
398 * to its current owner (a socket receive buffer).
399 */
400 skb_orphan(skb);
401
Tom Parkin7b7c0712013-03-19 06:11:22 +0000402 atomic_long_inc(&tunnel->stats.rx_packets);
403 atomic_long_add(length, &tunnel->stats.rx_bytes);
404 atomic_long_inc(&session->stats.rx_packets);
405 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000406
407 if (L2TP_SKB_CB(skb)->has_seq) {
408 /* Bump our Nr */
409 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100410 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000411
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000412 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
413 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000414 }
415
416 /* call private receive handler */
417 if (session->recv_skb != NULL)
418 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
419 else
420 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000421}
422
423/* Dequeue skbs from the session's reorder_q, subject to packet order.
424 * Skbs that have been in the queue for too long are simply discarded.
425 */
426static void l2tp_recv_dequeue(struct l2tp_session *session)
427{
428 struct sk_buff *skb;
429 struct sk_buff *tmp;
430
431 /* If the pkt at the head of the queue has the nr that we
432 * expect to send up next, dequeue it and any other
433 * in-sequence packets behind it.
434 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000435start:
James Chapmanfd558d12010-04-02 06:18:33 +0000436 spin_lock_bh(&session->reorder_q.lock);
437 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
438 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000439 atomic_long_inc(&session->stats.rx_seq_discards);
440 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000441 l2tp_dbg(session, L2TP_MSG_SEQ,
442 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
443 session->name, L2TP_SKB_CB(skb)->ns,
444 L2TP_SKB_CB(skb)->length, session->nr,
445 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000446 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000447 __skb_unlink(skb, &session->reorder_q);
448 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000449 continue;
450 }
451
452 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000453 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000454 l2tp_dbg(session, L2TP_MSG_SEQ,
455 "%s: advancing nr to next pkt: %u -> %u",
456 session->name, session->nr,
457 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000458 session->reorder_skip = 0;
459 session->nr = L2TP_SKB_CB(skb)->ns;
460 }
James Chapmanfd558d12010-04-02 06:18:33 +0000461 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000462 l2tp_dbg(session, L2TP_MSG_SEQ,
463 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
464 session->name, L2TP_SKB_CB(skb)->ns,
465 L2TP_SKB_CB(skb)->length, session->nr,
466 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000467 goto out;
468 }
469 }
470 __skb_unlink(skb, &session->reorder_q);
471
472 /* Process the skb. We release the queue lock while we
473 * do so to let other contexts process the queue.
474 */
475 spin_unlock_bh(&session->reorder_q.lock);
476 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000477 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000478 }
479
480out:
481 spin_unlock_bh(&session->reorder_q.lock);
482}
483
James Chapman8a1631d2013-07-02 20:28:59 +0100484static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
485{
486 u32 nws;
487
488 if (nr >= session->nr)
489 nws = nr - session->nr;
490 else
491 nws = (session->nr_max + 1) - (session->nr - nr);
492
493 return nws < session->nr_window_size;
494}
495
James Chapmanb6dc01a2013-07-02 20:28:58 +0100496/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
497 * acceptable, else non-zero.
498 */
499static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
500{
James Chapman8a1631d2013-07-02 20:28:59 +0100501 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
502 /* Packet sequence number is outside allowed window.
503 * Discard it.
504 */
505 l2tp_dbg(session, L2TP_MSG_SEQ,
506 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
507 session->name, L2TP_SKB_CB(skb)->ns,
508 L2TP_SKB_CB(skb)->length, session->nr);
509 goto discard;
510 }
511
James Chapmanb6dc01a2013-07-02 20:28:58 +0100512 if (session->reorder_timeout != 0) {
513 /* Packet reordering enabled. Add skb to session's
514 * reorder queue, in order of ns.
515 */
516 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100517 goto out;
518 }
519
520 /* Packet reordering disabled. Discard out-of-sequence packets, while
521 * tracking the number if in-sequence packets after the first OOS packet
522 * is seen. After nr_oos_count_max in-sequence packets, reset the
523 * sequence number to re-enable packet reception.
524 */
525 if (L2TP_SKB_CB(skb)->ns == session->nr) {
526 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100527 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100528 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
529 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
530
531 if (nr_oos == nr_next)
532 session->nr_oos_count++;
533 else
534 session->nr_oos_count = 0;
535
536 session->nr_oos = nr_oos;
537 if (session->nr_oos_count > session->nr_oos_count_max) {
538 session->reorder_skip = 1;
539 l2tp_dbg(session, L2TP_MSG_SEQ,
540 "%s: %d oos packets received. Resetting sequence numbers\n",
541 session->name, session->nr_oos_count);
542 }
543 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100544 atomic_long_inc(&session->stats.rx_seq_discards);
545 l2tp_dbg(session, L2TP_MSG_SEQ,
546 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
547 session->name, L2TP_SKB_CB(skb)->ns,
548 L2TP_SKB_CB(skb)->length, session->nr,
549 skb_queue_len(&session->reorder_q));
550 goto discard;
551 }
552 skb_queue_tail(&session->reorder_q, skb);
553 }
554
James Chapmana0dbd822013-07-02 20:29:00 +0100555out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100556 return 0;
557
558discard:
559 return 1;
560}
561
James Chapmanf7faffa2010-04-02 06:18:49 +0000562/* Do receive processing of L2TP data frames. We handle both L2TPv2
563 * and L2TPv3 data frames here.
564 *
565 * L2TPv2 Data Message Header
566 *
567 * 0 1 2 3
568 * 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
569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
572 * | Tunnel ID | Session ID |
573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
574 * | Ns (opt) | Nr (opt) |
575 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
576 * | Offset Size (opt) | Offset pad... (opt)
577 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
578 *
579 * Data frames are marked by T=0. All other fields are the same as
580 * those in L2TP control frames.
581 *
582 * L2TPv3 Data Message Header
583 *
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 * | L2TP Session Header |
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | L2-Specific Sublayer |
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * | Tunnel Payload ...
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 *
592 * L2TPv3 Session Header Over IP
593 *
594 * 0 1 2 3
595 * 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
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | Session ID |
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * | Cookie (optional, maximum 64 bits)...
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * |
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 *
604 * L2TPv3 L2-Specific Sublayer Format
605 *
606 * 0 1 2 3
607 * 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
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * |x|S|x|x|x|x|x|x| Sequence Number |
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 *
Guillaume Nault23fe8462018-01-05 19:47:14 +0100612 * Cookie value and sublayer format are negotiated with the peer when
613 * the session is set up. Unlike L2TPv2, we do not need to parse the
614 * packet header to determine if optional fields are present.
James Chapmanf7faffa2010-04-02 06:18:49 +0000615 *
616 * Caller must already have parsed the frame and determined that it is
617 * a data (not control) frame before coming here. Fields up to the
618 * session-id have already been parsed and ptr points to the data
619 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000620 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000621void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
622 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
623 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000624{
James Chapmanf7faffa2010-04-02 06:18:49 +0000625 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000626 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000627 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000628
James Chapmanf7faffa2010-04-02 06:18:49 +0000629 /* Parse and check optional cookie */
630 if (session->peer_cookie_len > 0) {
631 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000632 l2tp_info(tunnel, L2TP_MSG_DATA,
633 "%s: cookie mismatch (%u/%u). Discarding.\n",
634 tunnel->name, tunnel->tunnel_id,
635 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000636 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000637 goto discard;
638 }
639 ptr += session->peer_cookie_len;
640 }
641
James Chapmanfd558d12010-04-02 06:18:33 +0000642 /* Handle the optional sequence numbers. Sequence numbers are
643 * in different places for L2TPv2 and L2TPv3.
644 *
645 * If we are the LAC, enable/disable sequence numbers under
646 * the control of the LNS. If no sequence numbers present but
647 * we were expecting them, discard frame.
648 */
649 ns = nr = 0;
650 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000651 if (tunnel->version == L2TP_HDR_VER_2) {
652 if (hdrflags & L2TP_HDRFLAG_S) {
653 ns = ntohs(*(__be16 *) ptr);
654 ptr += 2;
655 nr = ntohs(*(__be16 *) ptr);
656 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000657
James Chapmanf7faffa2010-04-02 06:18:49 +0000658 /* Store L2TP info in the skb */
659 L2TP_SKB_CB(skb)->ns = ns;
660 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000661
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000662 l2tp_dbg(session, L2TP_MSG_SEQ,
663 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
664 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000665 }
666 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
667 u32 l2h = ntohl(*(__be32 *) ptr);
668
669 if (l2h & 0x40000000) {
670 ns = l2h & 0x00ffffff;
671
672 /* Store L2TP info in the skb */
673 L2TP_SKB_CB(skb)->ns = ns;
674 L2TP_SKB_CB(skb)->has_seq = 1;
675
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000676 l2tp_dbg(session, L2TP_MSG_SEQ,
677 "%s: recv data ns=%u, session nr=%u\n",
678 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000679 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100680 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000681 }
682
683 if (L2TP_SKB_CB(skb)->has_seq) {
684 /* Received a packet with sequence numbers. If we're the LNS,
685 * check if we sre sending sequence numbers and if not,
686 * configure it so.
687 */
688 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000689 l2tp_info(session, L2TP_MSG_SEQ,
690 "%s: requested to enable seq numbers by LNS\n",
691 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000692 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000693 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000694 }
695 } else {
696 /* No sequence numbers.
697 * If user has configured mandatory sequence numbers, discard.
698 */
699 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000700 l2tp_warn(session, L2TP_MSG_SEQ,
701 "%s: recv data has no seq numbers when required. Discarding.\n",
702 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000703 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000704 goto discard;
705 }
706
707 /* If we're the LAC and we're sending sequence numbers, the
708 * LNS has requested that we no longer send sequence numbers.
709 * If we're the LNS and we're sending sequence numbers, the
710 * LAC is broken. Discard the frame.
711 */
712 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000713 l2tp_info(session, L2TP_MSG_SEQ,
714 "%s: requested to disable seq numbers by LNS\n",
715 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000716 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000717 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000718 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000719 l2tp_warn(session, L2TP_MSG_SEQ,
720 "%s: recv data has no seq numbers when required. Discarding.\n",
721 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000722 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000723 goto discard;
724 }
725 }
726
James Chapman900631e2018-01-03 22:48:06 +0000727 /* Session data offset is defined only for L2TPv2 and is
728 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000729 */
730 if (tunnel->version == L2TP_HDR_VER_2) {
731 /* If offset bit set, skip it. */
732 if (hdrflags & L2TP_HDRFLAG_O) {
733 offset = ntohs(*(__be16 *)ptr);
734 ptr += 2 + offset;
735 }
James Chapman900631e2018-01-03 22:48:06 +0000736 }
James Chapmanfd558d12010-04-02 06:18:33 +0000737
738 offset = ptr - optr;
739 if (!pskb_may_pull(skb, offset))
740 goto discard;
741
742 __skb_pull(skb, offset);
743
744 /* If caller wants to process the payload before we queue the
745 * packet, do so now.
746 */
747 if (payload_hook)
748 if ((*payload_hook)(skb))
749 goto discard;
750
751 /* Prepare skb for adding to the session's reorder_q. Hold
752 * packets for max reorder_timeout or 1 second if not
753 * reordering.
754 */
755 L2TP_SKB_CB(skb)->length = length;
756 L2TP_SKB_CB(skb)->expires = jiffies +
757 (session->reorder_timeout ? session->reorder_timeout : HZ);
758
759 /* Add packet to the session's receive queue. Reordering is done here, if
760 * enabled. Saved L2TP protocol info is stored in skb->sb[].
761 */
762 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100763 if (l2tp_recv_data_seq(session, skb))
764 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000765 } else {
766 /* No sequence numbers. Add the skb to the tail of the
767 * reorder queue. This ensures that it will be
768 * delivered after all previous sequenced skbs.
769 */
770 skb_queue_tail(&session->reorder_q, skb);
771 }
772
773 /* Try to dequeue as many skbs from reorder_q as we can. */
774 l2tp_recv_dequeue(session);
775
James Chapmanf7faffa2010-04-02 06:18:49 +0000776 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000777
778discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000779 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000780 kfree_skb(skb);
James Chapmanf7faffa2010-04-02 06:18:49 +0000781}
782EXPORT_SYMBOL(l2tp_recv_common);
783
Tom Parkin48f72f92013-03-19 06:11:19 +0000784/* Drop skbs from the session's reorder_q
785 */
786int l2tp_session_queue_purge(struct l2tp_session *session)
787{
788 struct sk_buff *skb = NULL;
789 BUG_ON(!session);
790 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
791 while ((skb = skb_dequeue(&session->reorder_q))) {
792 atomic_long_inc(&session->stats.rx_errors);
793 kfree_skb(skb);
Tom Parkin48f72f92013-03-19 06:11:19 +0000794 }
795 return 0;
796}
797EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
798
James Chapmanf7faffa2010-04-02 06:18:49 +0000799/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
800 * here. The skb is not on a list when we get here.
801 * Returns 0 if the packet was a data packet and was successfully passed on.
802 * Returns 1 if the packet was not a good data packet and could not be
803 * forwarded. All such packets are passed up to userspace to deal with.
804 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000805static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
806 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000807{
808 struct l2tp_session *session = NULL;
809 unsigned char *ptr, *optr;
810 u16 hdrflags;
811 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000812 u16 version;
813 int length;
814
Tom Herbert58d60852014-05-07 16:52:48 -0700815 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000816
817 /* UDP always verifies the packet length. */
818 __skb_pull(skb, sizeof(struct udphdr));
819
820 /* Short packet? */
821 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000822 l2tp_info(tunnel, L2TP_MSG_DATA,
823 "%s: recv short packet (len=%d)\n",
824 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000825 goto error;
826 }
827
James Chapmanf7faffa2010-04-02 06:18:49 +0000828 /* Trace packet contents, if enabled */
829 if (tunnel->debug & L2TP_MSG_DATA) {
830 length = min(32u, skb->len);
831 if (!pskb_may_pull(skb, length))
832 goto error;
833
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000834 pr_debug("%s: recv\n", tunnel->name);
835 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000836 }
837
Eric Dumazete50e7052011-11-08 13:59:44 -0500838 /* Point to L2TP header */
839 optr = ptr = skb->data;
840
James Chapmanf7faffa2010-04-02 06:18:49 +0000841 /* Get L2TP header flags */
842 hdrflags = ntohs(*(__be16 *) ptr);
843
844 /* Check protocol version */
845 version = hdrflags & L2TP_HDR_VER_MASK;
846 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000847 l2tp_info(tunnel, L2TP_MSG_DATA,
848 "%s: recv protocol version mismatch: got %d expected %d\n",
849 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000850 goto error;
851 }
852
853 /* Get length of L2TP packet */
854 length = skb->len;
855
856 /* If type is control packet, it is handled by userspace. */
857 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000858 l2tp_dbg(tunnel, L2TP_MSG_DATA,
859 "%s: recv control packet, len=%d\n",
860 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000861 goto error;
862 }
863
864 /* Skip flags */
865 ptr += 2;
866
867 if (tunnel->version == L2TP_HDR_VER_2) {
868 /* If length is present, skip it */
869 if (hdrflags & L2TP_HDRFLAG_L)
870 ptr += 2;
871
872 /* Extract tunnel and session ID */
873 tunnel_id = ntohs(*(__be16 *) ptr);
874 ptr += 2;
875 session_id = ntohs(*(__be16 *) ptr);
876 ptr += 2;
877 } else {
878 ptr += 2; /* skip reserved bits */
879 tunnel_id = tunnel->tunnel_id;
880 session_id = ntohl(*(__be32 *) ptr);
881 ptr += 4;
882 }
883
884 /* Find the session context */
Guillaume Naulta4346212017-10-31 17:36:42 +0100885 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000886 if (!session || !session->recv_skb) {
Guillaume Naulta4346212017-10-31 17:36:42 +0100887 if (session)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200888 l2tp_session_dec_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200889
James Chapmanf7faffa2010-04-02 06:18:49 +0000890 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000891 l2tp_info(tunnel, L2TP_MSG_DATA,
892 "%s: no session found (%u/%u). Passing up.\n",
893 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000894 goto error;
895 }
896
897 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200898 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000899
900 return 0;
901
James Chapmanfd558d12010-04-02 06:18:33 +0000902error:
903 /* Put UDP header back */
904 __skb_push(skb, sizeof(struct udphdr));
905
906 return 1;
907}
James Chapmanfd558d12010-04-02 06:18:33 +0000908
909/* UDP encapsulation receive handler. See net/ipv4/udp.c.
910 * Return codes:
911 * 0 : success.
912 * <0: error
913 * >0: skb should be passed up to userspace as UDP.
914 */
915int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
916{
917 struct l2tp_tunnel *tunnel;
918
James Chapmand00fa9a2018-02-23 17:45:45 +0000919 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000920 if (tunnel == NULL)
921 goto pass_up;
922
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000923 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
924 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000925
926 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
James Chapmand00fa9a2018-02-23 17:45:45 +0000927 goto pass_up;
James Chapmanfd558d12010-04-02 06:18:33 +0000928
James Chapmanfd558d12010-04-02 06:18:33 +0000929 return 0;
930
James Chapmanfd558d12010-04-02 06:18:33 +0000931pass_up:
932 return 1;
933}
934EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
935
936/************************************************************************
937 * Transmit handling
938 ***********************************************************************/
939
940/* Build an L2TP header for the session into the buffer provided.
941 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000942static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000943{
James Chapmanf7faffa2010-04-02 06:18:49 +0000944 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000945 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000946 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000947 u16 flags = L2TP_HDR_VER_2;
948 u32 tunnel_id = tunnel->peer_tunnel_id;
949 u32 session_id = session->peer_session_id;
950
951 if (session->send_seq)
952 flags |= L2TP_HDRFLAG_S;
953
954 /* Setup L2TP header. */
955 *bufp++ = htons(flags);
956 *bufp++ = htons(tunnel_id);
957 *bufp++ = htons(session_id);
958 if (session->send_seq) {
959 *bufp++ = htons(session->ns);
960 *bufp++ = 0;
961 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000962 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000963 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
964 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000965 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000966
967 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000968}
969
James Chapmanf7faffa2010-04-02 06:18:49 +0000970static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000971{
James Chapman0d767512010-04-02 06:19:00 +0000972 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000973 char *bufp = buf;
974 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000975
James Chapman0d767512010-04-02 06:19:00 +0000976 /* Setup L2TP header. The header differs slightly for UDP and
977 * IP encapsulations. For UDP, there is 4 bytes of flags.
978 */
979 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
980 u16 flags = L2TP_HDR_VER_3;
981 *((__be16 *) bufp) = htons(flags);
982 bufp += 2;
983 *((__be16 *) bufp) = 0;
984 bufp += 2;
985 }
986
James Chapmanf7faffa2010-04-02 06:18:49 +0000987 *((__be32 *) bufp) = htonl(session->peer_session_id);
988 bufp += 4;
989 if (session->cookie_len) {
990 memcpy(bufp, &session->cookie[0], session->cookie_len);
991 bufp += session->cookie_len;
992 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100993 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
994 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000995
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100996 if (session->send_seq) {
997 l2h = 0x40000000 | session->ns;
998 session->ns++;
999 session->ns &= 0xffffff;
1000 l2tp_dbg(session, L2TP_MSG_SEQ,
1001 "%s: updated ns to %u\n",
1002 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001003 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001004
1005 *((__be32 *)bufp) = htonl(l2h);
1006 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001007 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001008
1009 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001010}
James Chapmanfd558d12010-04-02 06:18:33 +00001011
stephen hemmingerfc130842010-10-21 07:50:46 +00001012static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001013 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001014{
1015 struct l2tp_tunnel *tunnel = session->tunnel;
1016 unsigned int len = skb->len;
1017 int error;
1018
1019 /* Debug */
1020 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001021 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001022 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001023 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001024 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001025 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001026
1027 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001028 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1029 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001030
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001031 pr_debug("%s: xmit\n", session->name);
1032 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1033 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001034 }
1035
1036 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001037 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001038#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001039 if (l2tp_sk_is_v6(tunnel->sock))
Eric Dumazetb0270e92014-04-15 12:58:34 -04001040 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001041 else
1042#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001043 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001044
1045 /* Update stats */
1046 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001047 atomic_long_inc(&tunnel->stats.tx_packets);
1048 atomic_long_add(len, &tunnel->stats.tx_bytes);
1049 atomic_long_inc(&session->stats.tx_packets);
1050 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001051 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001052 atomic_long_inc(&tunnel->stats.tx_errors);
1053 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001054 }
1055
1056 return 0;
1057}
James Chapmanfd558d12010-04-02 06:18:33 +00001058
James Chapmanfd558d12010-04-02 06:18:33 +00001059/* If caller requires the skb to have a ppp header, the header must be
1060 * inserted in the skb data before calling this function.
1061 */
1062int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1063{
1064 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001065 struct l2tp_tunnel *tunnel = session->tunnel;
1066 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001067 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001068 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001069 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001070 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001071 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1072 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001073 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001074
1075 /* Check that there's enough headroom in the skb to insert IP,
1076 * UDP and L2TP headers. If not enough, expand it to
1077 * make room. Adjust truesize.
1078 */
1079 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001080 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001081 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001082 kfree_skb(skb);
1083 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001084 }
James Chapmanfd558d12010-04-02 06:18:33 +00001085
James Chapmanfd558d12010-04-02 06:18:33 +00001086 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001087 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001088
James Chapman0d767512010-04-02 06:19:00 +00001089 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001090 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1091 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1092 IPSKB_REROUTED);
1093 nf_reset(skb);
1094
David S. Miller6af88da2011-05-08 13:45:20 -07001095 bh_lock_sock(sk);
1096 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001097 kfree_skb(skb);
1098 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001099 goto out_unlock;
1100 }
1101
Paolo Abenib954f942018-03-12 14:54:24 +01001102 /* The user-space may change the connection status for the user-space
1103 * provided socket at run time: we must check it under the socket lock
1104 */
1105 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1106 kfree_skb(skb);
1107 ret = NET_XMIT_DROP;
1108 goto out_unlock;
1109 }
1110
James Chapmanfd558d12010-04-02 06:18:33 +00001111 /* Get routing info from the tunnel socket */
1112 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001113 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001114
David S. Millerd9d8da82011-05-06 22:23:20 -07001115 inet = inet_sk(sk);
1116 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001117 switch (tunnel->encap) {
1118 case L2TP_ENCAPTYPE_UDP:
1119 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001120 __skb_push(skb, sizeof(*uh));
1121 skb_reset_transport_header(skb);
1122 uh = udp_hdr(skb);
1123 uh->source = inet->inet_sport;
1124 uh->dest = inet->inet_dport;
1125 udp_len = uhlen + hdr_len + data_len;
1126 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001127
1128 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001129#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001130 if (l2tp_sk_is_v6(sk))
Tom Herbert77157e12014-06-04 17:19:56 -07001131 udp6_set_csum(udp_get_no_check6_tx(sk),
1132 skb, &inet6_sk(sk)->saddr,
1133 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001134 else
1135#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001136 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1137 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001138 break;
1139
1140 case L2TP_ENCAPTYPE_IP:
1141 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001142 }
1143
David S. Millerd9d8da82011-05-06 22:23:20 -07001144 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001145out_unlock:
1146 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001147
Eric Dumazetb8c84302012-06-28 20:15:13 +00001148 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001149}
1150EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1151
1152/*****************************************************************************
1153 * Tinnel and session create/destroy.
1154 *****************************************************************************/
1155
1156/* Tunnel socket destruct hook.
1157 * The tunnel context is deleted only when all session sockets have been
1158 * closed.
1159 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001160static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001161{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001162 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001163
James Chapmanfd558d12010-04-02 06:18:33 +00001164 if (tunnel == NULL)
1165 goto end;
1166
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001167 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001168
Tom Parkinf8ccac02013-01-31 23:43:00 +00001169 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001170 switch (tunnel->encap) {
1171 case L2TP_ENCAPTYPE_UDP:
1172 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1173 (udp_sk(sk))->encap_type = 0;
1174 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001175 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001176 break;
1177 case L2TP_ENCAPTYPE_IP:
1178 break;
1179 }
James Chapmanfd558d12010-04-02 06:18:33 +00001180
1181 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001182 sk->sk_destruct = tunnel->old_sk_destruct;
1183 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001184
James Chapmanfd558d12010-04-02 06:18:33 +00001185 /* Call the original destructor */
1186 if (sk->sk_destruct)
1187 (*sk->sk_destruct)(sk);
James Chapmand00fa9a2018-02-23 17:45:45 +00001188
1189 kfree_rcu(tunnel, rcu);
James Chapmanfd558d12010-04-02 06:18:33 +00001190end:
1191 return;
1192}
James Chapmanfd558d12010-04-02 06:18:33 +00001193
1194/* When the tunnel is closed, all the attached sessions need to go too.
1195 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001196void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001197{
1198 int hash;
1199 struct hlist_node *walk;
1200 struct hlist_node *tmp;
1201 struct l2tp_session *session;
1202
1203 BUG_ON(tunnel == NULL);
1204
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001205 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1206 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001207
1208 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001209 tunnel->acpt_newsess = false;
James Chapmanfd558d12010-04-02 06:18:33 +00001210 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1211again:
1212 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1213 session = hlist_entry(walk, struct l2tp_session, hlist);
1214
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001215 l2tp_info(session, L2TP_MSG_CONTROL,
1216 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001217
1218 hlist_del_init(&session->hlist);
1219
Guillaume Naultb228a942017-09-22 15:39:24 +02001220 if (test_and_set_bit(0, &session->dead))
1221 goto again;
1222
James Chapmanfd558d12010-04-02 06:18:33 +00001223 write_unlock_bh(&tunnel->hlist_lock);
1224
Tom Parkinf6e16b22013-03-19 06:11:23 +00001225 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001226 l2tp_session_queue_purge(session);
1227
James Chapmanfd558d12010-04-02 06:18:33 +00001228 if (session->session_close != NULL)
1229 (*session->session_close)(session);
1230
Tom Parkin9980d002013-03-19 06:11:13 +00001231 l2tp_session_dec_refcount(session);
1232
James Chapmanfd558d12010-04-02 06:18:33 +00001233 write_lock_bh(&tunnel->hlist_lock);
1234
1235 /* Now restart from the beginning of this hash
1236 * chain. We always remove a session from the
1237 * list so we are guaranteed to make forward
1238 * progress.
1239 */
1240 goto again;
1241 }
1242 }
1243 write_unlock_bh(&tunnel->hlist_lock);
1244}
Tom Parkine34f4c72013-03-19 06:11:14 +00001245EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001246
Tom Parkin9980d002013-03-19 06:11:13 +00001247/* Tunnel socket destroy hook for UDP encapsulation */
1248static void l2tp_udp_encap_destroy(struct sock *sk)
1249{
James Chapmand00fa9a2018-02-23 17:45:45 +00001250 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1251
1252 if (tunnel)
1253 l2tp_tunnel_delete(tunnel);
Tom Parkin9980d002013-03-19 06:11:13 +00001254}
1255
Tom Parkinf8ccac02013-01-31 23:43:00 +00001256/* Workqueue tunnel deletion function */
1257static void l2tp_tunnel_del_work(struct work_struct *work)
1258{
James Chapmand00fa9a2018-02-23 17:45:45 +00001259 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1260 del_work);
1261 struct sock *sk = tunnel->sock;
1262 struct socket *sock = sk->sk_socket;
James Chapman28f5bfb2018-02-23 17:45:47 +00001263 struct l2tp_net *pn;
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001264
1265 l2tp_tunnel_closeall(tunnel);
1266
James Chapman76a6abd2018-02-23 17:45:43 +00001267 /* If the tunnel socket was created within the kernel, use
Tom Parkin02d13ed2013-03-19 06:11:18 +00001268 * the sk API to release it here.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001269 */
James Chapman76a6abd2018-02-23 17:45:43 +00001270 if (tunnel->fd < 0) {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001271 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001272 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001273 sock_release(sock);
1274 }
Tom Parkin167eb172013-01-31 23:43:03 +00001275 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001276
James Chapman28f5bfb2018-02-23 17:45:47 +00001277 /* Remove the tunnel struct from the tunnel list */
1278 pn = l2tp_pernet(tunnel->l2tp_net);
1279 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1280 list_del_rcu(&tunnel->list);
1281 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1282
James Chapmand00fa9a2018-02-23 17:45:45 +00001283 /* drop initial ref */
1284 l2tp_tunnel_dec_refcount(tunnel);
1285
1286 /* drop workqueue ref */
Alexander Couzens06a15f52015-09-28 11:32:42 +02001287 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001288}
James Chapmanfd558d12010-04-02 06:18:33 +00001289
James Chapman789a4a22010-04-02 06:19:40 +00001290/* Create a socket for the tunnel, if one isn't set up by
1291 * userspace. This is used for static tunnels where there is no
1292 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001293 *
1294 * Since we don't want these sockets to keep a namespace alive by
1295 * themselves, we drop the socket's namespace refcount after creation.
1296 * These sockets are freed when the namespace exits using the pernet
1297 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001298 */
Tom Parkin167eb172013-01-31 23:43:03 +00001299static int l2tp_tunnel_sock_create(struct net *net,
1300 u32 tunnel_id,
1301 u32 peer_tunnel_id,
1302 struct l2tp_tunnel_cfg *cfg,
1303 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001304{
1305 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001306 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001307 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001308
1309 switch (cfg->encap) {
1310 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001311 memset(&udp_conf, 0, sizeof(udp_conf));
1312
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001313#if IS_ENABLED(CONFIG_IPV6)
1314 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001315 udp_conf.family = AF_INET6;
1316 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1317 sizeof(udp_conf.local_ip6));
1318 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1319 sizeof(udp_conf.peer_ip6));
1320 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001321 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001322 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001323 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001324 } else
1325#endif
1326 {
Tom Herbert85644b42014-07-13 19:49:48 -07001327 udp_conf.family = AF_INET;
1328 udp_conf.local_ip = cfg->local_ip;
1329 udp_conf.peer_ip = cfg->peer_ip;
1330 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001331 }
James Chapman789a4a22010-04-02 06:19:40 +00001332
Tom Herbert85644b42014-07-13 19:49:48 -07001333 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1334 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1335
1336 err = udp_sock_create(net, &udp_conf, &sock);
1337 if (err < 0)
1338 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001339
1340 break;
1341
1342 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001343#if IS_ENABLED(CONFIG_IPV6)
1344 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001345 struct sockaddr_l2tpip6 ip6_addr = {0};
1346
Eric W. Biederman26abe142015-05-08 21:10:31 -05001347 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001348 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001349 if (err < 0)
1350 goto out;
1351
James Chapman5dac94e2012-04-29 21:48:55 +00001352 ip6_addr.l2tp_family = AF_INET6;
1353 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1354 sizeof(ip6_addr.l2tp_addr));
1355 ip6_addr.l2tp_conn_id = tunnel_id;
1356 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1357 sizeof(ip6_addr));
1358 if (err < 0)
1359 goto out;
1360
1361 ip6_addr.l2tp_family = AF_INET6;
1362 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1363 sizeof(ip6_addr.l2tp_addr));
1364 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1365 err = kernel_connect(sock,
1366 (struct sockaddr *) &ip6_addr,
1367 sizeof(ip6_addr), 0);
1368 if (err < 0)
1369 goto out;
1370 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001371#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001372 {
Tom Herbert85644b42014-07-13 19:49:48 -07001373 struct sockaddr_l2tpip ip_addr = {0};
1374
Eric W. Biederman26abe142015-05-08 21:10:31 -05001375 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001376 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001377 if (err < 0)
1378 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001379
James Chapman5dac94e2012-04-29 21:48:55 +00001380 ip_addr.l2tp_family = AF_INET;
1381 ip_addr.l2tp_addr = cfg->local_ip;
1382 ip_addr.l2tp_conn_id = tunnel_id;
1383 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1384 sizeof(ip_addr));
1385 if (err < 0)
1386 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001387
James Chapman5dac94e2012-04-29 21:48:55 +00001388 ip_addr.l2tp_family = AF_INET;
1389 ip_addr.l2tp_addr = cfg->peer_ip;
1390 ip_addr.l2tp_conn_id = peer_tunnel_id;
1391 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1392 sizeof(ip_addr), 0);
1393 if (err < 0)
1394 goto out;
1395 }
James Chapman789a4a22010-04-02 06:19:40 +00001396 break;
1397
1398 default:
1399 goto out;
1400 }
1401
1402out:
Tom Parkin167eb172013-01-31 23:43:03 +00001403 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001404 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001405 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001406 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001407 *sockp = NULL;
1408 }
1409
1410 return err;
1411}
1412
Eric Dumazet37159ef2012-09-04 07:18:57 +00001413static struct lock_class_key l2tp_socket_class;
1414
James Chapmanfd558d12010-04-02 06:18:33 +00001415int 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)
1416{
1417 struct l2tp_tunnel *tunnel = NULL;
1418 int err;
James Chapman0d767512010-04-02 06:19:00 +00001419 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001420
James Chapman0d767512010-04-02 06:19:00 +00001421 if (cfg != NULL)
1422 encap = cfg->encap;
1423
James Chapmanfd558d12010-04-02 06:18:33 +00001424 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1425 if (tunnel == NULL) {
1426 err = -ENOMEM;
1427 goto err;
1428 }
1429
1430 tunnel->version = version;
1431 tunnel->tunnel_id = tunnel_id;
1432 tunnel->peer_tunnel_id = peer_tunnel_id;
1433 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1434
1435 tunnel->magic = L2TP_TUNNEL_MAGIC;
1436 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1437 rwlock_init(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001438 tunnel->acpt_newsess = true;
James Chapmanfd558d12010-04-02 06:18:33 +00001439
James Chapman0d767512010-04-02 06:19:00 +00001440 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001441 tunnel->debug = cfg->debug;
1442
James Chapman0d767512010-04-02 06:19:00 +00001443 tunnel->encap = encap;
James Chapmanfd558d12010-04-02 06:18:33 +00001444
James Chapmand00fa9a2018-02-23 17:45:45 +00001445 refcount_set(&tunnel->ref_count, 1);
James Chapmand00fa9a2018-02-23 17:45:45 +00001446 tunnel->fd = fd;
1447
Tom Parkinf8ccac02013-01-31 23:43:00 +00001448 /* Init delete workqueue struct */
1449 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1450
James Chapmanfd558d12010-04-02 06:18:33 +00001451 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001452
1453 err = 0;
1454err:
1455 if (tunnelp)
1456 *tunnelp = tunnel;
1457
James Chapmanfd558d12010-04-02 06:18:33 +00001458 return err;
1459}
1460EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1461
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001462static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1463 enum l2tp_encap_type encap)
1464{
1465 if (!net_eq(sock_net(sk), net))
1466 return -EINVAL;
1467
1468 if (sk->sk_type != SOCK_DGRAM)
1469 return -EPROTONOSUPPORT;
1470
1471 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1472 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1473 return -EPROTONOSUPPORT;
1474
1475 if (sk->sk_user_data)
1476 return -EBUSY;
1477
1478 return 0;
1479}
1480
1481int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1482 struct l2tp_tunnel_cfg *cfg)
1483{
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001484 struct l2tp_tunnel *tunnel_walk;
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001485 struct l2tp_net *pn;
1486 struct socket *sock;
1487 struct sock *sk;
1488 int ret;
1489
1490 if (tunnel->fd < 0) {
1491 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1492 tunnel->peer_tunnel_id, cfg,
1493 &sock);
1494 if (ret < 0)
1495 goto err;
1496 } else {
1497 sock = sockfd_lookup(tunnel->fd, &ret);
1498 if (!sock)
1499 goto err;
1500
1501 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1502 if (ret < 0)
1503 goto err_sock;
1504 }
1505
1506 sk = sock->sk;
1507
1508 sock_hold(sk);
1509 tunnel->sock = sk;
1510 tunnel->l2tp_net = net;
1511
1512 pn = l2tp_pernet(net);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001513
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001514 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001515 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1516 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1517 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1518
1519 ret = -EEXIST;
1520 goto err_sock;
1521 }
1522 }
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001523 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1524 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1525
1526 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1527 struct udp_tunnel_sock_cfg udp_cfg = {
1528 .sk_user_data = tunnel,
1529 .encap_type = UDP_ENCAP_L2TPINUDP,
1530 .encap_rcv = l2tp_udp_encap_recv,
1531 .encap_destroy = l2tp_udp_encap_destroy,
1532 };
1533
1534 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1535 } else {
1536 sk->sk_user_data = tunnel;
1537 }
1538
1539 tunnel->old_sk_destruct = sk->sk_destruct;
1540 sk->sk_destruct = &l2tp_tunnel_destruct;
1541 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1542 "l2tp_sock");
1543 sk->sk_allocation = GFP_ATOMIC;
1544
1545 if (tunnel->fd >= 0)
1546 sockfd_put(sock);
1547
1548 return 0;
1549
1550err_sock:
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001551 if (tunnel->fd < 0)
1552 sock_release(sock);
1553 else
1554 sockfd_put(sock);
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001555err:
1556 return ret;
1557}
1558EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1559
James Chapman309795f2010-04-02 06:19:10 +00001560/* This function is used by the netlink TUNNEL_DELETE command.
1561 */
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001562void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001563{
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001564 if (!test_and_set_bit(0, &tunnel->dead)) {
1565 l2tp_tunnel_inc_refcount(tunnel);
1566 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001567 }
James Chapman309795f2010-04-02 06:19:10 +00001568}
1569EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1570
James Chapmanfd558d12010-04-02 06:18:33 +00001571/* Really kill the session.
1572 */
1573void l2tp_session_free(struct l2tp_session *session)
1574{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001575 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001576
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001577 BUG_ON(refcount_read(&session->ref_count) != 0);
James Chapmanfd558d12010-04-02 06:18:33 +00001578
Tom Parkinf6e16b22013-03-19 06:11:23 +00001579 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001580 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001581 l2tp_tunnel_dec_refcount(tunnel);
1582 }
1583
1584 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001585}
1586EXPORT_SYMBOL_GPL(l2tp_session_free);
1587
Tom Parkinf6e16b22013-03-19 06:11:23 +00001588/* Remove an l2tp session from l2tp_core's hash lists.
1589 * Provides a tidyup interface for pseudowire code which can't just route all
1590 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1591 * callback.
1592 */
1593void __l2tp_session_unhash(struct l2tp_session *session)
1594{
1595 struct l2tp_tunnel *tunnel = session->tunnel;
1596
1597 /* Remove the session from core hashes */
1598 if (tunnel) {
1599 /* Remove from the per-tunnel hash */
1600 write_lock_bh(&tunnel->hlist_lock);
1601 hlist_del_init(&session->hlist);
1602 write_unlock_bh(&tunnel->hlist_lock);
1603
1604 /* For L2TPv3 we have a per-net hash: remove from there, too */
1605 if (tunnel->version != L2TP_HDR_VER_2) {
1606 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1607 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1608 hlist_del_init_rcu(&session->global_hlist);
1609 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1610 synchronize_rcu();
1611 }
1612 }
1613}
1614EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1615
James Chapman309795f2010-04-02 06:19:10 +00001616/* This function is used by the netlink SESSION_DELETE command and by
1617 pseudowire modules.
1618 */
1619int l2tp_session_delete(struct l2tp_session *session)
1620{
Guillaume Naultb228a942017-09-22 15:39:24 +02001621 if (test_and_set_bit(0, &session->dead))
1622 return 0;
1623
Tom Parkinf6e16b22013-03-19 06:11:23 +00001624 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001625 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001626 if (session->session_close != NULL)
1627 (*session->session_close)(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001628
James Chapman309795f2010-04-02 06:19:10 +00001629 l2tp_session_dec_refcount(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001630
James Chapman309795f2010-04-02 06:19:10 +00001631 return 0;
1632}
1633EXPORT_SYMBOL_GPL(l2tp_session_delete);
1634
James Chapmanf7faffa2010-04-02 06:18:49 +00001635/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001636 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001637 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001638void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001639{
1640 if (version == L2TP_HDR_VER_2) {
1641 session->hdr_len = 6;
1642 if (session->send_seq)
1643 session->hdr_len += 4;
1644 } else {
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001645 session->hdr_len = 4 + session->cookie_len;
1646 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001647 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1648 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001649 }
1650
1651}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001652EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001653
James Chapmanfd558d12010-04-02 06:18:33 +00001654struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1655{
1656 struct l2tp_session *session;
1657
1658 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1659 if (session != NULL) {
1660 session->magic = L2TP_SESSION_MAGIC;
1661 session->tunnel = tunnel;
1662
1663 session->session_id = session_id;
1664 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001665 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001666 if (tunnel->version == L2TP_HDR_VER_2)
1667 session->nr_max = 0xffff;
1668 else
1669 session->nr_max = 0xffffff;
1670 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001671 session->nr_oos_count_max = 4;
1672
1673 /* Use NR of first received packet */
1674 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001675
1676 sprintf(&session->name[0], "sess %u/%u",
1677 tunnel->tunnel_id, session->session_id);
1678
1679 skb_queue_head_init(&session->reorder_q);
1680
1681 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001682 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001683
1684 /* Inherit debug options from tunnel */
1685 session->debug = tunnel->debug;
1686
1687 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001688 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001689 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001690 session->mtu = cfg->mtu;
1691 session->mru = cfg->mru;
1692 session->send_seq = cfg->send_seq;
1693 session->recv_seq = cfg->recv_seq;
1694 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001695 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001696 session->l2specific_type = cfg->l2specific_type;
James Chapmanf7faffa2010-04-02 06:18:49 +00001697 session->cookie_len = cfg->cookie_len;
1698 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1699 session->peer_cookie_len = cfg->peer_cookie_len;
1700 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001701 }
1702
James Chapmanf7faffa2010-04-02 06:18:49 +00001703 if (tunnel->version == L2TP_HDR_VER_2)
1704 session->build_header = l2tp_build_l2tpv2_header;
1705 else
1706 session->build_header = l2tp_build_l2tpv3_header;
1707
1708 l2tp_session_set_header_len(session, tunnel->version);
1709
Guillaume Nault9ee369a2017-08-25 16:22:17 +02001710 refcount_set(&session->ref_count, 1);
1711
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001712 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001713 }
1714
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001715 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001716}
1717EXPORT_SYMBOL_GPL(l2tp_session_create);
1718
1719/*****************************************************************************
1720 * Init and cleanup
1721 *****************************************************************************/
1722
1723static __net_init int l2tp_init_net(struct net *net)
1724{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001725 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001726 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001727
James Chapmanfd558d12010-04-02 06:18:33 +00001728 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001729 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001730
James Chapmanf7faffa2010-04-02 06:18:49 +00001731 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1732 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1733
James Chapmane02d4942010-04-02 06:19:16 +00001734 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001735
James Chapmanfd558d12010-04-02 06:18:33 +00001736 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001737}
1738
Tom Parkin167eb172013-01-31 23:43:03 +00001739static __net_exit void l2tp_exit_net(struct net *net)
1740{
1741 struct l2tp_net *pn = l2tp_pernet(net);
1742 struct l2tp_tunnel *tunnel = NULL;
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001743 int hash;
Tom Parkin167eb172013-01-31 23:43:03 +00001744
1745 rcu_read_lock_bh();
1746 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slaby4dc12ff2017-10-25 15:57:55 +02001747 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001748 }
1749 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001750
1751 flush_workqueue(l2tp_wq);
1752 rcu_barrier();
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001753
1754 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1755 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
Tom Parkin167eb172013-01-31 23:43:03 +00001756}
1757
James Chapmanfd558d12010-04-02 06:18:33 +00001758static struct pernet_operations l2tp_net_ops = {
1759 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001760 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001761 .id = &l2tp_net_id,
1762 .size = sizeof(struct l2tp_net),
1763};
1764
1765static int __init l2tp_init(void)
1766{
1767 int rc = 0;
1768
1769 rc = register_pernet_device(&l2tp_net_ops);
1770 if (rc)
1771 goto out;
1772
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001773 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001774 if (!l2tp_wq) {
1775 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001776 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001777 rc = -ENOMEM;
1778 goto out;
1779 }
1780
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001781 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001782
1783out:
1784 return rc;
1785}
1786
1787static void __exit l2tp_exit(void)
1788{
1789 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001790 if (l2tp_wq) {
1791 destroy_workqueue(l2tp_wq);
1792 l2tp_wq = NULL;
1793 }
James Chapmanfd558d12010-04-02 06:18:33 +00001794}
1795
1796module_init(l2tp_init);
1797module_exit(l2tp_exit);
1798
1799MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1800MODULE_DESCRIPTION("L2TP core");
1801MODULE_LICENSE("GPL");
1802MODULE_VERSION(L2TP_DRV_VERSION);
1803