blob: 0fa53ead24aae689336b4edab06be2e15fdb2265 [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
stephen hemmingerfc130842010-10-21 07:50:46 +0000114
David S. Miller8d8a51e2013-10-08 15:44:26 -0400115static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
116{
117 return sk->sk_user_data;
118}
119
Guillaume Nault9aaef502017-04-12 10:05:29 +0200120static inline struct l2tp_net *l2tp_pernet(const struct net *net)
James Chapmanfd558d12010-04-02 06:18:33 +0000121{
122 BUG_ON(!net);
123
124 return net_generic(net, l2tp_net_id);
125}
126
James Chapmanf7faffa2010-04-02 06:18:49 +0000127/* Session hash global list for L2TPv3.
128 * The session_id SHOULD be random according to RFC3931, but several
129 * L2TP implementations use incrementing session_ids. So we do a real
130 * hash on the session_id, rather than a simple bitmask.
131 */
132static inline struct hlist_head *
133l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
134{
135 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
136
137}
138
James Chapmanfd558d12010-04-02 06:18:33 +0000139/* Session hash list.
140 * The session_id SHOULD be random according to RFC2661, but several
141 * L2TP implementations (Cisco and Microsoft) use incrementing
142 * session_ids. So we do a real hash on the session_id, rather than a
143 * simple bitmask.
144 */
145static inline struct hlist_head *
146l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
147{
148 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
149}
150
James Chapmand00fa9a2018-02-23 17:45:45 +0000151void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
152{
153 sock_put(tunnel->sock);
154 /* the tunnel is freed in the socket destructor */
155}
156EXPORT_SYMBOL(l2tp_tunnel_free);
157
Guillaume Nault54652eb2017-08-25 16:51:40 +0200158/* Lookup a tunnel. A new reference is held on the returned tunnel. */
159struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
160{
161 const struct l2tp_net *pn = l2tp_pernet(net);
162 struct l2tp_tunnel *tunnel;
163
164 rcu_read_lock_bh();
165 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
166 if (tunnel->tunnel_id == tunnel_id) {
167 l2tp_tunnel_inc_refcount(tunnel);
168 rcu_read_unlock_bh();
169
170 return tunnel;
171 }
172 }
173 rcu_read_unlock_bh();
174
175 return NULL;
176}
177EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
178
Guillaume Naulta4346212017-10-31 17:36:42 +0100179/* Lookup a session. A new reference is held on the returned session. */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200180struct l2tp_session *l2tp_session_get(const struct net *net,
Guillaume Nault61b9a042017-03-31 13:02:25 +0200181 struct l2tp_tunnel *tunnel,
Guillaume Naulta4346212017-10-31 17:36:42 +0100182 u32 session_id)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200183{
184 struct hlist_head *session_list;
185 struct l2tp_session *session;
186
187 if (!tunnel) {
188 struct l2tp_net *pn = l2tp_pernet(net);
189
190 session_list = l2tp_session_id_hash_2(pn, session_id);
191
192 rcu_read_lock_bh();
193 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
194 if (session->session_id == session_id) {
195 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200196 rcu_read_unlock_bh();
197
198 return session;
199 }
200 }
201 rcu_read_unlock_bh();
202
203 return NULL;
204 }
205
206 session_list = l2tp_session_id_hash(tunnel, session_id);
207 read_lock_bh(&tunnel->hlist_lock);
208 hlist_for_each_entry(session, session_list, hlist) {
209 if (session->session_id == session_id) {
210 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200211 read_unlock_bh(&tunnel->hlist_lock);
212
213 return session;
214 }
215 }
216 read_unlock_bh(&tunnel->hlist_lock);
217
218 return NULL;
219}
220EXPORT_SYMBOL_GPL(l2tp_session_get);
221
Guillaume Naulta4346212017-10-31 17:36:42 +0100222struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000223{
224 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000225 struct l2tp_session *session;
226 int count = 0;
227
228 read_lock_bh(&tunnel->hlist_lock);
229 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800230 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000231 if (++count > nth) {
Guillaume Naulte08293a2017-04-03 12:03:13 +0200232 l2tp_session_inc_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000233 read_unlock_bh(&tunnel->hlist_lock);
234 return session;
235 }
236 }
237 }
238
239 read_unlock_bh(&tunnel->hlist_lock);
240
241 return NULL;
242}
Guillaume Naulte08293a2017-04-03 12:03:13 +0200243EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000244
James Chapman309795f2010-04-02 06:19:10 +0000245/* Lookup a session by interface name.
246 * This is very inefficient but is only used by management interfaces.
247 */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200248struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
Guillaume Naulta4346212017-10-31 17:36:42 +0100249 const char *ifname)
James Chapman309795f2010-04-02 06:19:10 +0000250{
251 struct l2tp_net *pn = l2tp_pernet(net);
252 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000253 struct l2tp_session *session;
254
James Chapmane02d4942010-04-02 06:19:16 +0000255 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000256 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000258 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200259 l2tp_session_inc_refcount(session);
James Chapmane02d4942010-04-02 06:19:16 +0000260 rcu_read_unlock_bh();
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200261
James Chapman309795f2010-04-02 06:19:10 +0000262 return session;
263 }
264 }
265 }
266
James Chapmane02d4942010-04-02 06:19:16 +0000267 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000268
269 return NULL;
270}
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200271EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000272
Guillaume Nault3953ae72017-10-27 16:51:50 +0200273int l2tp_session_register(struct l2tp_session *session,
274 struct l2tp_tunnel *tunnel)
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200275{
276 struct l2tp_session *session_walk;
277 struct hlist_head *g_head;
278 struct hlist_head *head;
279 struct l2tp_net *pn;
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200280 int err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200281
282 head = l2tp_session_id_hash(tunnel, session->session_id);
283
284 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200285 if (!tunnel->acpt_newsess) {
286 err = -ENODEV;
287 goto err_tlock;
288 }
289
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200290 hlist_for_each_entry(session_walk, head, hlist)
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200291 if (session_walk->session_id == session->session_id) {
292 err = -EEXIST;
293 goto err_tlock;
294 }
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200295
296 if (tunnel->version == L2TP_HDR_VER_3) {
297 pn = l2tp_pernet(tunnel->l2tp_net);
298 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
299 session->session_id);
300
301 spin_lock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200302
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200303 hlist_for_each_entry(session_walk, g_head, global_hlist)
304 if (session_walk->session_id == session->session_id) {
305 err = -EEXIST;
306 goto err_tlock_pnlock;
307 }
308
309 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200310 hlist_add_head_rcu(&session->global_hlist, g_head);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200311
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200312 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200313 } else {
314 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200315 }
316
317 hlist_add_head(&session->hlist, head);
318 write_unlock_bh(&tunnel->hlist_lock);
319
320 return 0;
321
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200322err_tlock_pnlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200323 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200324err_tlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200325 write_unlock_bh(&tunnel->hlist_lock);
326
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200327 return err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200328}
Guillaume Nault3953ae72017-10-27 16:51:50 +0200329EXPORT_SYMBOL_GPL(l2tp_session_register);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200330
James Chapmanfd558d12010-04-02 06:18:33 +0000331/* Lookup a tunnel by id
332 */
Guillaume Nault2f858b92017-04-12 10:05:30 +0200333struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000334{
335 struct l2tp_tunnel *tunnel;
336 struct l2tp_net *pn = l2tp_pernet(net);
337
James Chapmane02d4942010-04-02 06:19:16 +0000338 rcu_read_lock_bh();
339 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000340 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000341 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000342 return tunnel;
343 }
344 }
James Chapmane02d4942010-04-02 06:19:16 +0000345 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000346
347 return NULL;
348}
349EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
350
Guillaume Nault2f858b92017-04-12 10:05:30 +0200351struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000352{
353 struct l2tp_net *pn = l2tp_pernet(net);
354 struct l2tp_tunnel *tunnel;
355 int count = 0;
356
James Chapmane02d4942010-04-02 06:19:16 +0000357 rcu_read_lock_bh();
358 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000359 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000360 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000361 return tunnel;
362 }
363 }
364
James Chapmane02d4942010-04-02 06:19:16 +0000365 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000366
367 return NULL;
368}
369EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
370
371/*****************************************************************************
372 * Receive data handling
373 *****************************************************************************/
374
375/* Queue a skb in order. We come here only if the skb has an L2TP sequence
376 * number.
377 */
378static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
379{
380 struct sk_buff *skbp;
381 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000382 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000383
384 spin_lock_bh(&session->reorder_q.lock);
385 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
386 if (L2TP_SKB_CB(skbp)->ns > ns) {
387 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000388 l2tp_dbg(session, L2TP_MSG_SEQ,
389 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
390 session->name, ns, L2TP_SKB_CB(skbp)->ns,
391 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000392 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000393 goto out;
394 }
395 }
396
397 __skb_queue_tail(&session->reorder_q, skb);
398
399out:
400 spin_unlock_bh(&session->reorder_q.lock);
401}
402
403/* Dequeue a single skb.
404 */
405static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
406{
407 struct l2tp_tunnel *tunnel = session->tunnel;
408 int length = L2TP_SKB_CB(skb)->length;
409
410 /* We're about to requeue the skb, so return resources
411 * to its current owner (a socket receive buffer).
412 */
413 skb_orphan(skb);
414
Tom Parkin7b7c0712013-03-19 06:11:22 +0000415 atomic_long_inc(&tunnel->stats.rx_packets);
416 atomic_long_add(length, &tunnel->stats.rx_bytes);
417 atomic_long_inc(&session->stats.rx_packets);
418 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000419
420 if (L2TP_SKB_CB(skb)->has_seq) {
421 /* Bump our Nr */
422 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100423 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000424
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000425 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
426 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000427 }
428
429 /* call private receive handler */
430 if (session->recv_skb != NULL)
431 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
432 else
433 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000434}
435
436/* Dequeue skbs from the session's reorder_q, subject to packet order.
437 * Skbs that have been in the queue for too long are simply discarded.
438 */
439static void l2tp_recv_dequeue(struct l2tp_session *session)
440{
441 struct sk_buff *skb;
442 struct sk_buff *tmp;
443
444 /* If the pkt at the head of the queue has the nr that we
445 * expect to send up next, dequeue it and any other
446 * in-sequence packets behind it.
447 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000448start:
James Chapmanfd558d12010-04-02 06:18:33 +0000449 spin_lock_bh(&session->reorder_q.lock);
450 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
451 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000452 atomic_long_inc(&session->stats.rx_seq_discards);
453 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000454 l2tp_dbg(session, L2TP_MSG_SEQ,
455 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
456 session->name, L2TP_SKB_CB(skb)->ns,
457 L2TP_SKB_CB(skb)->length, session->nr,
458 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000459 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000460 __skb_unlink(skb, &session->reorder_q);
461 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000462 continue;
463 }
464
465 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000466 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000467 l2tp_dbg(session, L2TP_MSG_SEQ,
468 "%s: advancing nr to next pkt: %u -> %u",
469 session->name, session->nr,
470 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000471 session->reorder_skip = 0;
472 session->nr = L2TP_SKB_CB(skb)->ns;
473 }
James Chapmanfd558d12010-04-02 06:18:33 +0000474 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000475 l2tp_dbg(session, L2TP_MSG_SEQ,
476 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
477 session->name, L2TP_SKB_CB(skb)->ns,
478 L2TP_SKB_CB(skb)->length, session->nr,
479 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000480 goto out;
481 }
482 }
483 __skb_unlink(skb, &session->reorder_q);
484
485 /* Process the skb. We release the queue lock while we
486 * do so to let other contexts process the queue.
487 */
488 spin_unlock_bh(&session->reorder_q.lock);
489 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000490 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000491 }
492
493out:
494 spin_unlock_bh(&session->reorder_q.lock);
495}
496
James Chapman8a1631d2013-07-02 20:28:59 +0100497static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
498{
499 u32 nws;
500
501 if (nr >= session->nr)
502 nws = nr - session->nr;
503 else
504 nws = (session->nr_max + 1) - (session->nr - nr);
505
506 return nws < session->nr_window_size;
507}
508
James Chapmanb6dc01a2013-07-02 20:28:58 +0100509/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
510 * acceptable, else non-zero.
511 */
512static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
513{
James Chapman8a1631d2013-07-02 20:28:59 +0100514 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
515 /* Packet sequence number is outside allowed window.
516 * Discard it.
517 */
518 l2tp_dbg(session, L2TP_MSG_SEQ,
519 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
520 session->name, L2TP_SKB_CB(skb)->ns,
521 L2TP_SKB_CB(skb)->length, session->nr);
522 goto discard;
523 }
524
James Chapmanb6dc01a2013-07-02 20:28:58 +0100525 if (session->reorder_timeout != 0) {
526 /* Packet reordering enabled. Add skb to session's
527 * reorder queue, in order of ns.
528 */
529 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100530 goto out;
531 }
532
533 /* Packet reordering disabled. Discard out-of-sequence packets, while
534 * tracking the number if in-sequence packets after the first OOS packet
535 * is seen. After nr_oos_count_max in-sequence packets, reset the
536 * sequence number to re-enable packet reception.
537 */
538 if (L2TP_SKB_CB(skb)->ns == session->nr) {
539 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100540 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100541 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
542 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
543
544 if (nr_oos == nr_next)
545 session->nr_oos_count++;
546 else
547 session->nr_oos_count = 0;
548
549 session->nr_oos = nr_oos;
550 if (session->nr_oos_count > session->nr_oos_count_max) {
551 session->reorder_skip = 1;
552 l2tp_dbg(session, L2TP_MSG_SEQ,
553 "%s: %d oos packets received. Resetting sequence numbers\n",
554 session->name, session->nr_oos_count);
555 }
556 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100557 atomic_long_inc(&session->stats.rx_seq_discards);
558 l2tp_dbg(session, L2TP_MSG_SEQ,
559 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
560 session->name, L2TP_SKB_CB(skb)->ns,
561 L2TP_SKB_CB(skb)->length, session->nr,
562 skb_queue_len(&session->reorder_q));
563 goto discard;
564 }
565 skb_queue_tail(&session->reorder_q, skb);
566 }
567
James Chapmana0dbd822013-07-02 20:29:00 +0100568out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100569 return 0;
570
571discard:
572 return 1;
573}
574
James Chapmanf7faffa2010-04-02 06:18:49 +0000575/* Do receive processing of L2TP data frames. We handle both L2TPv2
576 * and L2TPv3 data frames here.
577 *
578 * L2TPv2 Data Message Header
579 *
580 * 0 1 2 3
581 * 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
582 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
583 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 * | Tunnel ID | Session ID |
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | Ns (opt) | Nr (opt) |
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * | Offset Size (opt) | Offset pad... (opt)
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 *
592 * Data frames are marked by T=0. All other fields are the same as
593 * those in L2TP control frames.
594 *
595 * L2TPv3 Data Message Header
596 *
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * | L2TP Session Header |
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * | L2-Specific Sublayer |
601 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 * | Tunnel Payload ...
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 *
605 * L2TPv3 Session Header Over IP
606 *
607 * 0 1 2 3
608 * 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
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 * | Session ID |
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * | Cookie (optional, maximum 64 bits)...
613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 * |
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 *
617 * L2TPv3 L2-Specific Sublayer Format
618 *
619 * 0 1 2 3
620 * 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
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
622 * |x|S|x|x|x|x|x|x| Sequence Number |
623 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
624 *
Guillaume Nault23fe8462018-01-05 19:47:14 +0100625 * Cookie value and sublayer format are negotiated with the peer when
626 * the session is set up. Unlike L2TPv2, we do not need to parse the
627 * packet header to determine if optional fields are present.
James Chapmanf7faffa2010-04-02 06:18:49 +0000628 *
629 * Caller must already have parsed the frame and determined that it is
630 * a data (not control) frame before coming here. Fields up to the
631 * session-id have already been parsed and ptr points to the data
632 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000633 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000634void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
635 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
636 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000637{
James Chapmanf7faffa2010-04-02 06:18:49 +0000638 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000639 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000640 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000641
James Chapmanf7faffa2010-04-02 06:18:49 +0000642 /* Parse and check optional cookie */
643 if (session->peer_cookie_len > 0) {
644 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000645 l2tp_info(tunnel, L2TP_MSG_DATA,
646 "%s: cookie mismatch (%u/%u). Discarding.\n",
647 tunnel->name, tunnel->tunnel_id,
648 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000649 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000650 goto discard;
651 }
652 ptr += session->peer_cookie_len;
653 }
654
James Chapmanfd558d12010-04-02 06:18:33 +0000655 /* Handle the optional sequence numbers. Sequence numbers are
656 * in different places for L2TPv2 and L2TPv3.
657 *
658 * If we are the LAC, enable/disable sequence numbers under
659 * the control of the LNS. If no sequence numbers present but
660 * we were expecting them, discard frame.
661 */
662 ns = nr = 0;
663 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000664 if (tunnel->version == L2TP_HDR_VER_2) {
665 if (hdrflags & L2TP_HDRFLAG_S) {
666 ns = ntohs(*(__be16 *) ptr);
667 ptr += 2;
668 nr = ntohs(*(__be16 *) ptr);
669 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000670
James Chapmanf7faffa2010-04-02 06:18:49 +0000671 /* Store L2TP info in the skb */
672 L2TP_SKB_CB(skb)->ns = ns;
673 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000674
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000675 l2tp_dbg(session, L2TP_MSG_SEQ,
676 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
677 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000678 }
679 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
680 u32 l2h = ntohl(*(__be32 *) ptr);
681
682 if (l2h & 0x40000000) {
683 ns = l2h & 0x00ffffff;
684
685 /* Store L2TP info in the skb */
686 L2TP_SKB_CB(skb)->ns = ns;
687 L2TP_SKB_CB(skb)->has_seq = 1;
688
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000689 l2tp_dbg(session, L2TP_MSG_SEQ,
690 "%s: recv data ns=%u, session nr=%u\n",
691 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000692 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100693 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000694 }
695
696 if (L2TP_SKB_CB(skb)->has_seq) {
697 /* Received a packet with sequence numbers. If we're the LNS,
698 * check if we sre sending sequence numbers and if not,
699 * configure it so.
700 */
701 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000702 l2tp_info(session, L2TP_MSG_SEQ,
703 "%s: requested to enable seq numbers by LNS\n",
704 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000705 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000706 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000707 }
708 } else {
709 /* No sequence numbers.
710 * If user has configured mandatory sequence numbers, discard.
711 */
712 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000713 l2tp_warn(session, L2TP_MSG_SEQ,
714 "%s: recv data has no seq numbers when required. Discarding.\n",
715 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000716 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000717 goto discard;
718 }
719
720 /* If we're the LAC and we're sending sequence numbers, the
721 * LNS has requested that we no longer send sequence numbers.
722 * If we're the LNS and we're sending sequence numbers, the
723 * LAC is broken. Discard the frame.
724 */
725 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000726 l2tp_info(session, L2TP_MSG_SEQ,
727 "%s: requested to disable seq numbers by LNS\n",
728 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000729 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000730 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000731 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000732 l2tp_warn(session, L2TP_MSG_SEQ,
733 "%s: recv data has no seq numbers when required. Discarding.\n",
734 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000735 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000736 goto discard;
737 }
738 }
739
James Chapman900631e2018-01-03 22:48:06 +0000740 /* Session data offset is defined only for L2TPv2 and is
741 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000742 */
743 if (tunnel->version == L2TP_HDR_VER_2) {
744 /* If offset bit set, skip it. */
745 if (hdrflags & L2TP_HDRFLAG_O) {
746 offset = ntohs(*(__be16 *)ptr);
747 ptr += 2 + offset;
748 }
James Chapman900631e2018-01-03 22:48:06 +0000749 }
James Chapmanfd558d12010-04-02 06:18:33 +0000750
751 offset = ptr - optr;
752 if (!pskb_may_pull(skb, offset))
753 goto discard;
754
755 __skb_pull(skb, offset);
756
757 /* If caller wants to process the payload before we queue the
758 * packet, do so now.
759 */
760 if (payload_hook)
761 if ((*payload_hook)(skb))
762 goto discard;
763
764 /* Prepare skb for adding to the session's reorder_q. Hold
765 * packets for max reorder_timeout or 1 second if not
766 * reordering.
767 */
768 L2TP_SKB_CB(skb)->length = length;
769 L2TP_SKB_CB(skb)->expires = jiffies +
770 (session->reorder_timeout ? session->reorder_timeout : HZ);
771
772 /* Add packet to the session's receive queue. Reordering is done here, if
773 * enabled. Saved L2TP protocol info is stored in skb->sb[].
774 */
775 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100776 if (l2tp_recv_data_seq(session, skb))
777 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000778 } else {
779 /* No sequence numbers. Add the skb to the tail of the
780 * reorder queue. This ensures that it will be
781 * delivered after all previous sequenced skbs.
782 */
783 skb_queue_tail(&session->reorder_q, skb);
784 }
785
786 /* Try to dequeue as many skbs from reorder_q as we can. */
787 l2tp_recv_dequeue(session);
788
James Chapmanf7faffa2010-04-02 06:18:49 +0000789 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000790
791discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000792 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000793 kfree_skb(skb);
James Chapmanf7faffa2010-04-02 06:18:49 +0000794}
795EXPORT_SYMBOL(l2tp_recv_common);
796
Tom Parkin48f72f92013-03-19 06:11:19 +0000797/* Drop skbs from the session's reorder_q
798 */
799int l2tp_session_queue_purge(struct l2tp_session *session)
800{
801 struct sk_buff *skb = NULL;
802 BUG_ON(!session);
803 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
804 while ((skb = skb_dequeue(&session->reorder_q))) {
805 atomic_long_inc(&session->stats.rx_errors);
806 kfree_skb(skb);
Tom Parkin48f72f92013-03-19 06:11:19 +0000807 }
808 return 0;
809}
810EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
811
James Chapmanf7faffa2010-04-02 06:18:49 +0000812/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
813 * here. The skb is not on a list when we get here.
814 * Returns 0 if the packet was a data packet and was successfully passed on.
815 * Returns 1 if the packet was not a good data packet and could not be
816 * forwarded. All such packets are passed up to userspace to deal with.
817 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000818static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
819 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000820{
821 struct l2tp_session *session = NULL;
822 unsigned char *ptr, *optr;
823 u16 hdrflags;
824 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000825 u16 version;
826 int length;
827
Tom Herbert58d60852014-05-07 16:52:48 -0700828 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000829
830 /* UDP always verifies the packet length. */
831 __skb_pull(skb, sizeof(struct udphdr));
832
833 /* Short packet? */
834 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000835 l2tp_info(tunnel, L2TP_MSG_DATA,
836 "%s: recv short packet (len=%d)\n",
837 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000838 goto error;
839 }
840
James Chapmanf7faffa2010-04-02 06:18:49 +0000841 /* Trace packet contents, if enabled */
842 if (tunnel->debug & L2TP_MSG_DATA) {
843 length = min(32u, skb->len);
844 if (!pskb_may_pull(skb, length))
845 goto error;
846
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000847 pr_debug("%s: recv\n", tunnel->name);
848 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000849 }
850
Eric Dumazete50e7052011-11-08 13:59:44 -0500851 /* Point to L2TP header */
852 optr = ptr = skb->data;
853
James Chapmanf7faffa2010-04-02 06:18:49 +0000854 /* Get L2TP header flags */
855 hdrflags = ntohs(*(__be16 *) ptr);
856
857 /* Check protocol version */
858 version = hdrflags & L2TP_HDR_VER_MASK;
859 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000860 l2tp_info(tunnel, L2TP_MSG_DATA,
861 "%s: recv protocol version mismatch: got %d expected %d\n",
862 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000863 goto error;
864 }
865
866 /* Get length of L2TP packet */
867 length = skb->len;
868
869 /* If type is control packet, it is handled by userspace. */
870 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000871 l2tp_dbg(tunnel, L2TP_MSG_DATA,
872 "%s: recv control packet, len=%d\n",
873 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000874 goto error;
875 }
876
877 /* Skip flags */
878 ptr += 2;
879
880 if (tunnel->version == L2TP_HDR_VER_2) {
881 /* If length is present, skip it */
882 if (hdrflags & L2TP_HDRFLAG_L)
883 ptr += 2;
884
885 /* Extract tunnel and session ID */
886 tunnel_id = ntohs(*(__be16 *) ptr);
887 ptr += 2;
888 session_id = ntohs(*(__be16 *) ptr);
889 ptr += 2;
890 } else {
891 ptr += 2; /* skip reserved bits */
892 tunnel_id = tunnel->tunnel_id;
893 session_id = ntohl(*(__be32 *) ptr);
894 ptr += 4;
895 }
896
897 /* Find the session context */
Guillaume Naulta4346212017-10-31 17:36:42 +0100898 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000899 if (!session || !session->recv_skb) {
Guillaume Naulta4346212017-10-31 17:36:42 +0100900 if (session)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200901 l2tp_session_dec_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200902
James Chapmanf7faffa2010-04-02 06:18:49 +0000903 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000904 l2tp_info(tunnel, L2TP_MSG_DATA,
905 "%s: no session found (%u/%u). Passing up.\n",
906 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000907 goto error;
908 }
909
910 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200911 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000912
913 return 0;
914
James Chapmanfd558d12010-04-02 06:18:33 +0000915error:
916 /* Put UDP header back */
917 __skb_push(skb, sizeof(struct udphdr));
918
919 return 1;
920}
James Chapmanfd558d12010-04-02 06:18:33 +0000921
922/* UDP encapsulation receive handler. See net/ipv4/udp.c.
923 * Return codes:
924 * 0 : success.
925 * <0: error
926 * >0: skb should be passed up to userspace as UDP.
927 */
928int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
929{
930 struct l2tp_tunnel *tunnel;
931
James Chapmand00fa9a2018-02-23 17:45:45 +0000932 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000933 if (tunnel == NULL)
934 goto pass_up;
935
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000936 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
937 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000938
939 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
James Chapmand00fa9a2018-02-23 17:45:45 +0000940 goto pass_up;
James Chapmanfd558d12010-04-02 06:18:33 +0000941
James Chapmanfd558d12010-04-02 06:18:33 +0000942 return 0;
943
James Chapmanfd558d12010-04-02 06:18:33 +0000944pass_up:
945 return 1;
946}
947EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
948
949/************************************************************************
950 * Transmit handling
951 ***********************************************************************/
952
953/* Build an L2TP header for the session into the buffer provided.
954 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000955static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000956{
James Chapmanf7faffa2010-04-02 06:18:49 +0000957 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000958 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000959 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000960 u16 flags = L2TP_HDR_VER_2;
961 u32 tunnel_id = tunnel->peer_tunnel_id;
962 u32 session_id = session->peer_session_id;
963
964 if (session->send_seq)
965 flags |= L2TP_HDRFLAG_S;
966
967 /* Setup L2TP header. */
968 *bufp++ = htons(flags);
969 *bufp++ = htons(tunnel_id);
970 *bufp++ = htons(session_id);
971 if (session->send_seq) {
972 *bufp++ = htons(session->ns);
973 *bufp++ = 0;
974 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000975 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000976 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
977 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000978 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000979
980 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000981}
982
James Chapmanf7faffa2010-04-02 06:18:49 +0000983static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000984{
James Chapman0d767512010-04-02 06:19:00 +0000985 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000986 char *bufp = buf;
987 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000988
James Chapman0d767512010-04-02 06:19:00 +0000989 /* Setup L2TP header. The header differs slightly for UDP and
990 * IP encapsulations. For UDP, there is 4 bytes of flags.
991 */
992 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
993 u16 flags = L2TP_HDR_VER_3;
994 *((__be16 *) bufp) = htons(flags);
995 bufp += 2;
996 *((__be16 *) bufp) = 0;
997 bufp += 2;
998 }
999
James Chapmanf7faffa2010-04-02 06:18:49 +00001000 *((__be32 *) bufp) = htonl(session->peer_session_id);
1001 bufp += 4;
1002 if (session->cookie_len) {
1003 memcpy(bufp, &session->cookie[0], session->cookie_len);
1004 bufp += session->cookie_len;
1005 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001006 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1007 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +00001008
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001009 if (session->send_seq) {
1010 l2h = 0x40000000 | session->ns;
1011 session->ns++;
1012 session->ns &= 0xffffff;
1013 l2tp_dbg(session, L2TP_MSG_SEQ,
1014 "%s: updated ns to %u\n",
1015 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001016 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001017
1018 *((__be32 *)bufp) = htonl(l2h);
1019 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001020 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001021
1022 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001023}
James Chapmanfd558d12010-04-02 06:18:33 +00001024
stephen hemmingerfc130842010-10-21 07:50:46 +00001025static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001026 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001027{
1028 struct l2tp_tunnel *tunnel = session->tunnel;
1029 unsigned int len = skb->len;
1030 int error;
1031
1032 /* Debug */
1033 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001034 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001035 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001036 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001037 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001038 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001039
1040 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001041 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1042 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001043
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001044 pr_debug("%s: xmit\n", session->name);
1045 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1046 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001047 }
1048
1049 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001050 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001051#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001052 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001053 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001054 else
1055#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001056 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001057
1058 /* Update stats */
1059 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001060 atomic_long_inc(&tunnel->stats.tx_packets);
1061 atomic_long_add(len, &tunnel->stats.tx_bytes);
1062 atomic_long_inc(&session->stats.tx_packets);
1063 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001064 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001065 atomic_long_inc(&tunnel->stats.tx_errors);
1066 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001067 }
1068
1069 return 0;
1070}
James Chapmanfd558d12010-04-02 06:18:33 +00001071
James Chapmanfd558d12010-04-02 06:18:33 +00001072/* If caller requires the skb to have a ppp header, the header must be
1073 * inserted in the skb data before calling this function.
1074 */
1075int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1076{
1077 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001078 struct l2tp_tunnel *tunnel = session->tunnel;
1079 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001080 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001081 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001082 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001083 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001084 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1085 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001086 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001087
1088 /* Check that there's enough headroom in the skb to insert IP,
1089 * UDP and L2TP headers. If not enough, expand it to
1090 * make room. Adjust truesize.
1091 */
1092 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001093 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001094 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001095 kfree_skb(skb);
1096 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001097 }
James Chapmanfd558d12010-04-02 06:18:33 +00001098
James Chapmanfd558d12010-04-02 06:18:33 +00001099 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001100 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001101
James Chapman0d767512010-04-02 06:19:00 +00001102 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001103 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1104 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1105 IPSKB_REROUTED);
1106 nf_reset(skb);
1107
David S. Miller6af88da2011-05-08 13:45:20 -07001108 bh_lock_sock(sk);
1109 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001110 kfree_skb(skb);
1111 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001112 goto out_unlock;
1113 }
1114
James Chapmanfd558d12010-04-02 06:18:33 +00001115 /* Get routing info from the tunnel socket */
1116 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001117 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001118
David S. Millerd9d8da82011-05-06 22:23:20 -07001119 inet = inet_sk(sk);
1120 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001121 switch (tunnel->encap) {
1122 case L2TP_ENCAPTYPE_UDP:
1123 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001124 __skb_push(skb, sizeof(*uh));
1125 skb_reset_transport_header(skb);
1126 uh = udp_hdr(skb);
1127 uh->source = inet->inet_sport;
1128 uh->dest = inet->inet_dport;
1129 udp_len = uhlen + hdr_len + data_len;
1130 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001131
1132 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001133#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001134 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001135 udp6_set_csum(udp_get_no_check6_tx(sk),
1136 skb, &inet6_sk(sk)->saddr,
1137 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001138 else
1139#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001140 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1141 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001142 break;
1143
1144 case L2TP_ENCAPTYPE_IP:
1145 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001146 }
1147
David S. Millerd9d8da82011-05-06 22:23:20 -07001148 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001149out_unlock:
1150 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001151
Eric Dumazetb8c84302012-06-28 20:15:13 +00001152 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001153}
1154EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1155
1156/*****************************************************************************
1157 * Tinnel and session create/destroy.
1158 *****************************************************************************/
1159
1160/* Tunnel socket destruct hook.
1161 * The tunnel context is deleted only when all session sockets have been
1162 * closed.
1163 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001164static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001165{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001166 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001167 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001168
James Chapmanfd558d12010-04-02 06:18:33 +00001169 if (tunnel == NULL)
1170 goto end;
1171
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001172 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001173
Tom Parkinf8ccac02013-01-31 23:43:00 +00001174 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001175 switch (tunnel->encap) {
1176 case L2TP_ENCAPTYPE_UDP:
1177 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1178 (udp_sk(sk))->encap_type = 0;
1179 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001180 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001181 break;
1182 case L2TP_ENCAPTYPE_IP:
1183 break;
1184 }
James Chapmanfd558d12010-04-02 06:18:33 +00001185
1186 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001187 sk->sk_destruct = tunnel->old_sk_destruct;
1188 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001189
1190 /* Remove the tunnel struct from the tunnel list */
1191 pn = l2tp_pernet(tunnel->l2tp_net);
1192 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1193 list_del_rcu(&tunnel->list);
1194 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001195
James Chapmanfd558d12010-04-02 06:18:33 +00001196 /* Call the original destructor */
1197 if (sk->sk_destruct)
1198 (*sk->sk_destruct)(sk);
James Chapmand00fa9a2018-02-23 17:45:45 +00001199
1200 kfree_rcu(tunnel, rcu);
James Chapmanfd558d12010-04-02 06:18:33 +00001201end:
1202 return;
1203}
James Chapmanfd558d12010-04-02 06:18:33 +00001204
1205/* When the tunnel is closed, all the attached sessions need to go too.
1206 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001207void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001208{
1209 int hash;
1210 struct hlist_node *walk;
1211 struct hlist_node *tmp;
1212 struct l2tp_session *session;
1213
1214 BUG_ON(tunnel == NULL);
1215
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001216 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1217 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001218
1219 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001220 tunnel->acpt_newsess = false;
James Chapmanfd558d12010-04-02 06:18:33 +00001221 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1222again:
1223 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1224 session = hlist_entry(walk, struct l2tp_session, hlist);
1225
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001226 l2tp_info(session, L2TP_MSG_CONTROL,
1227 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001228
1229 hlist_del_init(&session->hlist);
1230
Guillaume Naultb228a942017-09-22 15:39:24 +02001231 if (test_and_set_bit(0, &session->dead))
1232 goto again;
1233
James Chapmanfd558d12010-04-02 06:18:33 +00001234 write_unlock_bh(&tunnel->hlist_lock);
1235
Tom Parkinf6e16b22013-03-19 06:11:23 +00001236 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001237 l2tp_session_queue_purge(session);
1238
James Chapmanfd558d12010-04-02 06:18:33 +00001239 if (session->session_close != NULL)
1240 (*session->session_close)(session);
1241
Tom Parkin9980d002013-03-19 06:11:13 +00001242 l2tp_session_dec_refcount(session);
1243
James Chapmanfd558d12010-04-02 06:18:33 +00001244 write_lock_bh(&tunnel->hlist_lock);
1245
1246 /* Now restart from the beginning of this hash
1247 * chain. We always remove a session from the
1248 * list so we are guaranteed to make forward
1249 * progress.
1250 */
1251 goto again;
1252 }
1253 }
1254 write_unlock_bh(&tunnel->hlist_lock);
1255}
Tom Parkine34f4c72013-03-19 06:11:14 +00001256EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001257
Tom Parkin9980d002013-03-19 06:11:13 +00001258/* Tunnel socket destroy hook for UDP encapsulation */
1259static void l2tp_udp_encap_destroy(struct sock *sk)
1260{
James Chapmand00fa9a2018-02-23 17:45:45 +00001261 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1262
1263 if (tunnel)
1264 l2tp_tunnel_delete(tunnel);
Tom Parkin9980d002013-03-19 06:11:13 +00001265}
1266
Tom Parkinf8ccac02013-01-31 23:43:00 +00001267/* Workqueue tunnel deletion function */
1268static void l2tp_tunnel_del_work(struct work_struct *work)
1269{
James Chapmand00fa9a2018-02-23 17:45:45 +00001270 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1271 del_work);
1272 struct sock *sk = tunnel->sock;
1273 struct socket *sock = sk->sk_socket;
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001274
1275 l2tp_tunnel_closeall(tunnel);
1276
James Chapman76a6abd2018-02-23 17:45:43 +00001277 /* If the tunnel socket was created within the kernel, use
Tom Parkin02d13ed2013-03-19 06:11:18 +00001278 * the sk API to release it here.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001279 */
James Chapman76a6abd2018-02-23 17:45:43 +00001280 if (tunnel->fd < 0) {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001281 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001282 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001283 sock_release(sock);
1284 }
Tom Parkin167eb172013-01-31 23:43:03 +00001285 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001286
James Chapmand00fa9a2018-02-23 17:45:45 +00001287 /* drop initial ref */
1288 l2tp_tunnel_dec_refcount(tunnel);
1289
1290 /* drop workqueue ref */
Alexander Couzens06a15f52015-09-28 11:32:42 +02001291 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001292}
James Chapmanfd558d12010-04-02 06:18:33 +00001293
James Chapman789a4a22010-04-02 06:19:40 +00001294/* Create a socket for the tunnel, if one isn't set up by
1295 * userspace. This is used for static tunnels where there is no
1296 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001297 *
1298 * Since we don't want these sockets to keep a namespace alive by
1299 * themselves, we drop the socket's namespace refcount after creation.
1300 * These sockets are freed when the namespace exits using the pernet
1301 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001302 */
Tom Parkin167eb172013-01-31 23:43:03 +00001303static int l2tp_tunnel_sock_create(struct net *net,
1304 u32 tunnel_id,
1305 u32 peer_tunnel_id,
1306 struct l2tp_tunnel_cfg *cfg,
1307 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001308{
1309 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001310 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001311 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001312
1313 switch (cfg->encap) {
1314 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001315 memset(&udp_conf, 0, sizeof(udp_conf));
1316
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001317#if IS_ENABLED(CONFIG_IPV6)
1318 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001319 udp_conf.family = AF_INET6;
1320 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1321 sizeof(udp_conf.local_ip6));
1322 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1323 sizeof(udp_conf.peer_ip6));
1324 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001325 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001326 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001327 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001328 } else
1329#endif
1330 {
Tom Herbert85644b42014-07-13 19:49:48 -07001331 udp_conf.family = AF_INET;
1332 udp_conf.local_ip = cfg->local_ip;
1333 udp_conf.peer_ip = cfg->peer_ip;
1334 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001335 }
James Chapman789a4a22010-04-02 06:19:40 +00001336
Tom Herbert85644b42014-07-13 19:49:48 -07001337 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1338 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1339
1340 err = udp_sock_create(net, &udp_conf, &sock);
1341 if (err < 0)
1342 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001343
1344 break;
1345
1346 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001347#if IS_ENABLED(CONFIG_IPV6)
1348 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001349 struct sockaddr_l2tpip6 ip6_addr = {0};
1350
Eric W. Biederman26abe142015-05-08 21:10:31 -05001351 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001352 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001353 if (err < 0)
1354 goto out;
1355
James Chapman5dac94e2012-04-29 21:48:55 +00001356 ip6_addr.l2tp_family = AF_INET6;
1357 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1358 sizeof(ip6_addr.l2tp_addr));
1359 ip6_addr.l2tp_conn_id = tunnel_id;
1360 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1361 sizeof(ip6_addr));
1362 if (err < 0)
1363 goto out;
1364
1365 ip6_addr.l2tp_family = AF_INET6;
1366 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1367 sizeof(ip6_addr.l2tp_addr));
1368 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1369 err = kernel_connect(sock,
1370 (struct sockaddr *) &ip6_addr,
1371 sizeof(ip6_addr), 0);
1372 if (err < 0)
1373 goto out;
1374 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001375#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001376 {
Tom Herbert85644b42014-07-13 19:49:48 -07001377 struct sockaddr_l2tpip ip_addr = {0};
1378
Eric W. Biederman26abe142015-05-08 21:10:31 -05001379 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001380 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001381 if (err < 0)
1382 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001383
James Chapman5dac94e2012-04-29 21:48:55 +00001384 ip_addr.l2tp_family = AF_INET;
1385 ip_addr.l2tp_addr = cfg->local_ip;
1386 ip_addr.l2tp_conn_id = tunnel_id;
1387 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1388 sizeof(ip_addr));
1389 if (err < 0)
1390 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001391
James Chapman5dac94e2012-04-29 21:48:55 +00001392 ip_addr.l2tp_family = AF_INET;
1393 ip_addr.l2tp_addr = cfg->peer_ip;
1394 ip_addr.l2tp_conn_id = peer_tunnel_id;
1395 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1396 sizeof(ip_addr), 0);
1397 if (err < 0)
1398 goto out;
1399 }
James Chapman789a4a22010-04-02 06:19:40 +00001400 break;
1401
1402 default:
1403 goto out;
1404 }
1405
1406out:
Tom Parkin167eb172013-01-31 23:43:03 +00001407 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001408 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001409 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001410 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001411 *sockp = NULL;
1412 }
1413
1414 return err;
1415}
1416
Eric Dumazet37159ef2012-09-04 07:18:57 +00001417static struct lock_class_key l2tp_socket_class;
1418
James Chapmanfd558d12010-04-02 06:18:33 +00001419int 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)
1420{
1421 struct l2tp_tunnel *tunnel = NULL;
1422 int err;
1423 struct socket *sock = NULL;
1424 struct sock *sk = NULL;
1425 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001426 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001427
1428 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001429 * the userspace L2TP daemon. If not specified, create a
1430 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001431 */
James Chapman789a4a22010-04-02 06:19:40 +00001432 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001433 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1434 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001435 if (err < 0)
1436 goto err;
1437 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001438 sock = sockfd_lookup(fd, &err);
1439 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001440 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001441 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001442 err = -EBADF;
1443 goto err;
1444 }
1445
1446 /* Reject namespace mismatches */
1447 if (!net_eq(sock_net(sock->sk), net)) {
1448 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1449 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001450 goto err;
1451 }
James Chapmanfd558d12010-04-02 06:18:33 +00001452 }
1453
1454 sk = sock->sk;
1455
James Chapman0d767512010-04-02 06:19:00 +00001456 if (cfg != NULL)
1457 encap = cfg->encap;
1458
James Chapmanfd558d12010-04-02 06:18:33 +00001459 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001460 switch (encap) {
1461 case L2TP_ENCAPTYPE_UDP:
1462 err = -EPROTONOSUPPORT;
1463 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001464 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001465 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1466 goto err;
1467 }
1468 break;
1469 case L2TP_ENCAPTYPE_IP:
1470 err = -EPROTONOSUPPORT;
1471 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001472 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001473 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1474 goto err;
1475 }
1476 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001477 }
1478
1479 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001480 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001481 if (tunnel != NULL) {
1482 /* This socket has already been prepped */
1483 err = -EBUSY;
1484 goto err;
1485 }
1486
James Chapmanfd558d12010-04-02 06:18:33 +00001487 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1488 if (tunnel == NULL) {
1489 err = -ENOMEM;
1490 goto err;
1491 }
1492
1493 tunnel->version = version;
1494 tunnel->tunnel_id = tunnel_id;
1495 tunnel->peer_tunnel_id = peer_tunnel_id;
1496 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1497
1498 tunnel->magic = L2TP_TUNNEL_MAGIC;
1499 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1500 rwlock_init(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001501 tunnel->acpt_newsess = true;
James Chapmanfd558d12010-04-02 06:18:33 +00001502
1503 /* The net we belong to */
1504 tunnel->l2tp_net = net;
1505 pn = l2tp_pernet(net);
1506
James Chapman0d767512010-04-02 06:19:00 +00001507 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001508 tunnel->debug = cfg->debug;
1509
François Cachereule18503f2013-10-02 10:16:02 +02001510#if IS_ENABLED(CONFIG_IPV6)
1511 if (sk->sk_family == PF_INET6) {
1512 struct ipv6_pinfo *np = inet6_sk(sk);
1513
1514 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001515 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001516 struct inet_sock *inet = inet_sk(sk);
1517
1518 tunnel->v4mapped = true;
1519 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001520 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1521 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001522 } else {
1523 tunnel->v4mapped = false;
1524 }
1525 }
1526#endif
1527
James Chapmanfd558d12010-04-02 06:18:33 +00001528 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001529 tunnel->encap = encap;
1530 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001531 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001532
Andy Zhouc8fffce2014-09-16 17:31:19 -07001533 udp_cfg.sk_user_data = tunnel;
1534 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1535 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1536 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1537
1538 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1539 } else {
1540 sk->sk_user_data = tunnel;
1541 }
James Chapmanfd558d12010-04-02 06:18:33 +00001542
James Chapmand00fa9a2018-02-23 17:45:45 +00001543 /* Bump the reference count. The tunnel context is deleted
1544 * only when this drops to zero. A reference is also held on
1545 * the tunnel socket to ensure that it is not released while
1546 * the tunnel is extant. Must be done before sk_destruct is
1547 * set.
1548 */
1549 refcount_set(&tunnel->ref_count, 1);
1550 sock_hold(sk);
1551 tunnel->sock = sk;
1552 tunnel->fd = fd;
1553
James Chapmanfd558d12010-04-02 06:18:33 +00001554 /* Hook on the tunnel socket destructor so that we can cleanup
1555 * if the tunnel socket goes away.
1556 */
1557 tunnel->old_sk_destruct = sk->sk_destruct;
1558 sk->sk_destruct = &l2tp_tunnel_destruct;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001559 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1560
James Chapmanfd558d12010-04-02 06:18:33 +00001561 sk->sk_allocation = GFP_ATOMIC;
1562
Tom Parkinf8ccac02013-01-31 23:43:00 +00001563 /* Init delete workqueue struct */
1564 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1565
James Chapmanfd558d12010-04-02 06:18:33 +00001566 /* Add tunnel to our list */
1567 INIT_LIST_HEAD(&tunnel->list);
Eric Dumazet17691922011-05-11 18:22:36 +00001568 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1569 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1570 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001571
1572 err = 0;
1573err:
1574 if (tunnelp)
1575 *tunnelp = tunnel;
1576
James Chapman789a4a22010-04-02 06:19:40 +00001577 /* If tunnel's socket was created by the kernel, it doesn't
1578 * have a file.
1579 */
1580 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001581 sockfd_put(sock);
1582
1583 return err;
1584}
1585EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1586
James Chapman309795f2010-04-02 06:19:10 +00001587/* This function is used by the netlink TUNNEL_DELETE command.
1588 */
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001589void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001590{
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001591 if (!test_and_set_bit(0, &tunnel->dead)) {
1592 l2tp_tunnel_inc_refcount(tunnel);
1593 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001594 }
James Chapman309795f2010-04-02 06:19:10 +00001595}
1596EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1597
James Chapmanfd558d12010-04-02 06:18:33 +00001598/* Really kill the session.
1599 */
1600void l2tp_session_free(struct l2tp_session *session)
1601{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001602 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001603
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001604 BUG_ON(refcount_read(&session->ref_count) != 0);
James Chapmanfd558d12010-04-02 06:18:33 +00001605
Tom Parkinf6e16b22013-03-19 06:11:23 +00001606 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001607 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001608 l2tp_tunnel_dec_refcount(tunnel);
1609 }
1610
1611 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001612}
1613EXPORT_SYMBOL_GPL(l2tp_session_free);
1614
Tom Parkinf6e16b22013-03-19 06:11:23 +00001615/* Remove an l2tp session from l2tp_core's hash lists.
1616 * Provides a tidyup interface for pseudowire code which can't just route all
1617 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1618 * callback.
1619 */
1620void __l2tp_session_unhash(struct l2tp_session *session)
1621{
1622 struct l2tp_tunnel *tunnel = session->tunnel;
1623
1624 /* Remove the session from core hashes */
1625 if (tunnel) {
1626 /* Remove from the per-tunnel hash */
1627 write_lock_bh(&tunnel->hlist_lock);
1628 hlist_del_init(&session->hlist);
1629 write_unlock_bh(&tunnel->hlist_lock);
1630
1631 /* For L2TPv3 we have a per-net hash: remove from there, too */
1632 if (tunnel->version != L2TP_HDR_VER_2) {
1633 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1634 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1635 hlist_del_init_rcu(&session->global_hlist);
1636 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1637 synchronize_rcu();
1638 }
1639 }
1640}
1641EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1642
James Chapman309795f2010-04-02 06:19:10 +00001643/* This function is used by the netlink SESSION_DELETE command and by
1644 pseudowire modules.
1645 */
1646int l2tp_session_delete(struct l2tp_session *session)
1647{
Guillaume Naultb228a942017-09-22 15:39:24 +02001648 if (test_and_set_bit(0, &session->dead))
1649 return 0;
1650
Tom Parkinf6e16b22013-03-19 06:11:23 +00001651 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001652 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001653 if (session->session_close != NULL)
1654 (*session->session_close)(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001655
James Chapman309795f2010-04-02 06:19:10 +00001656 l2tp_session_dec_refcount(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001657
James Chapman309795f2010-04-02 06:19:10 +00001658 return 0;
1659}
1660EXPORT_SYMBOL_GPL(l2tp_session_delete);
1661
James Chapmanf7faffa2010-04-02 06:18:49 +00001662/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001663 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001664 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001665void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001666{
1667 if (version == L2TP_HDR_VER_2) {
1668 session->hdr_len = 6;
1669 if (session->send_seq)
1670 session->hdr_len += 4;
1671 } else {
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001672 session->hdr_len = 4 + session->cookie_len;
1673 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001674 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1675 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001676 }
1677
1678}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001679EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001680
James Chapmanfd558d12010-04-02 06:18:33 +00001681struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1682{
1683 struct l2tp_session *session;
1684
1685 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1686 if (session != NULL) {
1687 session->magic = L2TP_SESSION_MAGIC;
1688 session->tunnel = tunnel;
1689
1690 session->session_id = session_id;
1691 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001692 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001693 if (tunnel->version == L2TP_HDR_VER_2)
1694 session->nr_max = 0xffff;
1695 else
1696 session->nr_max = 0xffffff;
1697 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001698 session->nr_oos_count_max = 4;
1699
1700 /* Use NR of first received packet */
1701 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001702
1703 sprintf(&session->name[0], "sess %u/%u",
1704 tunnel->tunnel_id, session->session_id);
1705
1706 skb_queue_head_init(&session->reorder_q);
1707
1708 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001709 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001710
1711 /* Inherit debug options from tunnel */
1712 session->debug = tunnel->debug;
1713
1714 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001715 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001716 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001717 session->mtu = cfg->mtu;
1718 session->mru = cfg->mru;
1719 session->send_seq = cfg->send_seq;
1720 session->recv_seq = cfg->recv_seq;
1721 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001722 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001723 session->l2specific_type = cfg->l2specific_type;
James Chapmanf7faffa2010-04-02 06:18:49 +00001724 session->cookie_len = cfg->cookie_len;
1725 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1726 session->peer_cookie_len = cfg->peer_cookie_len;
1727 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001728 }
1729
James Chapmanf7faffa2010-04-02 06:18:49 +00001730 if (tunnel->version == L2TP_HDR_VER_2)
1731 session->build_header = l2tp_build_l2tpv2_header;
1732 else
1733 session->build_header = l2tp_build_l2tpv3_header;
1734
1735 l2tp_session_set_header_len(session, tunnel->version);
1736
Guillaume Nault9ee369a2017-08-25 16:22:17 +02001737 refcount_set(&session->ref_count, 1);
1738
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001739 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001740 }
1741
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001742 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001743}
1744EXPORT_SYMBOL_GPL(l2tp_session_create);
1745
1746/*****************************************************************************
1747 * Init and cleanup
1748 *****************************************************************************/
1749
1750static __net_init int l2tp_init_net(struct net *net)
1751{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001752 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001753 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001754
James Chapmanfd558d12010-04-02 06:18:33 +00001755 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001756 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001757
James Chapmanf7faffa2010-04-02 06:18:49 +00001758 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1759 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1760
James Chapmane02d4942010-04-02 06:19:16 +00001761 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001762
James Chapmanfd558d12010-04-02 06:18:33 +00001763 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001764}
1765
Tom Parkin167eb172013-01-31 23:43:03 +00001766static __net_exit void l2tp_exit_net(struct net *net)
1767{
1768 struct l2tp_net *pn = l2tp_pernet(net);
1769 struct l2tp_tunnel *tunnel = NULL;
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001770 int hash;
Tom Parkin167eb172013-01-31 23:43:03 +00001771
1772 rcu_read_lock_bh();
1773 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slaby4dc12ff2017-10-25 15:57:55 +02001774 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001775 }
1776 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001777
1778 flush_workqueue(l2tp_wq);
1779 rcu_barrier();
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001780
1781 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1782 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
Tom Parkin167eb172013-01-31 23:43:03 +00001783}
1784
James Chapmanfd558d12010-04-02 06:18:33 +00001785static struct pernet_operations l2tp_net_ops = {
1786 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001787 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001788 .id = &l2tp_net_id,
1789 .size = sizeof(struct l2tp_net),
1790};
1791
1792static int __init l2tp_init(void)
1793{
1794 int rc = 0;
1795
1796 rc = register_pernet_device(&l2tp_net_ops);
1797 if (rc)
1798 goto out;
1799
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001800 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001801 if (!l2tp_wq) {
1802 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001803 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001804 rc = -ENOMEM;
1805 goto out;
1806 }
1807
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001808 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001809
1810out:
1811 return rc;
1812}
1813
1814static void __exit l2tp_exit(void)
1815{
1816 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001817 if (l2tp_wq) {
1818 destroy_workqueue(l2tp_wq);
1819 l2tp_wq = NULL;
1820 }
James Chapmanfd558d12010-04-02 06:18:33 +00001821}
1822
1823module_init(l2tp_init);
1824module_exit(l2tp_exit);
1825
1826MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1827MODULE_DESCRIPTION("L2TP core");
1828MODULE_LICENSE("GPL");
1829MODULE_VERSION(L2TP_DRV_VERSION);
1830