blob: 88efb8b845cac65003b41a30f9874512cdb01b51 [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
Tom Parkin80d84ef2013-01-22 05:13:48 +0000139/* Lookup the tunnel socket, possibly involving the fs code if the socket is
140 * owned by userspace. A struct sock returned from this function must be
141 * released using l2tp_tunnel_sock_put once you're done with it.
142 */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800143static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000144{
145 int err = 0;
146 struct socket *sock = NULL;
147 struct sock *sk = NULL;
148
149 if (!tunnel)
150 goto out;
151
152 if (tunnel->fd >= 0) {
153 /* Socket is owned by userspace, who might be in the process
154 * of closing it. Look the socket up using the fd to ensure
155 * consistency.
156 */
157 sock = sockfd_lookup(tunnel->fd, &err);
158 if (sock)
159 sk = sock->sk;
160 } else {
161 /* Socket is owned by kernelspace */
162 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000163 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000164 }
165
166out:
167 return sk;
168}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000169
170/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800171static void l2tp_tunnel_sock_put(struct sock *sk)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000172{
173 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
174 if (tunnel) {
175 if (tunnel->fd >= 0) {
176 /* Socket is owned by userspace */
177 sockfd_put(sk->sk_socket);
178 }
179 sock_put(sk);
180 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000181 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000182}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000183
James Chapmanfd558d12010-04-02 06:18:33 +0000184/* Session hash list.
185 * The session_id SHOULD be random according to RFC2661, but several
186 * L2TP implementations (Cisco and Microsoft) use incrementing
187 * session_ids. So we do a real hash on the session_id, rather than a
188 * simple bitmask.
189 */
190static inline struct hlist_head *
191l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
192{
193 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
194}
195
Guillaume Nault54652eb2017-08-25 16:51:40 +0200196/* Lookup a tunnel. A new reference is held on the returned tunnel. */
197struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
198{
199 const struct l2tp_net *pn = l2tp_pernet(net);
200 struct l2tp_tunnel *tunnel;
201
202 rcu_read_lock_bh();
203 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
204 if (tunnel->tunnel_id == tunnel_id) {
205 l2tp_tunnel_inc_refcount(tunnel);
206 rcu_read_unlock_bh();
207
208 return tunnel;
209 }
210 }
211 rcu_read_unlock_bh();
212
213 return NULL;
214}
215EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
216
Guillaume Naulta4346212017-10-31 17:36:42 +0100217/* Lookup a session. A new reference is held on the returned session. */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200218struct l2tp_session *l2tp_session_get(const struct net *net,
Guillaume Nault61b9a042017-03-31 13:02:25 +0200219 struct l2tp_tunnel *tunnel,
Guillaume Naulta4346212017-10-31 17:36:42 +0100220 u32 session_id)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200221{
222 struct hlist_head *session_list;
223 struct l2tp_session *session;
224
225 if (!tunnel) {
226 struct l2tp_net *pn = l2tp_pernet(net);
227
228 session_list = l2tp_session_id_hash_2(pn, session_id);
229
230 rcu_read_lock_bh();
231 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
232 if (session->session_id == session_id) {
233 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200234 rcu_read_unlock_bh();
235
236 return session;
237 }
238 }
239 rcu_read_unlock_bh();
240
241 return NULL;
242 }
243
244 session_list = l2tp_session_id_hash(tunnel, session_id);
245 read_lock_bh(&tunnel->hlist_lock);
246 hlist_for_each_entry(session, session_list, hlist) {
247 if (session->session_id == session_id) {
248 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200249 read_unlock_bh(&tunnel->hlist_lock);
250
251 return session;
252 }
253 }
254 read_unlock_bh(&tunnel->hlist_lock);
255
256 return NULL;
257}
258EXPORT_SYMBOL_GPL(l2tp_session_get);
259
Guillaume Naulta4346212017-10-31 17:36:42 +0100260struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000261{
262 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000263 struct l2tp_session *session;
264 int count = 0;
265
266 read_lock_bh(&tunnel->hlist_lock);
267 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800268 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000269 if (++count > nth) {
Guillaume Naulte08293a2017-04-03 12:03:13 +0200270 l2tp_session_inc_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000271 read_unlock_bh(&tunnel->hlist_lock);
272 return session;
273 }
274 }
275 }
276
277 read_unlock_bh(&tunnel->hlist_lock);
278
279 return NULL;
280}
Guillaume Naulte08293a2017-04-03 12:03:13 +0200281EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000282
James Chapman309795f2010-04-02 06:19:10 +0000283/* Lookup a session by interface name.
284 * This is very inefficient but is only used by management interfaces.
285 */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200286struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
Guillaume Naulta4346212017-10-31 17:36:42 +0100287 const char *ifname)
James Chapman309795f2010-04-02 06:19:10 +0000288{
289 struct l2tp_net *pn = l2tp_pernet(net);
290 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000291 struct l2tp_session *session;
292
James Chapmane02d4942010-04-02 06:19:16 +0000293 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000294 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800295 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000296 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200297 l2tp_session_inc_refcount(session);
James Chapmane02d4942010-04-02 06:19:16 +0000298 rcu_read_unlock_bh();
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200299
James Chapman309795f2010-04-02 06:19:10 +0000300 return session;
301 }
302 }
303 }
304
James Chapmane02d4942010-04-02 06:19:16 +0000305 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000306
307 return NULL;
308}
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200309EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000310
Guillaume Nault3953ae72017-10-27 16:51:50 +0200311int l2tp_session_register(struct l2tp_session *session,
312 struct l2tp_tunnel *tunnel)
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200313{
314 struct l2tp_session *session_walk;
315 struct hlist_head *g_head;
316 struct hlist_head *head;
317 struct l2tp_net *pn;
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200318 int err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200319
320 head = l2tp_session_id_hash(tunnel, session->session_id);
321
322 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200323 if (!tunnel->acpt_newsess) {
324 err = -ENODEV;
325 goto err_tlock;
326 }
327
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200328 hlist_for_each_entry(session_walk, head, hlist)
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200329 if (session_walk->session_id == session->session_id) {
330 err = -EEXIST;
331 goto err_tlock;
332 }
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200333
334 if (tunnel->version == L2TP_HDR_VER_3) {
335 pn = l2tp_pernet(tunnel->l2tp_net);
336 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
337 session->session_id);
338
339 spin_lock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200340
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200341 hlist_for_each_entry(session_walk, g_head, global_hlist)
342 if (session_walk->session_id == session->session_id) {
343 err = -EEXIST;
344 goto err_tlock_pnlock;
345 }
346
347 l2tp_tunnel_inc_refcount(tunnel);
348 sock_hold(tunnel->sock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200349 hlist_add_head_rcu(&session->global_hlist, g_head);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200350
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200351 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200352 } else {
353 l2tp_tunnel_inc_refcount(tunnel);
354 sock_hold(tunnel->sock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200355 }
356
357 hlist_add_head(&session->hlist, head);
358 write_unlock_bh(&tunnel->hlist_lock);
359
360 return 0;
361
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200362err_tlock_pnlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200363 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200364err_tlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200365 write_unlock_bh(&tunnel->hlist_lock);
366
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200367 return err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200368}
Guillaume Nault3953ae72017-10-27 16:51:50 +0200369EXPORT_SYMBOL_GPL(l2tp_session_register);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200370
James Chapmanfd558d12010-04-02 06:18:33 +0000371/* Lookup a tunnel by id
372 */
Guillaume Nault2f858b92017-04-12 10:05:30 +0200373struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000374{
375 struct l2tp_tunnel *tunnel;
376 struct l2tp_net *pn = l2tp_pernet(net);
377
James Chapmane02d4942010-04-02 06:19:16 +0000378 rcu_read_lock_bh();
379 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000380 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000381 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000382 return tunnel;
383 }
384 }
James Chapmane02d4942010-04-02 06:19:16 +0000385 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000386
387 return NULL;
388}
389EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
390
Guillaume Nault2f858b92017-04-12 10:05:30 +0200391struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000392{
393 struct l2tp_net *pn = l2tp_pernet(net);
394 struct l2tp_tunnel *tunnel;
395 int count = 0;
396
James Chapmane02d4942010-04-02 06:19:16 +0000397 rcu_read_lock_bh();
398 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000399 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000400 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000401 return tunnel;
402 }
403 }
404
James Chapmane02d4942010-04-02 06:19:16 +0000405 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000406
407 return NULL;
408}
409EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
410
411/*****************************************************************************
412 * Receive data handling
413 *****************************************************************************/
414
415/* Queue a skb in order. We come here only if the skb has an L2TP sequence
416 * number.
417 */
418static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
419{
420 struct sk_buff *skbp;
421 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000422 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000423
424 spin_lock_bh(&session->reorder_q.lock);
425 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
426 if (L2TP_SKB_CB(skbp)->ns > ns) {
427 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000428 l2tp_dbg(session, L2TP_MSG_SEQ,
429 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
430 session->name, ns, L2TP_SKB_CB(skbp)->ns,
431 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000432 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000433 goto out;
434 }
435 }
436
437 __skb_queue_tail(&session->reorder_q, skb);
438
439out:
440 spin_unlock_bh(&session->reorder_q.lock);
441}
442
443/* Dequeue a single skb.
444 */
445static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
446{
447 struct l2tp_tunnel *tunnel = session->tunnel;
448 int length = L2TP_SKB_CB(skb)->length;
449
450 /* We're about to requeue the skb, so return resources
451 * to its current owner (a socket receive buffer).
452 */
453 skb_orphan(skb);
454
Tom Parkin7b7c0712013-03-19 06:11:22 +0000455 atomic_long_inc(&tunnel->stats.rx_packets);
456 atomic_long_add(length, &tunnel->stats.rx_bytes);
457 atomic_long_inc(&session->stats.rx_packets);
458 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000459
460 if (L2TP_SKB_CB(skb)->has_seq) {
461 /* Bump our Nr */
462 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100463 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000464
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000465 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
466 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000467 }
468
469 /* call private receive handler */
470 if (session->recv_skb != NULL)
471 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
472 else
473 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000474}
475
476/* Dequeue skbs from the session's reorder_q, subject to packet order.
477 * Skbs that have been in the queue for too long are simply discarded.
478 */
479static void l2tp_recv_dequeue(struct l2tp_session *session)
480{
481 struct sk_buff *skb;
482 struct sk_buff *tmp;
483
484 /* If the pkt at the head of the queue has the nr that we
485 * expect to send up next, dequeue it and any other
486 * in-sequence packets behind it.
487 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000488start:
James Chapmanfd558d12010-04-02 06:18:33 +0000489 spin_lock_bh(&session->reorder_q.lock);
490 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
491 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000492 atomic_long_inc(&session->stats.rx_seq_discards);
493 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000494 l2tp_dbg(session, L2TP_MSG_SEQ,
495 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
496 session->name, L2TP_SKB_CB(skb)->ns,
497 L2TP_SKB_CB(skb)->length, session->nr,
498 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000499 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000500 __skb_unlink(skb, &session->reorder_q);
501 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000502 continue;
503 }
504
505 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000506 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000507 l2tp_dbg(session, L2TP_MSG_SEQ,
508 "%s: advancing nr to next pkt: %u -> %u",
509 session->name, session->nr,
510 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000511 session->reorder_skip = 0;
512 session->nr = L2TP_SKB_CB(skb)->ns;
513 }
James Chapmanfd558d12010-04-02 06:18:33 +0000514 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000515 l2tp_dbg(session, L2TP_MSG_SEQ,
516 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
517 session->name, L2TP_SKB_CB(skb)->ns,
518 L2TP_SKB_CB(skb)->length, session->nr,
519 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000520 goto out;
521 }
522 }
523 __skb_unlink(skb, &session->reorder_q);
524
525 /* Process the skb. We release the queue lock while we
526 * do so to let other contexts process the queue.
527 */
528 spin_unlock_bh(&session->reorder_q.lock);
529 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000530 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000531 }
532
533out:
534 spin_unlock_bh(&session->reorder_q.lock);
535}
536
James Chapman8a1631d2013-07-02 20:28:59 +0100537static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
538{
539 u32 nws;
540
541 if (nr >= session->nr)
542 nws = nr - session->nr;
543 else
544 nws = (session->nr_max + 1) - (session->nr - nr);
545
546 return nws < session->nr_window_size;
547}
548
James Chapmanb6dc01a2013-07-02 20:28:58 +0100549/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
550 * acceptable, else non-zero.
551 */
552static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
553{
James Chapman8a1631d2013-07-02 20:28:59 +0100554 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
555 /* Packet sequence number is outside allowed window.
556 * Discard it.
557 */
558 l2tp_dbg(session, L2TP_MSG_SEQ,
559 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
560 session->name, L2TP_SKB_CB(skb)->ns,
561 L2TP_SKB_CB(skb)->length, session->nr);
562 goto discard;
563 }
564
James Chapmanb6dc01a2013-07-02 20:28:58 +0100565 if (session->reorder_timeout != 0) {
566 /* Packet reordering enabled. Add skb to session's
567 * reorder queue, in order of ns.
568 */
569 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100570 goto out;
571 }
572
573 /* Packet reordering disabled. Discard out-of-sequence packets, while
574 * tracking the number if in-sequence packets after the first OOS packet
575 * is seen. After nr_oos_count_max in-sequence packets, reset the
576 * sequence number to re-enable packet reception.
577 */
578 if (L2TP_SKB_CB(skb)->ns == session->nr) {
579 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100580 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100581 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
582 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
583
584 if (nr_oos == nr_next)
585 session->nr_oos_count++;
586 else
587 session->nr_oos_count = 0;
588
589 session->nr_oos = nr_oos;
590 if (session->nr_oos_count > session->nr_oos_count_max) {
591 session->reorder_skip = 1;
592 l2tp_dbg(session, L2TP_MSG_SEQ,
593 "%s: %d oos packets received. Resetting sequence numbers\n",
594 session->name, session->nr_oos_count);
595 }
596 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100597 atomic_long_inc(&session->stats.rx_seq_discards);
598 l2tp_dbg(session, L2TP_MSG_SEQ,
599 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
600 session->name, L2TP_SKB_CB(skb)->ns,
601 L2TP_SKB_CB(skb)->length, session->nr,
602 skb_queue_len(&session->reorder_q));
603 goto discard;
604 }
605 skb_queue_tail(&session->reorder_q, skb);
606 }
607
James Chapmana0dbd822013-07-02 20:29:00 +0100608out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100609 return 0;
610
611discard:
612 return 1;
613}
614
James Chapmanf7faffa2010-04-02 06:18:49 +0000615/* Do receive processing of L2TP data frames. We handle both L2TPv2
616 * and L2TPv3 data frames here.
617 *
618 * L2TPv2 Data Message Header
619 *
620 * 0 1 2 3
621 * 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
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
625 * | Tunnel ID | Session ID |
626 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627 * | Ns (opt) | Nr (opt) |
628 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
629 * | Offset Size (opt) | Offset pad... (opt)
630 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 *
632 * Data frames are marked by T=0. All other fields are the same as
633 * those in L2TP control frames.
634 *
635 * L2TPv3 Data Message Header
636 *
637 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 * | L2TP Session Header |
639 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * | L2-Specific Sublayer |
641 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 * | Tunnel Payload ...
643 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
644 *
645 * L2TPv3 Session Header Over IP
646 *
647 * 0 1 2 3
648 * 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
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 * | Session ID |
651 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 * | Cookie (optional, maximum 64 bits)...
653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
654 * |
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 *
657 * L2TPv3 L2-Specific Sublayer Format
658 *
659 * 0 1 2 3
660 * 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
661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
662 * |x|S|x|x|x|x|x|x| Sequence Number |
663 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
664 *
Guillaume Nault23fe8462018-01-05 19:47:14 +0100665 * Cookie value and sublayer format are negotiated with the peer when
666 * the session is set up. Unlike L2TPv2, we do not need to parse the
667 * packet header to determine if optional fields are present.
James Chapmanf7faffa2010-04-02 06:18:49 +0000668 *
669 * Caller must already have parsed the frame and determined that it is
670 * a data (not control) frame before coming here. Fields up to the
671 * session-id have already been parsed and ptr points to the data
672 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000673 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000674void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
675 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
676 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000677{
James Chapmanf7faffa2010-04-02 06:18:49 +0000678 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000679 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000680 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000681
James Chapmanf7faffa2010-04-02 06:18:49 +0000682 /* Parse and check optional cookie */
683 if (session->peer_cookie_len > 0) {
684 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000685 l2tp_info(tunnel, L2TP_MSG_DATA,
686 "%s: cookie mismatch (%u/%u). Discarding.\n",
687 tunnel->name, tunnel->tunnel_id,
688 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000689 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000690 goto discard;
691 }
692 ptr += session->peer_cookie_len;
693 }
694
James Chapmanfd558d12010-04-02 06:18:33 +0000695 /* Handle the optional sequence numbers. Sequence numbers are
696 * in different places for L2TPv2 and L2TPv3.
697 *
698 * If we are the LAC, enable/disable sequence numbers under
699 * the control of the LNS. If no sequence numbers present but
700 * we were expecting them, discard frame.
701 */
702 ns = nr = 0;
703 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000704 if (tunnel->version == L2TP_HDR_VER_2) {
705 if (hdrflags & L2TP_HDRFLAG_S) {
706 ns = ntohs(*(__be16 *) ptr);
707 ptr += 2;
708 nr = ntohs(*(__be16 *) ptr);
709 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000710
James Chapmanf7faffa2010-04-02 06:18:49 +0000711 /* Store L2TP info in the skb */
712 L2TP_SKB_CB(skb)->ns = ns;
713 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000714
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000715 l2tp_dbg(session, L2TP_MSG_SEQ,
716 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
717 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000718 }
719 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
720 u32 l2h = ntohl(*(__be32 *) ptr);
721
722 if (l2h & 0x40000000) {
723 ns = l2h & 0x00ffffff;
724
725 /* Store L2TP info in the skb */
726 L2TP_SKB_CB(skb)->ns = ns;
727 L2TP_SKB_CB(skb)->has_seq = 1;
728
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000729 l2tp_dbg(session, L2TP_MSG_SEQ,
730 "%s: recv data ns=%u, session nr=%u\n",
731 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000732 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100733 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000734 }
735
736 if (L2TP_SKB_CB(skb)->has_seq) {
737 /* Received a packet with sequence numbers. If we're the LNS,
738 * check if we sre sending sequence numbers and if not,
739 * configure it so.
740 */
741 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000742 l2tp_info(session, L2TP_MSG_SEQ,
743 "%s: requested to enable seq numbers by LNS\n",
744 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000745 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000746 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000747 }
748 } else {
749 /* No sequence numbers.
750 * If user has configured mandatory sequence numbers, discard.
751 */
752 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000753 l2tp_warn(session, L2TP_MSG_SEQ,
754 "%s: recv data has no seq numbers when required. Discarding.\n",
755 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000756 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000757 goto discard;
758 }
759
760 /* If we're the LAC and we're sending sequence numbers, the
761 * LNS has requested that we no longer send sequence numbers.
762 * If we're the LNS and we're sending sequence numbers, the
763 * LAC is broken. Discard the frame.
764 */
765 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000766 l2tp_info(session, L2TP_MSG_SEQ,
767 "%s: requested to disable seq numbers by LNS\n",
768 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000769 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000770 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000771 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000772 l2tp_warn(session, L2TP_MSG_SEQ,
773 "%s: recv data has no seq numbers when required. Discarding.\n",
774 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000775 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000776 goto discard;
777 }
778 }
779
James Chapman900631e2018-01-03 22:48:06 +0000780 /* Session data offset is defined only for L2TPv2 and is
781 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000782 */
783 if (tunnel->version == L2TP_HDR_VER_2) {
784 /* If offset bit set, skip it. */
785 if (hdrflags & L2TP_HDRFLAG_O) {
786 offset = ntohs(*(__be16 *)ptr);
787 ptr += 2 + offset;
788 }
James Chapman900631e2018-01-03 22:48:06 +0000789 }
James Chapmanfd558d12010-04-02 06:18:33 +0000790
791 offset = ptr - optr;
792 if (!pskb_may_pull(skb, offset))
793 goto discard;
794
795 __skb_pull(skb, offset);
796
797 /* If caller wants to process the payload before we queue the
798 * packet, do so now.
799 */
800 if (payload_hook)
801 if ((*payload_hook)(skb))
802 goto discard;
803
804 /* Prepare skb for adding to the session's reorder_q. Hold
805 * packets for max reorder_timeout or 1 second if not
806 * reordering.
807 */
808 L2TP_SKB_CB(skb)->length = length;
809 L2TP_SKB_CB(skb)->expires = jiffies +
810 (session->reorder_timeout ? session->reorder_timeout : HZ);
811
812 /* Add packet to the session's receive queue. Reordering is done here, if
813 * enabled. Saved L2TP protocol info is stored in skb->sb[].
814 */
815 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100816 if (l2tp_recv_data_seq(session, skb))
817 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000818 } else {
819 /* No sequence numbers. Add the skb to the tail of the
820 * reorder queue. This ensures that it will be
821 * delivered after all previous sequenced skbs.
822 */
823 skb_queue_tail(&session->reorder_q, skb);
824 }
825
826 /* Try to dequeue as many skbs from reorder_q as we can. */
827 l2tp_recv_dequeue(session);
828
James Chapmanf7faffa2010-04-02 06:18:49 +0000829 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000830
831discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000832 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000833 kfree_skb(skb);
James Chapmanf7faffa2010-04-02 06:18:49 +0000834}
835EXPORT_SYMBOL(l2tp_recv_common);
836
Tom Parkin48f72f92013-03-19 06:11:19 +0000837/* Drop skbs from the session's reorder_q
838 */
839int l2tp_session_queue_purge(struct l2tp_session *session)
840{
841 struct sk_buff *skb = NULL;
842 BUG_ON(!session);
843 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
844 while ((skb = skb_dequeue(&session->reorder_q))) {
845 atomic_long_inc(&session->stats.rx_errors);
846 kfree_skb(skb);
Tom Parkin48f72f92013-03-19 06:11:19 +0000847 }
848 return 0;
849}
850EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
851
James Chapmanf7faffa2010-04-02 06:18:49 +0000852/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
853 * here. The skb is not on a list when we get here.
854 * Returns 0 if the packet was a data packet and was successfully passed on.
855 * Returns 1 if the packet was not a good data packet and could not be
856 * forwarded. All such packets are passed up to userspace to deal with.
857 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000858static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
859 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000860{
861 struct l2tp_session *session = NULL;
862 unsigned char *ptr, *optr;
863 u16 hdrflags;
864 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000865 u16 version;
866 int length;
867
Tom Herbert58d60852014-05-07 16:52:48 -0700868 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000869
870 /* UDP always verifies the packet length. */
871 __skb_pull(skb, sizeof(struct udphdr));
872
873 /* Short packet? */
874 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000875 l2tp_info(tunnel, L2TP_MSG_DATA,
876 "%s: recv short packet (len=%d)\n",
877 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000878 goto error;
879 }
880
James Chapmanf7faffa2010-04-02 06:18:49 +0000881 /* Trace packet contents, if enabled */
882 if (tunnel->debug & L2TP_MSG_DATA) {
883 length = min(32u, skb->len);
884 if (!pskb_may_pull(skb, length))
885 goto error;
886
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000887 pr_debug("%s: recv\n", tunnel->name);
888 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000889 }
890
Eric Dumazete50e7052011-11-08 13:59:44 -0500891 /* Point to L2TP header */
892 optr = ptr = skb->data;
893
James Chapmanf7faffa2010-04-02 06:18:49 +0000894 /* Get L2TP header flags */
895 hdrflags = ntohs(*(__be16 *) ptr);
896
897 /* Check protocol version */
898 version = hdrflags & L2TP_HDR_VER_MASK;
899 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000900 l2tp_info(tunnel, L2TP_MSG_DATA,
901 "%s: recv protocol version mismatch: got %d expected %d\n",
902 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000903 goto error;
904 }
905
906 /* Get length of L2TP packet */
907 length = skb->len;
908
909 /* If type is control packet, it is handled by userspace. */
910 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000911 l2tp_dbg(tunnel, L2TP_MSG_DATA,
912 "%s: recv control packet, len=%d\n",
913 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000914 goto error;
915 }
916
917 /* Skip flags */
918 ptr += 2;
919
920 if (tunnel->version == L2TP_HDR_VER_2) {
921 /* If length is present, skip it */
922 if (hdrflags & L2TP_HDRFLAG_L)
923 ptr += 2;
924
925 /* Extract tunnel and session ID */
926 tunnel_id = ntohs(*(__be16 *) ptr);
927 ptr += 2;
928 session_id = ntohs(*(__be16 *) ptr);
929 ptr += 2;
930 } else {
931 ptr += 2; /* skip reserved bits */
932 tunnel_id = tunnel->tunnel_id;
933 session_id = ntohl(*(__be32 *) ptr);
934 ptr += 4;
935 }
936
937 /* Find the session context */
Guillaume Naulta4346212017-10-31 17:36:42 +0100938 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000939 if (!session || !session->recv_skb) {
Guillaume Naulta4346212017-10-31 17:36:42 +0100940 if (session)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200941 l2tp_session_dec_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200942
James Chapmanf7faffa2010-04-02 06:18:49 +0000943 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000944 l2tp_info(tunnel, L2TP_MSG_DATA,
945 "%s: no session found (%u/%u). Passing up.\n",
946 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000947 goto error;
948 }
949
950 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200951 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000952
953 return 0;
954
James Chapmanfd558d12010-04-02 06:18:33 +0000955error:
956 /* Put UDP header back */
957 __skb_push(skb, sizeof(struct udphdr));
958
959 return 1;
960}
James Chapmanfd558d12010-04-02 06:18:33 +0000961
962/* UDP encapsulation receive handler. See net/ipv4/udp.c.
963 * Return codes:
964 * 0 : success.
965 * <0: error
966 * >0: skb should be passed up to userspace as UDP.
967 */
968int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
969{
970 struct l2tp_tunnel *tunnel;
971
972 tunnel = l2tp_sock_to_tunnel(sk);
973 if (tunnel == NULL)
974 goto pass_up;
975
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000976 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
977 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000978
979 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
980 goto pass_up_put;
981
982 sock_put(sk);
983 return 0;
984
985pass_up_put:
986 sock_put(sk);
987pass_up:
988 return 1;
989}
990EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
991
992/************************************************************************
993 * Transmit handling
994 ***********************************************************************/
995
996/* Build an L2TP header for the session into the buffer provided.
997 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000998static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000999{
James Chapmanf7faffa2010-04-02 06:18:49 +00001000 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001001 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001002 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001003 u16 flags = L2TP_HDR_VER_2;
1004 u32 tunnel_id = tunnel->peer_tunnel_id;
1005 u32 session_id = session->peer_session_id;
1006
1007 if (session->send_seq)
1008 flags |= L2TP_HDRFLAG_S;
1009
1010 /* Setup L2TP header. */
1011 *bufp++ = htons(flags);
1012 *bufp++ = htons(tunnel_id);
1013 *bufp++ = htons(session_id);
1014 if (session->send_seq) {
1015 *bufp++ = htons(session->ns);
1016 *bufp++ = 0;
1017 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001018 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001019 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1020 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001021 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001022
1023 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001024}
1025
James Chapmanf7faffa2010-04-02 06:18:49 +00001026static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001027{
James Chapman0d767512010-04-02 06:19:00 +00001028 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001029 char *bufp = buf;
1030 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001031
James Chapman0d767512010-04-02 06:19:00 +00001032 /* Setup L2TP header. The header differs slightly for UDP and
1033 * IP encapsulations. For UDP, there is 4 bytes of flags.
1034 */
1035 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1036 u16 flags = L2TP_HDR_VER_3;
1037 *((__be16 *) bufp) = htons(flags);
1038 bufp += 2;
1039 *((__be16 *) bufp) = 0;
1040 bufp += 2;
1041 }
1042
James Chapmanf7faffa2010-04-02 06:18:49 +00001043 *((__be32 *) bufp) = htonl(session->peer_session_id);
1044 bufp += 4;
1045 if (session->cookie_len) {
1046 memcpy(bufp, &session->cookie[0], session->cookie_len);
1047 bufp += session->cookie_len;
1048 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001049 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1050 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +00001051
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001052 if (session->send_seq) {
1053 l2h = 0x40000000 | session->ns;
1054 session->ns++;
1055 session->ns &= 0xffffff;
1056 l2tp_dbg(session, L2TP_MSG_SEQ,
1057 "%s: updated ns to %u\n",
1058 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001059 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001060
1061 *((__be32 *)bufp) = htonl(l2h);
1062 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001063 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001064
1065 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001066}
James Chapmanfd558d12010-04-02 06:18:33 +00001067
stephen hemmingerfc130842010-10-21 07:50:46 +00001068static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001069 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001070{
1071 struct l2tp_tunnel *tunnel = session->tunnel;
1072 unsigned int len = skb->len;
1073 int error;
1074
1075 /* Debug */
1076 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001077 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001078 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001079 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001080 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001081 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001082
1083 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001084 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1085 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001086
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001087 pr_debug("%s: xmit\n", session->name);
1088 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1089 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001090 }
1091
1092 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001093 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001094#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001095 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001096 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001097 else
1098#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001099 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001100
1101 /* Update stats */
1102 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001103 atomic_long_inc(&tunnel->stats.tx_packets);
1104 atomic_long_add(len, &tunnel->stats.tx_bytes);
1105 atomic_long_inc(&session->stats.tx_packets);
1106 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001107 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001108 atomic_long_inc(&tunnel->stats.tx_errors);
1109 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001110 }
1111
1112 return 0;
1113}
James Chapmanfd558d12010-04-02 06:18:33 +00001114
James Chapmanfd558d12010-04-02 06:18:33 +00001115/* If caller requires the skb to have a ppp header, the header must be
1116 * inserted in the skb data before calling this function.
1117 */
1118int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1119{
1120 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001121 struct l2tp_tunnel *tunnel = session->tunnel;
1122 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001123 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001124 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001125 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001126 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001127 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1128 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001129 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001130
1131 /* Check that there's enough headroom in the skb to insert IP,
1132 * UDP and L2TP headers. If not enough, expand it to
1133 * make room. Adjust truesize.
1134 */
1135 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001136 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001137 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001138 kfree_skb(skb);
1139 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001140 }
James Chapmanfd558d12010-04-02 06:18:33 +00001141
James Chapmanfd558d12010-04-02 06:18:33 +00001142 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001143 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001144
James Chapman0d767512010-04-02 06:19:00 +00001145 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001146 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1147 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1148 IPSKB_REROUTED);
1149 nf_reset(skb);
1150
David S. Miller6af88da2011-05-08 13:45:20 -07001151 bh_lock_sock(sk);
1152 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001153 kfree_skb(skb);
1154 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001155 goto out_unlock;
1156 }
1157
James Chapmanfd558d12010-04-02 06:18:33 +00001158 /* Get routing info from the tunnel socket */
1159 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001160 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001161
David S. Millerd9d8da82011-05-06 22:23:20 -07001162 inet = inet_sk(sk);
1163 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001164 switch (tunnel->encap) {
1165 case L2TP_ENCAPTYPE_UDP:
1166 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001167 __skb_push(skb, sizeof(*uh));
1168 skb_reset_transport_header(skb);
1169 uh = udp_hdr(skb);
1170 uh->source = inet->inet_sport;
1171 uh->dest = inet->inet_dport;
1172 udp_len = uhlen + hdr_len + data_len;
1173 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001174
1175 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001176#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001177 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001178 udp6_set_csum(udp_get_no_check6_tx(sk),
1179 skb, &inet6_sk(sk)->saddr,
1180 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001181 else
1182#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001183 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1184 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001185 break;
1186
1187 case L2TP_ENCAPTYPE_IP:
1188 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001189 }
1190
David S. Millerd9d8da82011-05-06 22:23:20 -07001191 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001192out_unlock:
1193 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001194
Eric Dumazetb8c84302012-06-28 20:15:13 +00001195 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001196}
1197EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1198
1199/*****************************************************************************
1200 * Tinnel and session create/destroy.
1201 *****************************************************************************/
1202
1203/* Tunnel socket destruct hook.
1204 * The tunnel context is deleted only when all session sockets have been
1205 * closed.
1206 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001207static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001208{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001209 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001210 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001211
James Chapmanfd558d12010-04-02 06:18:33 +00001212 if (tunnel == NULL)
1213 goto end;
1214
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001215 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001216
James Chapmanfd558d12010-04-02 06:18:33 +00001217
Tom Parkinf8ccac02013-01-31 23:43:00 +00001218 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001219 switch (tunnel->encap) {
1220 case L2TP_ENCAPTYPE_UDP:
1221 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1222 (udp_sk(sk))->encap_type = 0;
1223 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001224 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001225 break;
1226 case L2TP_ENCAPTYPE_IP:
1227 break;
1228 }
James Chapmanfd558d12010-04-02 06:18:33 +00001229
1230 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001231 sk->sk_destruct = tunnel->old_sk_destruct;
1232 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001233
1234 /* Remove the tunnel struct from the tunnel list */
1235 pn = l2tp_pernet(tunnel->l2tp_net);
1236 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1237 list_del_rcu(&tunnel->list);
1238 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001239
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001240 tunnel->sock = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001241 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001242
1243 /* Call the original destructor */
1244 if (sk->sk_destruct)
1245 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001246end:
1247 return;
1248}
James Chapmanfd558d12010-04-02 06:18:33 +00001249
1250/* When the tunnel is closed, all the attached sessions need to go too.
1251 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001252void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001253{
1254 int hash;
1255 struct hlist_node *walk;
1256 struct hlist_node *tmp;
1257 struct l2tp_session *session;
1258
1259 BUG_ON(tunnel == NULL);
1260
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001261 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1262 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001263
1264 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001265 tunnel->acpt_newsess = false;
James Chapmanfd558d12010-04-02 06:18:33 +00001266 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1267again:
1268 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1269 session = hlist_entry(walk, struct l2tp_session, hlist);
1270
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001271 l2tp_info(session, L2TP_MSG_CONTROL,
1272 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001273
1274 hlist_del_init(&session->hlist);
1275
Guillaume Naultb228a942017-09-22 15:39:24 +02001276 if (test_and_set_bit(0, &session->dead))
1277 goto again;
1278
James Chapmanfd558d12010-04-02 06:18:33 +00001279 write_unlock_bh(&tunnel->hlist_lock);
1280
Tom Parkinf6e16b22013-03-19 06:11:23 +00001281 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001282 l2tp_session_queue_purge(session);
1283
James Chapmanfd558d12010-04-02 06:18:33 +00001284 if (session->session_close != NULL)
1285 (*session->session_close)(session);
1286
Tom Parkin9980d002013-03-19 06:11:13 +00001287 l2tp_session_dec_refcount(session);
1288
James Chapmanfd558d12010-04-02 06:18:33 +00001289 write_lock_bh(&tunnel->hlist_lock);
1290
1291 /* Now restart from the beginning of this hash
1292 * chain. We always remove a session from the
1293 * list so we are guaranteed to make forward
1294 * progress.
1295 */
1296 goto again;
1297 }
1298 }
1299 write_unlock_bh(&tunnel->hlist_lock);
1300}
Tom Parkine34f4c72013-03-19 06:11:14 +00001301EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001302
Tom Parkin9980d002013-03-19 06:11:13 +00001303/* Tunnel socket destroy hook for UDP encapsulation */
1304static void l2tp_udp_encap_destroy(struct sock *sk)
1305{
1306 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1307 if (tunnel) {
1308 l2tp_tunnel_closeall(tunnel);
1309 sock_put(sk);
1310 }
1311}
1312
Tom Parkinf8ccac02013-01-31 23:43:00 +00001313/* Workqueue tunnel deletion function */
1314static void l2tp_tunnel_del_work(struct work_struct *work)
1315{
1316 struct l2tp_tunnel *tunnel = NULL;
1317 struct socket *sock = NULL;
1318 struct sock *sk = NULL;
1319
1320 tunnel = container_of(work, struct l2tp_tunnel, del_work);
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001321
1322 l2tp_tunnel_closeall(tunnel);
1323
Tom Parkinf8ccac02013-01-31 23:43:00 +00001324 sk = l2tp_tunnel_sock_lookup(tunnel);
1325 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001326 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001327
1328 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001329
Tom Parkin02d13ed2013-03-19 06:11:18 +00001330 /* If the tunnel socket was created by userspace, then go through the
1331 * inet layer to shut the socket down, and let userspace close it.
1332 * Otherwise, if we created the socket directly within the kernel, use
1333 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001334 * In either case the tunnel resources are freed in the socket
1335 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001336 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001337 if (tunnel->fd >= 0) {
1338 if (sock)
1339 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001340 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001341 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001342 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001343 sock_release(sock);
1344 }
Tom Parkin167eb172013-01-31 23:43:03 +00001345 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001346
1347 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001348out:
1349 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001350}
James Chapmanfd558d12010-04-02 06:18:33 +00001351
James Chapman789a4a22010-04-02 06:19:40 +00001352/* Create a socket for the tunnel, if one isn't set up by
1353 * userspace. This is used for static tunnels where there is no
1354 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001355 *
1356 * Since we don't want these sockets to keep a namespace alive by
1357 * themselves, we drop the socket's namespace refcount after creation.
1358 * These sockets are freed when the namespace exits using the pernet
1359 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001360 */
Tom Parkin167eb172013-01-31 23:43:03 +00001361static int l2tp_tunnel_sock_create(struct net *net,
1362 u32 tunnel_id,
1363 u32 peer_tunnel_id,
1364 struct l2tp_tunnel_cfg *cfg,
1365 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001366{
1367 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001368 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001369 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001370
1371 switch (cfg->encap) {
1372 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001373 memset(&udp_conf, 0, sizeof(udp_conf));
1374
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001375#if IS_ENABLED(CONFIG_IPV6)
1376 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001377 udp_conf.family = AF_INET6;
1378 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1379 sizeof(udp_conf.local_ip6));
1380 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1381 sizeof(udp_conf.peer_ip6));
1382 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001383 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001384 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001385 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001386 } else
1387#endif
1388 {
Tom Herbert85644b42014-07-13 19:49:48 -07001389 udp_conf.family = AF_INET;
1390 udp_conf.local_ip = cfg->local_ip;
1391 udp_conf.peer_ip = cfg->peer_ip;
1392 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001393 }
James Chapman789a4a22010-04-02 06:19:40 +00001394
Tom Herbert85644b42014-07-13 19:49:48 -07001395 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1396 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1397
1398 err = udp_sock_create(net, &udp_conf, &sock);
1399 if (err < 0)
1400 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001401
1402 break;
1403
1404 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001405#if IS_ENABLED(CONFIG_IPV6)
1406 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001407 struct sockaddr_l2tpip6 ip6_addr = {0};
1408
Eric W. Biederman26abe142015-05-08 21:10:31 -05001409 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001410 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001411 if (err < 0)
1412 goto out;
1413
James Chapman5dac94e2012-04-29 21:48:55 +00001414 ip6_addr.l2tp_family = AF_INET6;
1415 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1416 sizeof(ip6_addr.l2tp_addr));
1417 ip6_addr.l2tp_conn_id = tunnel_id;
1418 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1419 sizeof(ip6_addr));
1420 if (err < 0)
1421 goto out;
1422
1423 ip6_addr.l2tp_family = AF_INET6;
1424 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1425 sizeof(ip6_addr.l2tp_addr));
1426 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1427 err = kernel_connect(sock,
1428 (struct sockaddr *) &ip6_addr,
1429 sizeof(ip6_addr), 0);
1430 if (err < 0)
1431 goto out;
1432 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001433#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001434 {
Tom Herbert85644b42014-07-13 19:49:48 -07001435 struct sockaddr_l2tpip ip_addr = {0};
1436
Eric W. Biederman26abe142015-05-08 21:10:31 -05001437 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001438 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001439 if (err < 0)
1440 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001441
James Chapman5dac94e2012-04-29 21:48:55 +00001442 ip_addr.l2tp_family = AF_INET;
1443 ip_addr.l2tp_addr = cfg->local_ip;
1444 ip_addr.l2tp_conn_id = tunnel_id;
1445 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1446 sizeof(ip_addr));
1447 if (err < 0)
1448 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001449
James Chapman5dac94e2012-04-29 21:48:55 +00001450 ip_addr.l2tp_family = AF_INET;
1451 ip_addr.l2tp_addr = cfg->peer_ip;
1452 ip_addr.l2tp_conn_id = peer_tunnel_id;
1453 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1454 sizeof(ip_addr), 0);
1455 if (err < 0)
1456 goto out;
1457 }
James Chapman789a4a22010-04-02 06:19:40 +00001458 break;
1459
1460 default:
1461 goto out;
1462 }
1463
1464out:
Tom Parkin167eb172013-01-31 23:43:03 +00001465 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001466 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001467 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001468 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001469 *sockp = NULL;
1470 }
1471
1472 return err;
1473}
1474
Eric Dumazet37159ef2012-09-04 07:18:57 +00001475static struct lock_class_key l2tp_socket_class;
1476
James Chapmanfd558d12010-04-02 06:18:33 +00001477int 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)
1478{
1479 struct l2tp_tunnel *tunnel = NULL;
1480 int err;
1481 struct socket *sock = NULL;
1482 struct sock *sk = NULL;
1483 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001484 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001485
1486 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001487 * the userspace L2TP daemon. If not specified, create a
1488 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001489 */
James Chapman789a4a22010-04-02 06:19:40 +00001490 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001491 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1492 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001493 if (err < 0)
1494 goto err;
1495 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001496 sock = sockfd_lookup(fd, &err);
1497 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001498 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001499 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001500 err = -EBADF;
1501 goto err;
1502 }
1503
1504 /* Reject namespace mismatches */
1505 if (!net_eq(sock_net(sock->sk), net)) {
1506 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1507 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001508 goto err;
1509 }
James Chapmanfd558d12010-04-02 06:18:33 +00001510 }
1511
1512 sk = sock->sk;
1513
James Chapman0d767512010-04-02 06:19:00 +00001514 if (cfg != NULL)
1515 encap = cfg->encap;
1516
James Chapmanfd558d12010-04-02 06:18:33 +00001517 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001518 switch (encap) {
1519 case L2TP_ENCAPTYPE_UDP:
1520 err = -EPROTONOSUPPORT;
1521 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001522 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001523 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1524 goto err;
1525 }
1526 break;
1527 case L2TP_ENCAPTYPE_IP:
1528 err = -EPROTONOSUPPORT;
1529 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001530 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001531 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1532 goto err;
1533 }
1534 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001535 }
1536
1537 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001538 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001539 if (tunnel != NULL) {
1540 /* This socket has already been prepped */
1541 err = -EBUSY;
1542 goto err;
1543 }
1544
James Chapmanfd558d12010-04-02 06:18:33 +00001545 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1546 if (tunnel == NULL) {
1547 err = -ENOMEM;
1548 goto err;
1549 }
1550
1551 tunnel->version = version;
1552 tunnel->tunnel_id = tunnel_id;
1553 tunnel->peer_tunnel_id = peer_tunnel_id;
1554 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1555
1556 tunnel->magic = L2TP_TUNNEL_MAGIC;
1557 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1558 rwlock_init(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001559 tunnel->acpt_newsess = true;
James Chapmanfd558d12010-04-02 06:18:33 +00001560
1561 /* The net we belong to */
1562 tunnel->l2tp_net = net;
1563 pn = l2tp_pernet(net);
1564
James Chapman0d767512010-04-02 06:19:00 +00001565 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001566 tunnel->debug = cfg->debug;
1567
François Cachereule18503f2013-10-02 10:16:02 +02001568#if IS_ENABLED(CONFIG_IPV6)
1569 if (sk->sk_family == PF_INET6) {
1570 struct ipv6_pinfo *np = inet6_sk(sk);
1571
1572 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001573 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001574 struct inet_sock *inet = inet_sk(sk);
1575
1576 tunnel->v4mapped = true;
1577 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001578 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1579 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001580 } else {
1581 tunnel->v4mapped = false;
1582 }
1583 }
1584#endif
1585
James Chapmanfd558d12010-04-02 06:18:33 +00001586 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001587 tunnel->encap = encap;
1588 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001589 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001590
Andy Zhouc8fffce2014-09-16 17:31:19 -07001591 udp_cfg.sk_user_data = tunnel;
1592 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1593 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1594 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1595
1596 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1597 } else {
1598 sk->sk_user_data = tunnel;
1599 }
James Chapmanfd558d12010-04-02 06:18:33 +00001600
1601 /* Hook on the tunnel socket destructor so that we can cleanup
1602 * if the tunnel socket goes away.
1603 */
1604 tunnel->old_sk_destruct = sk->sk_destruct;
1605 sk->sk_destruct = &l2tp_tunnel_destruct;
1606 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001607 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001608 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1609
James Chapmanfd558d12010-04-02 06:18:33 +00001610 sk->sk_allocation = GFP_ATOMIC;
1611
Tom Parkinf8ccac02013-01-31 23:43:00 +00001612 /* Init delete workqueue struct */
1613 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1614
James Chapmanfd558d12010-04-02 06:18:33 +00001615 /* Add tunnel to our list */
1616 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001617
1618 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001619 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001620 */
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001621 refcount_set(&tunnel->ref_count, 1);
Eric Dumazet17691922011-05-11 18:22:36 +00001622 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1623 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1624 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001625
1626 err = 0;
1627err:
1628 if (tunnelp)
1629 *tunnelp = tunnel;
1630
James Chapman789a4a22010-04-02 06:19:40 +00001631 /* If tunnel's socket was created by the kernel, it doesn't
1632 * have a file.
1633 */
1634 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001635 sockfd_put(sock);
1636
1637 return err;
1638}
1639EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1640
James Chapman309795f2010-04-02 06:19:10 +00001641/* This function is used by the netlink TUNNEL_DELETE command.
1642 */
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001643void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001644{
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001645 if (!test_and_set_bit(0, &tunnel->dead)) {
1646 l2tp_tunnel_inc_refcount(tunnel);
1647 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001648 }
James Chapman309795f2010-04-02 06:19:10 +00001649}
1650EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1651
James Chapmanfd558d12010-04-02 06:18:33 +00001652/* Really kill the session.
1653 */
1654void l2tp_session_free(struct l2tp_session *session)
1655{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001656 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001657
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001658 BUG_ON(refcount_read(&session->ref_count) != 0);
James Chapmanfd558d12010-04-02 06:18:33 +00001659
Tom Parkinf6e16b22013-03-19 06:11:23 +00001660 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001661 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001662 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001663 session->tunnel = NULL;
1664 l2tp_tunnel_dec_refcount(tunnel);
1665 }
1666
1667 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001668}
1669EXPORT_SYMBOL_GPL(l2tp_session_free);
1670
Tom Parkinf6e16b22013-03-19 06:11:23 +00001671/* Remove an l2tp session from l2tp_core's hash lists.
1672 * Provides a tidyup interface for pseudowire code which can't just route all
1673 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1674 * callback.
1675 */
1676void __l2tp_session_unhash(struct l2tp_session *session)
1677{
1678 struct l2tp_tunnel *tunnel = session->tunnel;
1679
1680 /* Remove the session from core hashes */
1681 if (tunnel) {
1682 /* Remove from the per-tunnel hash */
1683 write_lock_bh(&tunnel->hlist_lock);
1684 hlist_del_init(&session->hlist);
1685 write_unlock_bh(&tunnel->hlist_lock);
1686
1687 /* For L2TPv3 we have a per-net hash: remove from there, too */
1688 if (tunnel->version != L2TP_HDR_VER_2) {
1689 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1690 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1691 hlist_del_init_rcu(&session->global_hlist);
1692 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1693 synchronize_rcu();
1694 }
1695 }
1696}
1697EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1698
James Chapman309795f2010-04-02 06:19:10 +00001699/* This function is used by the netlink SESSION_DELETE command and by
1700 pseudowire modules.
1701 */
1702int l2tp_session_delete(struct l2tp_session *session)
1703{
Guillaume Naultb228a942017-09-22 15:39:24 +02001704 if (test_and_set_bit(0, &session->dead))
1705 return 0;
1706
Tom Parkinf6e16b22013-03-19 06:11:23 +00001707 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001708 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001709 if (session->session_close != NULL)
1710 (*session->session_close)(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001711
James Chapman309795f2010-04-02 06:19:10 +00001712 l2tp_session_dec_refcount(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001713
James Chapman309795f2010-04-02 06:19:10 +00001714 return 0;
1715}
1716EXPORT_SYMBOL_GPL(l2tp_session_delete);
1717
James Chapmanf7faffa2010-04-02 06:18:49 +00001718/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001719 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001720 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001721void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001722{
1723 if (version == L2TP_HDR_VER_2) {
1724 session->hdr_len = 6;
1725 if (session->send_seq)
1726 session->hdr_len += 4;
1727 } else {
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001728 session->hdr_len = 4 + session->cookie_len;
1729 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001730 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1731 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001732 }
1733
1734}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001735EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001736
James Chapmanfd558d12010-04-02 06:18:33 +00001737struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1738{
1739 struct l2tp_session *session;
1740
1741 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1742 if (session != NULL) {
1743 session->magic = L2TP_SESSION_MAGIC;
1744 session->tunnel = tunnel;
1745
1746 session->session_id = session_id;
1747 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001748 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001749 if (tunnel->version == L2TP_HDR_VER_2)
1750 session->nr_max = 0xffff;
1751 else
1752 session->nr_max = 0xffffff;
1753 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001754 session->nr_oos_count_max = 4;
1755
1756 /* Use NR of first received packet */
1757 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001758
1759 sprintf(&session->name[0], "sess %u/%u",
1760 tunnel->tunnel_id, session->session_id);
1761
1762 skb_queue_head_init(&session->reorder_q);
1763
1764 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001765 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001766
1767 /* Inherit debug options from tunnel */
1768 session->debug = tunnel->debug;
1769
1770 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001771 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001772 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001773 session->mtu = cfg->mtu;
1774 session->mru = cfg->mru;
1775 session->send_seq = cfg->send_seq;
1776 session->recv_seq = cfg->recv_seq;
1777 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001778 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001779 session->l2specific_type = cfg->l2specific_type;
1780 session->l2specific_len = cfg->l2specific_len;
1781 session->cookie_len = cfg->cookie_len;
1782 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1783 session->peer_cookie_len = cfg->peer_cookie_len;
1784 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001785 }
1786
James Chapmanf7faffa2010-04-02 06:18:49 +00001787 if (tunnel->version == L2TP_HDR_VER_2)
1788 session->build_header = l2tp_build_l2tpv2_header;
1789 else
1790 session->build_header = l2tp_build_l2tpv3_header;
1791
1792 l2tp_session_set_header_len(session, tunnel->version);
1793
Guillaume Nault9ee369a2017-08-25 16:22:17 +02001794 refcount_set(&session->ref_count, 1);
1795
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001796 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001797 }
1798
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001799 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001800}
1801EXPORT_SYMBOL_GPL(l2tp_session_create);
1802
1803/*****************************************************************************
1804 * Init and cleanup
1805 *****************************************************************************/
1806
1807static __net_init int l2tp_init_net(struct net *net)
1808{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001809 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001810 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001811
James Chapmanfd558d12010-04-02 06:18:33 +00001812 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001813 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001814
James Chapmanf7faffa2010-04-02 06:18:49 +00001815 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1816 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1817
James Chapmane02d4942010-04-02 06:19:16 +00001818 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001819
James Chapmanfd558d12010-04-02 06:18:33 +00001820 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001821}
1822
Tom Parkin167eb172013-01-31 23:43:03 +00001823static __net_exit void l2tp_exit_net(struct net *net)
1824{
1825 struct l2tp_net *pn = l2tp_pernet(net);
1826 struct l2tp_tunnel *tunnel = NULL;
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001827 int hash;
Tom Parkin167eb172013-01-31 23:43:03 +00001828
1829 rcu_read_lock_bh();
1830 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slaby4dc12ff2017-10-25 15:57:55 +02001831 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001832 }
1833 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001834
1835 flush_workqueue(l2tp_wq);
1836 rcu_barrier();
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001837
1838 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1839 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
Tom Parkin167eb172013-01-31 23:43:03 +00001840}
1841
James Chapmanfd558d12010-04-02 06:18:33 +00001842static struct pernet_operations l2tp_net_ops = {
1843 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001844 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001845 .id = &l2tp_net_id,
1846 .size = sizeof(struct l2tp_net),
1847};
1848
1849static int __init l2tp_init(void)
1850{
1851 int rc = 0;
1852
1853 rc = register_pernet_device(&l2tp_net_ops);
1854 if (rc)
1855 goto out;
1856
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001857 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001858 if (!l2tp_wq) {
1859 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001860 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001861 rc = -ENOMEM;
1862 goto out;
1863 }
1864
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001865 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001866
1867out:
1868 return rc;
1869}
1870
1871static void __exit l2tp_exit(void)
1872{
1873 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001874 if (l2tp_wq) {
1875 destroy_workqueue(l2tp_wq);
1876 l2tp_wq = NULL;
1877 }
James Chapmanfd558d12010-04-02 06:18:33 +00001878}
1879
1880module_init(l2tp_init);
1881module_exit(l2tp_exit);
1882
1883MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1884MODULE_DESCRIPTION("L2TP core");
1885MODULE_LICENSE("GPL");
1886MODULE_VERSION(L2TP_DRV_VERSION);
1887