blob: e702cb95b89b7284d42169e80abc05b5b1071fe7 [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
103static atomic_t l2tp_tunnel_count;
104static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000105static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000106
107/* per-net private data for this module */
108static unsigned int l2tp_net_id;
109struct l2tp_net {
110 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000111 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000112 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000113 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000114};
115
stephen hemmingerfc130842010-10-21 07:50:46 +0000116static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000117
David S. Miller8d8a51e2013-10-08 15:44:26 -0400118static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
119{
120 return sk->sk_user_data;
121}
122
James Chapmanfd558d12010-04-02 06:18:33 +0000123static inline struct l2tp_net *l2tp_pernet(struct net *net)
124{
125 BUG_ON(!net);
126
127 return net_generic(net, l2tp_net_id);
128}
129
stephen hemmingerfc130842010-10-21 07:50:46 +0000130/* Tunnel reference counts. Incremented per session that is added to
131 * the tunnel.
132 */
133static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 atomic_inc(&tunnel->ref_count);
136}
137
138static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139{
140 if (atomic_dec_and_test(&tunnel->ref_count))
141 l2tp_tunnel_free(tunnel);
142}
143#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000144#define l2tp_tunnel_inc_refcount(_t) \
145do { \
146 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
147 __func__, __LINE__, (_t)->name, \
148 atomic_read(&_t->ref_count)); \
149 l2tp_tunnel_inc_refcount_1(_t); \
150} while (0)
Andy Zhou29abe2f2014-09-03 13:16:54 -0700151#define l2tp_tunnel_dec_refcount(_t) \
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000152do { \
153 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
154 __func__, __LINE__, (_t)->name, \
155 atomic_read(&_t->ref_count)); \
156 l2tp_tunnel_dec_refcount_1(_t); \
157} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000158#else
159#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
160#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
161#endif
162
James Chapmanf7faffa2010-04-02 06:18:49 +0000163/* Session hash global list for L2TPv3.
164 * The session_id SHOULD be random according to RFC3931, but several
165 * L2TP implementations use incrementing session_ids. So we do a real
166 * hash on the session_id, rather than a simple bitmask.
167 */
168static inline struct hlist_head *
169l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
170{
171 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
172
173}
174
Tom Parkin80d84ef2013-01-22 05:13:48 +0000175/* Lookup the tunnel socket, possibly involving the fs code if the socket is
176 * owned by userspace. A struct sock returned from this function must be
177 * released using l2tp_tunnel_sock_put once you're done with it.
178 */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800179static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000180{
181 int err = 0;
182 struct socket *sock = NULL;
183 struct sock *sk = NULL;
184
185 if (!tunnel)
186 goto out;
187
188 if (tunnel->fd >= 0) {
189 /* Socket is owned by userspace, who might be in the process
190 * of closing it. Look the socket up using the fd to ensure
191 * consistency.
192 */
193 sock = sockfd_lookup(tunnel->fd, &err);
194 if (sock)
195 sk = sock->sk;
196 } else {
197 /* Socket is owned by kernelspace */
198 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000199 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000200 }
201
202out:
203 return sk;
204}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000205
206/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800207static void l2tp_tunnel_sock_put(struct sock *sk)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000208{
209 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
210 if (tunnel) {
211 if (tunnel->fd >= 0) {
212 /* Socket is owned by userspace */
213 sockfd_put(sk->sk_socket);
214 }
215 sock_put(sk);
216 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000217 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000218}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000219
James Chapmanf7faffa2010-04-02 06:18:49 +0000220/* Lookup a session by id in the global session list
221 */
222static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
223{
224 struct l2tp_net *pn = l2tp_pernet(net);
225 struct hlist_head *session_list =
226 l2tp_session_id_hash_2(pn, session_id);
227 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000228
James Chapmane02d4942010-04-02 06:19:16 +0000229 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800230 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000231 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000232 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000233 return session;
234 }
235 }
James Chapmane02d4942010-04-02 06:19:16 +0000236 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000237
238 return NULL;
239}
240
James Chapmanfd558d12010-04-02 06:18:33 +0000241/* Session hash list.
242 * The session_id SHOULD be random according to RFC2661, but several
243 * L2TP implementations (Cisco and Microsoft) use incrementing
244 * session_ids. So we do a real hash on the session_id, rather than a
245 * simple bitmask.
246 */
247static inline struct hlist_head *
248l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
249{
250 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
251}
252
253/* Lookup a session by id
254 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000255struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000256{
James Chapmanf7faffa2010-04-02 06:18:49 +0000257 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000258 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000259
James Chapmanf7faffa2010-04-02 06:18:49 +0000260 /* In L2TPv3, session_ids are unique over all tunnels and we
261 * sometimes need to look them up before we know the
262 * tunnel.
263 */
264 if (tunnel == NULL)
265 return l2tp_session_find_2(net, session_id);
266
267 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000268 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800269 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000270 if (session->session_id == session_id) {
271 read_unlock_bh(&tunnel->hlist_lock);
272 return session;
273 }
274 }
275 read_unlock_bh(&tunnel->hlist_lock);
276
277 return NULL;
278}
279EXPORT_SYMBOL_GPL(l2tp_session_find);
280
Guillaume Naultb7902602017-04-03 12:03:13 +0200281struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
282 bool do_ref)
James Chapmanfd558d12010-04-02 06:18:33 +0000283{
284 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000285 struct l2tp_session *session;
286 int count = 0;
287
288 read_lock_bh(&tunnel->hlist_lock);
289 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800290 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000291 if (++count > nth) {
Guillaume Naultb7902602017-04-03 12:03:13 +0200292 l2tp_session_inc_refcount(session);
293 if (do_ref && session->ref)
294 session->ref(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000295 read_unlock_bh(&tunnel->hlist_lock);
296 return session;
297 }
298 }
299 }
300
301 read_unlock_bh(&tunnel->hlist_lock);
302
303 return NULL;
304}
Guillaume Naultb7902602017-04-03 12:03:13 +0200305EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000306
James Chapman309795f2010-04-02 06:19:10 +0000307/* Lookup a session by interface name.
308 * This is very inefficient but is only used by management interfaces.
309 */
310struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
311{
312 struct l2tp_net *pn = l2tp_pernet(net);
313 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000314 struct l2tp_session *session;
315
James Chapmane02d4942010-04-02 06:19:16 +0000316 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000317 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800318 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000319 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000320 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000321 return session;
322 }
323 }
324 }
325
James Chapmane02d4942010-04-02 06:19:16 +0000326 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000327
328 return NULL;
329}
330EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
331
James Chapmanfd558d12010-04-02 06:18:33 +0000332/* Lookup a tunnel by id
333 */
334struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
335{
336 struct l2tp_tunnel *tunnel;
337 struct l2tp_net *pn = l2tp_pernet(net);
338
James Chapmane02d4942010-04-02 06:19:16 +0000339 rcu_read_lock_bh();
340 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000341 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000342 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000343 return tunnel;
344 }
345 }
James Chapmane02d4942010-04-02 06:19:16 +0000346 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000347
348 return NULL;
349}
350EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
351
352struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
353{
354 struct l2tp_net *pn = l2tp_pernet(net);
355 struct l2tp_tunnel *tunnel;
356 int count = 0;
357
James Chapmane02d4942010-04-02 06:19:16 +0000358 rcu_read_lock_bh();
359 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000360 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000361 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000362 return tunnel;
363 }
364 }
365
James Chapmane02d4942010-04-02 06:19:16 +0000366 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000367
368 return NULL;
369}
370EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
371
372/*****************************************************************************
373 * Receive data handling
374 *****************************************************************************/
375
376/* Queue a skb in order. We come here only if the skb has an L2TP sequence
377 * number.
378 */
379static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
380{
381 struct sk_buff *skbp;
382 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000383 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000384
385 spin_lock_bh(&session->reorder_q.lock);
386 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
387 if (L2TP_SKB_CB(skbp)->ns > ns) {
388 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000389 l2tp_dbg(session, L2TP_MSG_SEQ,
390 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
391 session->name, ns, L2TP_SKB_CB(skbp)->ns,
392 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000393 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000394 goto out;
395 }
396 }
397
398 __skb_queue_tail(&session->reorder_q, skb);
399
400out:
401 spin_unlock_bh(&session->reorder_q.lock);
402}
403
404/* Dequeue a single skb.
405 */
406static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
407{
408 struct l2tp_tunnel *tunnel = session->tunnel;
409 int length = L2TP_SKB_CB(skb)->length;
410
411 /* We're about to requeue the skb, so return resources
412 * to its current owner (a socket receive buffer).
413 */
414 skb_orphan(skb);
415
Tom Parkin7b7c0712013-03-19 06:11:22 +0000416 atomic_long_inc(&tunnel->stats.rx_packets);
417 atomic_long_add(length, &tunnel->stats.rx_bytes);
418 atomic_long_inc(&session->stats.rx_packets);
419 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000420
421 if (L2TP_SKB_CB(skb)->has_seq) {
422 /* Bump our Nr */
423 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100424 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000425
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000426 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
427 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000428 }
429
430 /* call private receive handler */
431 if (session->recv_skb != NULL)
432 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
433 else
434 kfree_skb(skb);
435
436 if (session->deref)
437 (*session->deref)(session);
438}
439
440/* Dequeue skbs from the session's reorder_q, subject to packet order.
441 * Skbs that have been in the queue for too long are simply discarded.
442 */
443static void l2tp_recv_dequeue(struct l2tp_session *session)
444{
445 struct sk_buff *skb;
446 struct sk_buff *tmp;
447
448 /* If the pkt at the head of the queue has the nr that we
449 * expect to send up next, dequeue it and any other
450 * in-sequence packets behind it.
451 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000452start:
James Chapmanfd558d12010-04-02 06:18:33 +0000453 spin_lock_bh(&session->reorder_q.lock);
454 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
455 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000456 atomic_long_inc(&session->stats.rx_seq_discards);
457 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000458 l2tp_dbg(session, L2TP_MSG_SEQ,
459 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
460 session->name, L2TP_SKB_CB(skb)->ns,
461 L2TP_SKB_CB(skb)->length, session->nr,
462 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000463 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000464 __skb_unlink(skb, &session->reorder_q);
465 kfree_skb(skb);
466 if (session->deref)
467 (*session->deref)(session);
468 continue;
469 }
470
471 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000472 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000473 l2tp_dbg(session, L2TP_MSG_SEQ,
474 "%s: advancing nr to next pkt: %u -> %u",
475 session->name, session->nr,
476 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000477 session->reorder_skip = 0;
478 session->nr = L2TP_SKB_CB(skb)->ns;
479 }
James Chapmanfd558d12010-04-02 06:18:33 +0000480 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000481 l2tp_dbg(session, L2TP_MSG_SEQ,
482 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
483 session->name, L2TP_SKB_CB(skb)->ns,
484 L2TP_SKB_CB(skb)->length, session->nr,
485 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000486 goto out;
487 }
488 }
489 __skb_unlink(skb, &session->reorder_q);
490
491 /* Process the skb. We release the queue lock while we
492 * do so to let other contexts process the queue.
493 */
494 spin_unlock_bh(&session->reorder_q.lock);
495 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000496 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000497 }
498
499out:
500 spin_unlock_bh(&session->reorder_q.lock);
501}
502
James Chapman8a1631d2013-07-02 20:28:59 +0100503static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
504{
505 u32 nws;
506
507 if (nr >= session->nr)
508 nws = nr - session->nr;
509 else
510 nws = (session->nr_max + 1) - (session->nr - nr);
511
512 return nws < session->nr_window_size;
513}
514
James Chapmanb6dc01a2013-07-02 20:28:58 +0100515/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
516 * acceptable, else non-zero.
517 */
518static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
519{
James Chapman8a1631d2013-07-02 20:28:59 +0100520 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
521 /* Packet sequence number is outside allowed window.
522 * Discard it.
523 */
524 l2tp_dbg(session, L2TP_MSG_SEQ,
525 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
526 session->name, L2TP_SKB_CB(skb)->ns,
527 L2TP_SKB_CB(skb)->length, session->nr);
528 goto discard;
529 }
530
James Chapmanb6dc01a2013-07-02 20:28:58 +0100531 if (session->reorder_timeout != 0) {
532 /* Packet reordering enabled. Add skb to session's
533 * reorder queue, in order of ns.
534 */
535 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100536 goto out;
537 }
538
539 /* Packet reordering disabled. Discard out-of-sequence packets, while
540 * tracking the number if in-sequence packets after the first OOS packet
541 * is seen. After nr_oos_count_max in-sequence packets, reset the
542 * sequence number to re-enable packet reception.
543 */
544 if (L2TP_SKB_CB(skb)->ns == session->nr) {
545 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100546 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100547 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
548 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
549
550 if (nr_oos == nr_next)
551 session->nr_oos_count++;
552 else
553 session->nr_oos_count = 0;
554
555 session->nr_oos = nr_oos;
556 if (session->nr_oos_count > session->nr_oos_count_max) {
557 session->reorder_skip = 1;
558 l2tp_dbg(session, L2TP_MSG_SEQ,
559 "%s: %d oos packets received. Resetting sequence numbers\n",
560 session->name, session->nr_oos_count);
561 }
562 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100563 atomic_long_inc(&session->stats.rx_seq_discards);
564 l2tp_dbg(session, L2TP_MSG_SEQ,
565 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
566 session->name, L2TP_SKB_CB(skb)->ns,
567 L2TP_SKB_CB(skb)->length, session->nr,
568 skb_queue_len(&session->reorder_q));
569 goto discard;
570 }
571 skb_queue_tail(&session->reorder_q, skb);
572 }
573
James Chapmana0dbd822013-07-02 20:29:00 +0100574out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100575 return 0;
576
577discard:
578 return 1;
579}
580
James Chapmanf7faffa2010-04-02 06:18:49 +0000581/* Do receive processing of L2TP data frames. We handle both L2TPv2
582 * and L2TPv3 data frames here.
583 *
584 * L2TPv2 Data Message Header
585 *
586 * 0 1 2 3
587 * 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
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 * | Tunnel ID | Session ID |
592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
593 * | Ns (opt) | Nr (opt) |
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * | Offset Size (opt) | Offset pad... (opt)
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 *
598 * Data frames are marked by T=0. All other fields are the same as
599 * those in L2TP control frames.
600 *
601 * L2TPv3 Data Message Header
602 *
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 * | L2TP Session Header |
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * | L2-Specific Sublayer |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | Tunnel Payload ...
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 *
611 * L2TPv3 Session Header Over IP
612 *
613 * 0 1 2 3
614 * 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
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * | Session ID |
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
618 * | Cookie (optional, maximum 64 bits)...
619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
620 * |
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
622 *
623 * L2TPv3 L2-Specific Sublayer Format
624 *
625 * 0 1 2 3
626 * 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
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * |x|S|x|x|x|x|x|x| Sequence Number |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 *
631 * Cookie value, sublayer format and offset (pad) are negotiated with
632 * the peer when the session is set up. Unlike L2TPv2, we do not need
633 * to parse the packet header to determine if optional fields are
634 * present.
635 *
636 * Caller must already have parsed the frame and determined that it is
637 * a data (not control) frame before coming here. Fields up to the
638 * session-id have already been parsed and ptr points to the data
639 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000640 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000641void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
642 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
643 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000644{
James Chapmanf7faffa2010-04-02 06:18:49 +0000645 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000646 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000647 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000648
649 /* The ref count is increased since we now hold a pointer to
650 * the session. Take care to decrement the refcnt when exiting
651 * this function from now on...
652 */
653 l2tp_session_inc_refcount(session);
654 if (session->ref)
655 (*session->ref)(session);
656
James Chapmanf7faffa2010-04-02 06:18:49 +0000657 /* Parse and check optional cookie */
658 if (session->peer_cookie_len > 0) {
659 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000660 l2tp_info(tunnel, L2TP_MSG_DATA,
661 "%s: cookie mismatch (%u/%u). Discarding.\n",
662 tunnel->name, tunnel->tunnel_id,
663 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000664 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000665 goto discard;
666 }
667 ptr += session->peer_cookie_len;
668 }
669
James Chapmanfd558d12010-04-02 06:18:33 +0000670 /* Handle the optional sequence numbers. Sequence numbers are
671 * in different places for L2TPv2 and L2TPv3.
672 *
673 * If we are the LAC, enable/disable sequence numbers under
674 * the control of the LNS. If no sequence numbers present but
675 * we were expecting them, discard frame.
676 */
677 ns = nr = 0;
678 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000679 if (tunnel->version == L2TP_HDR_VER_2) {
680 if (hdrflags & L2TP_HDRFLAG_S) {
681 ns = ntohs(*(__be16 *) ptr);
682 ptr += 2;
683 nr = ntohs(*(__be16 *) ptr);
684 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000685
James Chapmanf7faffa2010-04-02 06:18:49 +0000686 /* Store L2TP info in the skb */
687 L2TP_SKB_CB(skb)->ns = ns;
688 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000689
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000690 l2tp_dbg(session, L2TP_MSG_SEQ,
691 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
692 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000693 }
694 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
695 u32 l2h = ntohl(*(__be32 *) ptr);
696
697 if (l2h & 0x40000000) {
698 ns = l2h & 0x00ffffff;
699
700 /* Store L2TP info in the skb */
701 L2TP_SKB_CB(skb)->ns = ns;
702 L2TP_SKB_CB(skb)->has_seq = 1;
703
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000704 l2tp_dbg(session, L2TP_MSG_SEQ,
705 "%s: recv data ns=%u, session nr=%u\n",
706 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000707 }
James Chapmanfd558d12010-04-02 06:18:33 +0000708 }
709
James Chapmanf7faffa2010-04-02 06:18:49 +0000710 /* Advance past L2-specific header, if present */
711 ptr += session->l2specific_len;
712
James Chapmanfd558d12010-04-02 06:18:33 +0000713 if (L2TP_SKB_CB(skb)->has_seq) {
714 /* Received a packet with sequence numbers. If we're the LNS,
715 * check if we sre sending sequence numbers and if not,
716 * configure it so.
717 */
718 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000719 l2tp_info(session, L2TP_MSG_SEQ,
720 "%s: requested to enable seq numbers by LNS\n",
721 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000722 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000723 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000724 }
725 } else {
726 /* No sequence numbers.
727 * If user has configured mandatory sequence numbers, discard.
728 */
729 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000730 l2tp_warn(session, L2TP_MSG_SEQ,
731 "%s: recv data has no seq numbers when required. Discarding.\n",
732 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000733 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000734 goto discard;
735 }
736
737 /* If we're the LAC and we're sending sequence numbers, the
738 * LNS has requested that we no longer send sequence numbers.
739 * If we're the LNS and we're sending sequence numbers, the
740 * LAC is broken. Discard the frame.
741 */
742 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000743 l2tp_info(session, L2TP_MSG_SEQ,
744 "%s: requested to disable seq numbers by LNS\n",
745 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000746 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000747 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000748 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000749 l2tp_warn(session, L2TP_MSG_SEQ,
750 "%s: recv data has no seq numbers when required. Discarding.\n",
751 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000752 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000753 goto discard;
754 }
755 }
756
James Chapmanf7faffa2010-04-02 06:18:49 +0000757 /* Session data offset is handled differently for L2TPv2 and
758 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
759 * the header. For L2TPv3, the offset is negotiated using AVPs
760 * in the session setup control protocol.
761 */
762 if (tunnel->version == L2TP_HDR_VER_2) {
763 /* If offset bit set, skip it. */
764 if (hdrflags & L2TP_HDRFLAG_O) {
765 offset = ntohs(*(__be16 *)ptr);
766 ptr += 2 + offset;
767 }
768 } else
769 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000770
771 offset = ptr - optr;
772 if (!pskb_may_pull(skb, offset))
773 goto discard;
774
775 __skb_pull(skb, offset);
776
777 /* If caller wants to process the payload before we queue the
778 * packet, do so now.
779 */
780 if (payload_hook)
781 if ((*payload_hook)(skb))
782 goto discard;
783
784 /* Prepare skb for adding to the session's reorder_q. Hold
785 * packets for max reorder_timeout or 1 second if not
786 * reordering.
787 */
788 L2TP_SKB_CB(skb)->length = length;
789 L2TP_SKB_CB(skb)->expires = jiffies +
790 (session->reorder_timeout ? session->reorder_timeout : HZ);
791
792 /* Add packet to the session's receive queue. Reordering is done here, if
793 * enabled. Saved L2TP protocol info is stored in skb->sb[].
794 */
795 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100796 if (l2tp_recv_data_seq(session, skb))
797 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000798 } else {
799 /* No sequence numbers. Add the skb to the tail of the
800 * reorder queue. This ensures that it will be
801 * delivered after all previous sequenced skbs.
802 */
803 skb_queue_tail(&session->reorder_q, skb);
804 }
805
806 /* Try to dequeue as many skbs from reorder_q as we can. */
807 l2tp_recv_dequeue(session);
808
809 l2tp_session_dec_refcount(session);
810
James Chapmanf7faffa2010-04-02 06:18:49 +0000811 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000812
813discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000814 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000815 kfree_skb(skb);
816
817 if (session->deref)
818 (*session->deref)(session);
819
820 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000821}
822EXPORT_SYMBOL(l2tp_recv_common);
823
Tom Parkin48f72f92013-03-19 06:11:19 +0000824/* Drop skbs from the session's reorder_q
825 */
826int l2tp_session_queue_purge(struct l2tp_session *session)
827{
828 struct sk_buff *skb = NULL;
829 BUG_ON(!session);
830 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
831 while ((skb = skb_dequeue(&session->reorder_q))) {
832 atomic_long_inc(&session->stats.rx_errors);
833 kfree_skb(skb);
834 if (session->deref)
835 (*session->deref)(session);
836 }
837 return 0;
838}
839EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
840
James Chapmanf7faffa2010-04-02 06:18:49 +0000841/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
842 * here. The skb is not on a list when we get here.
843 * Returns 0 if the packet was a data packet and was successfully passed on.
844 * Returns 1 if the packet was not a good data packet and could not be
845 * forwarded. All such packets are passed up to userspace to deal with.
846 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000847static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
848 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000849{
850 struct l2tp_session *session = NULL;
851 unsigned char *ptr, *optr;
852 u16 hdrflags;
853 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000854 u16 version;
855 int length;
856
Tom Herbert58d60852014-05-07 16:52:48 -0700857 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000858
859 /* UDP always verifies the packet length. */
860 __skb_pull(skb, sizeof(struct udphdr));
861
862 /* Short packet? */
863 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000864 l2tp_info(tunnel, L2TP_MSG_DATA,
865 "%s: recv short packet (len=%d)\n",
866 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000867 goto error;
868 }
869
James Chapmanf7faffa2010-04-02 06:18:49 +0000870 /* Trace packet contents, if enabled */
871 if (tunnel->debug & L2TP_MSG_DATA) {
872 length = min(32u, skb->len);
873 if (!pskb_may_pull(skb, length))
874 goto error;
875
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000876 pr_debug("%s: recv\n", tunnel->name);
877 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000878 }
879
Eric Dumazete50e7052011-11-08 13:59:44 -0500880 /* Point to L2TP header */
881 optr = ptr = skb->data;
882
James Chapmanf7faffa2010-04-02 06:18:49 +0000883 /* Get L2TP header flags */
884 hdrflags = ntohs(*(__be16 *) ptr);
885
886 /* Check protocol version */
887 version = hdrflags & L2TP_HDR_VER_MASK;
888 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000889 l2tp_info(tunnel, L2TP_MSG_DATA,
890 "%s: recv protocol version mismatch: got %d expected %d\n",
891 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000892 goto error;
893 }
894
895 /* Get length of L2TP packet */
896 length = skb->len;
897
898 /* If type is control packet, it is handled by userspace. */
899 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000900 l2tp_dbg(tunnel, L2TP_MSG_DATA,
901 "%s: recv control packet, len=%d\n",
902 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000903 goto error;
904 }
905
906 /* Skip flags */
907 ptr += 2;
908
909 if (tunnel->version == L2TP_HDR_VER_2) {
910 /* If length is present, skip it */
911 if (hdrflags & L2TP_HDRFLAG_L)
912 ptr += 2;
913
914 /* Extract tunnel and session ID */
915 tunnel_id = ntohs(*(__be16 *) ptr);
916 ptr += 2;
917 session_id = ntohs(*(__be16 *) ptr);
918 ptr += 2;
919 } else {
920 ptr += 2; /* skip reserved bits */
921 tunnel_id = tunnel->tunnel_id;
922 session_id = ntohl(*(__be32 *) ptr);
923 ptr += 4;
924 }
925
926 /* Find the session context */
927 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000928 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000929 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000930 l2tp_info(tunnel, L2TP_MSG_DATA,
931 "%s: no session found (%u/%u). Passing up.\n",
932 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000933 goto error;
934 }
935
936 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000937
938 return 0;
939
James Chapmanfd558d12010-04-02 06:18:33 +0000940error:
941 /* Put UDP header back */
942 __skb_push(skb, sizeof(struct udphdr));
943
944 return 1;
945}
James Chapmanfd558d12010-04-02 06:18:33 +0000946
947/* UDP encapsulation receive handler. See net/ipv4/udp.c.
948 * Return codes:
949 * 0 : success.
950 * <0: error
951 * >0: skb should be passed up to userspace as UDP.
952 */
953int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
954{
955 struct l2tp_tunnel *tunnel;
956
957 tunnel = l2tp_sock_to_tunnel(sk);
958 if (tunnel == NULL)
959 goto pass_up;
960
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000961 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
962 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000963
964 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
965 goto pass_up_put;
966
967 sock_put(sk);
968 return 0;
969
970pass_up_put:
971 sock_put(sk);
972pass_up:
973 return 1;
974}
975EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
976
977/************************************************************************
978 * Transmit handling
979 ***********************************************************************/
980
981/* Build an L2TP header for the session into the buffer provided.
982 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000983static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000984{
James Chapmanf7faffa2010-04-02 06:18:49 +0000985 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000986 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000987 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000988 u16 flags = L2TP_HDR_VER_2;
989 u32 tunnel_id = tunnel->peer_tunnel_id;
990 u32 session_id = session->peer_session_id;
991
992 if (session->send_seq)
993 flags |= L2TP_HDRFLAG_S;
994
995 /* Setup L2TP header. */
996 *bufp++ = htons(flags);
997 *bufp++ = htons(tunnel_id);
998 *bufp++ = htons(session_id);
999 if (session->send_seq) {
1000 *bufp++ = htons(session->ns);
1001 *bufp++ = 0;
1002 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001003 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001004 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1005 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001006 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001007
1008 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001009}
1010
James Chapmanf7faffa2010-04-02 06:18:49 +00001011static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001012{
James Chapman0d767512010-04-02 06:19:00 +00001013 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001014 char *bufp = buf;
1015 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001016
James Chapman0d767512010-04-02 06:19:00 +00001017 /* Setup L2TP header. The header differs slightly for UDP and
1018 * IP encapsulations. For UDP, there is 4 bytes of flags.
1019 */
1020 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1021 u16 flags = L2TP_HDR_VER_3;
1022 *((__be16 *) bufp) = htons(flags);
1023 bufp += 2;
1024 *((__be16 *) bufp) = 0;
1025 bufp += 2;
1026 }
1027
James Chapmanf7faffa2010-04-02 06:18:49 +00001028 *((__be32 *) bufp) = htonl(session->peer_session_id);
1029 bufp += 4;
1030 if (session->cookie_len) {
1031 memcpy(bufp, &session->cookie[0], session->cookie_len);
1032 bufp += session->cookie_len;
1033 }
1034 if (session->l2specific_len) {
1035 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1036 u32 l2h = 0;
1037 if (session->send_seq) {
1038 l2h = 0x40000000 | session->ns;
1039 session->ns++;
1040 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001041 l2tp_dbg(session, L2TP_MSG_SEQ,
1042 "%s: updated ns to %u\n",
1043 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001044 }
1045
1046 *((__be32 *) bufp) = htonl(l2h);
1047 }
1048 bufp += session->l2specific_len;
1049 }
1050 if (session->offset)
1051 bufp += session->offset;
1052
1053 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001054}
James Chapmanfd558d12010-04-02 06:18:33 +00001055
stephen hemmingerfc130842010-10-21 07:50:46 +00001056static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001057 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001058{
1059 struct l2tp_tunnel *tunnel = session->tunnel;
1060 unsigned int len = skb->len;
1061 int error;
1062
1063 /* Debug */
1064 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001065 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1066 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001067 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001068 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1069 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001070
1071 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001072 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1073 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001074
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001075 pr_debug("%s: xmit\n", session->name);
1076 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1077 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001078 }
1079
1080 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001081 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001082#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001083 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001084 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001085 else
1086#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001087 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001088
1089 /* Update stats */
1090 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001091 atomic_long_inc(&tunnel->stats.tx_packets);
1092 atomic_long_add(len, &tunnel->stats.tx_bytes);
1093 atomic_long_inc(&session->stats.tx_packets);
1094 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001095 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001096 atomic_long_inc(&tunnel->stats.tx_errors);
1097 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001098 }
1099
1100 return 0;
1101}
James Chapmanfd558d12010-04-02 06:18:33 +00001102
James Chapmanfd558d12010-04-02 06:18:33 +00001103/* If caller requires the skb to have a ppp header, the header must be
1104 * inserted in the skb data before calling this function.
1105 */
1106int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1107{
1108 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001109 struct l2tp_tunnel *tunnel = session->tunnel;
1110 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001111 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001112 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001113 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001114 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001115 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1116 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001117 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001118
1119 /* Check that there's enough headroom in the skb to insert IP,
1120 * UDP and L2TP headers. If not enough, expand it to
1121 * make room. Adjust truesize.
1122 */
1123 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001124 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001125 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001126 kfree_skb(skb);
1127 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001128 }
James Chapmanfd558d12010-04-02 06:18:33 +00001129
James Chapmanfd558d12010-04-02 06:18:33 +00001130 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001131 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001132
James Chapman0d767512010-04-02 06:19:00 +00001133 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001134 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1135 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1136 IPSKB_REROUTED);
1137 nf_reset(skb);
1138
David S. Miller6af88da2011-05-08 13:45:20 -07001139 bh_lock_sock(sk);
1140 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001141 kfree_skb(skb);
1142 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001143 goto out_unlock;
1144 }
1145
James Chapmanfd558d12010-04-02 06:18:33 +00001146 /* Get routing info from the tunnel socket */
1147 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001148 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001149
David S. Millerd9d8da82011-05-06 22:23:20 -07001150 inet = inet_sk(sk);
1151 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001152 switch (tunnel->encap) {
1153 case L2TP_ENCAPTYPE_UDP:
1154 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001155 __skb_push(skb, sizeof(*uh));
1156 skb_reset_transport_header(skb);
1157 uh = udp_hdr(skb);
1158 uh->source = inet->inet_sport;
1159 uh->dest = inet->inet_dport;
1160 udp_len = uhlen + hdr_len + data_len;
1161 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001162
1163 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001164#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001165 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001166 udp6_set_csum(udp_get_no_check6_tx(sk),
1167 skb, &inet6_sk(sk)->saddr,
1168 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001169 else
1170#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001171 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1172 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001173 break;
1174
1175 case L2TP_ENCAPTYPE_IP:
1176 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001177 }
1178
David S. Millerd9d8da82011-05-06 22:23:20 -07001179 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001180out_unlock:
1181 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001182
Eric Dumazetb8c84302012-06-28 20:15:13 +00001183 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001184}
1185EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1186
1187/*****************************************************************************
1188 * Tinnel and session create/destroy.
1189 *****************************************************************************/
1190
1191/* Tunnel socket destruct hook.
1192 * The tunnel context is deleted only when all session sockets have been
1193 * closed.
1194 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001195static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001196{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001197 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001198 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001199
James Chapmanfd558d12010-04-02 06:18:33 +00001200 if (tunnel == NULL)
1201 goto end;
1202
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001203 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001204
James Chapmanfd558d12010-04-02 06:18:33 +00001205
Tom Parkinf8ccac02013-01-31 23:43:00 +00001206 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001207 switch (tunnel->encap) {
1208 case L2TP_ENCAPTYPE_UDP:
1209 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1210 (udp_sk(sk))->encap_type = 0;
1211 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001212 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001213 break;
1214 case L2TP_ENCAPTYPE_IP:
1215 break;
1216 }
James Chapmanfd558d12010-04-02 06:18:33 +00001217
1218 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001219 sk->sk_destruct = tunnel->old_sk_destruct;
1220 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001221 tunnel->sock = NULL;
1222
1223 /* Remove the tunnel struct from the tunnel list */
1224 pn = l2tp_pernet(tunnel->l2tp_net);
1225 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1226 list_del_rcu(&tunnel->list);
1227 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1228 atomic_dec(&l2tp_tunnel_count);
1229
1230 l2tp_tunnel_closeall(tunnel);
1231 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001232
1233 /* Call the original destructor */
1234 if (sk->sk_destruct)
1235 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001236end:
1237 return;
1238}
James Chapmanfd558d12010-04-02 06:18:33 +00001239
1240/* When the tunnel is closed, all the attached sessions need to go too.
1241 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001242void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001243{
1244 int hash;
1245 struct hlist_node *walk;
1246 struct hlist_node *tmp;
1247 struct l2tp_session *session;
1248
1249 BUG_ON(tunnel == NULL);
1250
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001251 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1252 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001253
1254 write_lock_bh(&tunnel->hlist_lock);
1255 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1256again:
1257 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1258 session = hlist_entry(walk, struct l2tp_session, hlist);
1259
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001260 l2tp_info(session, L2TP_MSG_CONTROL,
1261 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001262
1263 hlist_del_init(&session->hlist);
1264
James Chapmanfd558d12010-04-02 06:18:33 +00001265 if (session->ref != NULL)
1266 (*session->ref)(session);
1267
1268 write_unlock_bh(&tunnel->hlist_lock);
1269
Tom Parkinf6e16b22013-03-19 06:11:23 +00001270 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001271 l2tp_session_queue_purge(session);
1272
James Chapmanfd558d12010-04-02 06:18:33 +00001273 if (session->session_close != NULL)
1274 (*session->session_close)(session);
1275
1276 if (session->deref != NULL)
1277 (*session->deref)(session);
1278
Tom Parkin9980d002013-03-19 06:11:13 +00001279 l2tp_session_dec_refcount(session);
1280
James Chapmanfd558d12010-04-02 06:18:33 +00001281 write_lock_bh(&tunnel->hlist_lock);
1282
1283 /* Now restart from the beginning of this hash
1284 * chain. We always remove a session from the
1285 * list so we are guaranteed to make forward
1286 * progress.
1287 */
1288 goto again;
1289 }
1290 }
1291 write_unlock_bh(&tunnel->hlist_lock);
1292}
Tom Parkine34f4c72013-03-19 06:11:14 +00001293EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001294
Tom Parkin9980d002013-03-19 06:11:13 +00001295/* Tunnel socket destroy hook for UDP encapsulation */
1296static void l2tp_udp_encap_destroy(struct sock *sk)
1297{
1298 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1299 if (tunnel) {
1300 l2tp_tunnel_closeall(tunnel);
1301 sock_put(sk);
1302 }
1303}
1304
James Chapmanfd558d12010-04-02 06:18:33 +00001305/* Really kill the tunnel.
1306 * Come here only when all sessions have been cleared from the tunnel.
1307 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001308static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001309{
James Chapmanfd558d12010-04-02 06:18:33 +00001310 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1311 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001312 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001313 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001314}
James Chapmanfd558d12010-04-02 06:18:33 +00001315
Tom Parkinf8ccac02013-01-31 23:43:00 +00001316/* Workqueue tunnel deletion function */
1317static void l2tp_tunnel_del_work(struct work_struct *work)
1318{
1319 struct l2tp_tunnel *tunnel = NULL;
1320 struct socket *sock = NULL;
1321 struct sock *sk = NULL;
1322
1323 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1324 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);
1559
1560 /* The net we belong to */
1561 tunnel->l2tp_net = net;
1562 pn = l2tp_pernet(net);
1563
James Chapman0d767512010-04-02 06:19:00 +00001564 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001565 tunnel->debug = cfg->debug;
1566
François Cachereule18503f2013-10-02 10:16:02 +02001567#if IS_ENABLED(CONFIG_IPV6)
1568 if (sk->sk_family == PF_INET6) {
1569 struct ipv6_pinfo *np = inet6_sk(sk);
1570
1571 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001572 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001573 struct inet_sock *inet = inet_sk(sk);
1574
1575 tunnel->v4mapped = true;
1576 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001577 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1578 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001579 } else {
1580 tunnel->v4mapped = false;
1581 }
1582 }
1583#endif
1584
James Chapmanfd558d12010-04-02 06:18:33 +00001585 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001586 tunnel->encap = encap;
1587 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001588 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001589
Andy Zhouc8fffce2014-09-16 17:31:19 -07001590 udp_cfg.sk_user_data = tunnel;
1591 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1592 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1593 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1594
1595 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1596 } else {
1597 sk->sk_user_data = tunnel;
1598 }
James Chapmanfd558d12010-04-02 06:18:33 +00001599
1600 /* Hook on the tunnel socket destructor so that we can cleanup
1601 * if the tunnel socket goes away.
1602 */
1603 tunnel->old_sk_destruct = sk->sk_destruct;
1604 sk->sk_destruct = &l2tp_tunnel_destruct;
1605 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001606 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001607 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1608
James Chapmanfd558d12010-04-02 06:18:33 +00001609 sk->sk_allocation = GFP_ATOMIC;
1610
Tom Parkinf8ccac02013-01-31 23:43:00 +00001611 /* Init delete workqueue struct */
1612 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1613
James Chapmanfd558d12010-04-02 06:18:33 +00001614 /* Add tunnel to our list */
1615 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001616 atomic_inc(&l2tp_tunnel_count);
1617
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 */
1621 l2tp_tunnel_inc_refcount(tunnel);
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 */
1643int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1644{
Alexander Couzens06a15f52015-09-28 11:32:42 +02001645 l2tp_tunnel_inc_refcount(tunnel);
Tom Parkin2b551c62013-03-19 06:11:16 +00001646 l2tp_tunnel_closeall(tunnel);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001647 if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
1648 l2tp_tunnel_dec_refcount(tunnel);
1649 return 1;
1650 }
1651 return 0;
James Chapman309795f2010-04-02 06:19:10 +00001652}
1653EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1654
James Chapmanfd558d12010-04-02 06:18:33 +00001655/* Really kill the session.
1656 */
1657void l2tp_session_free(struct l2tp_session *session)
1658{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001659 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001660
1661 BUG_ON(atomic_read(&session->ref_count) != 0);
1662
Tom Parkinf6e16b22013-03-19 06:11:23 +00001663 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001664 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001665 if (session->session_id != 0)
1666 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001667 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001668 session->tunnel = NULL;
1669 l2tp_tunnel_dec_refcount(tunnel);
1670 }
1671
1672 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001673}
1674EXPORT_SYMBOL_GPL(l2tp_session_free);
1675
Tom Parkinf6e16b22013-03-19 06:11:23 +00001676/* Remove an l2tp session from l2tp_core's hash lists.
1677 * Provides a tidyup interface for pseudowire code which can't just route all
1678 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1679 * callback.
1680 */
1681void __l2tp_session_unhash(struct l2tp_session *session)
1682{
1683 struct l2tp_tunnel *tunnel = session->tunnel;
1684
1685 /* Remove the session from core hashes */
1686 if (tunnel) {
1687 /* Remove from the per-tunnel hash */
1688 write_lock_bh(&tunnel->hlist_lock);
1689 hlist_del_init(&session->hlist);
1690 write_unlock_bh(&tunnel->hlist_lock);
1691
1692 /* For L2TPv3 we have a per-net hash: remove from there, too */
1693 if (tunnel->version != L2TP_HDR_VER_2) {
1694 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1695 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1696 hlist_del_init_rcu(&session->global_hlist);
1697 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1698 synchronize_rcu();
1699 }
1700 }
1701}
1702EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1703
James Chapman309795f2010-04-02 06:19:10 +00001704/* This function is used by the netlink SESSION_DELETE command and by
1705 pseudowire modules.
1706 */
1707int l2tp_session_delete(struct l2tp_session *session)
1708{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001709 if (session->ref)
1710 (*session->ref)(session);
1711 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001712 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001713 if (session->session_close != NULL)
1714 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001715 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001716 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001717 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001718 return 0;
1719}
1720EXPORT_SYMBOL_GPL(l2tp_session_delete);
1721
James Chapmanf7faffa2010-04-02 06:18:49 +00001722/* We come here whenever a session's send_seq, cookie_len or
1723 * l2specific_len parameters are set.
1724 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001725void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001726{
1727 if (version == L2TP_HDR_VER_2) {
1728 session->hdr_len = 6;
1729 if (session->send_seq)
1730 session->hdr_len += 4;
1731 } else {
James Chapman0d767512010-04-02 06:19:00 +00001732 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1733 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1734 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001735 }
1736
1737}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001738EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001739
James Chapmanfd558d12010-04-02 06:18:33 +00001740struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1741{
1742 struct l2tp_session *session;
1743
1744 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1745 if (session != NULL) {
1746 session->magic = L2TP_SESSION_MAGIC;
1747 session->tunnel = tunnel;
1748
1749 session->session_id = session_id;
1750 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001751 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001752 if (tunnel->version == L2TP_HDR_VER_2)
1753 session->nr_max = 0xffff;
1754 else
1755 session->nr_max = 0xffffff;
1756 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001757 session->nr_oos_count_max = 4;
1758
1759 /* Use NR of first received packet */
1760 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001761
1762 sprintf(&session->name[0], "sess %u/%u",
1763 tunnel->tunnel_id, session->session_id);
1764
1765 skb_queue_head_init(&session->reorder_q);
1766
1767 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001768 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001769
1770 /* Inherit debug options from tunnel */
1771 session->debug = tunnel->debug;
1772
1773 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001774 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001775 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001776 session->mtu = cfg->mtu;
1777 session->mru = cfg->mru;
1778 session->send_seq = cfg->send_seq;
1779 session->recv_seq = cfg->recv_seq;
1780 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001781 session->reorder_timeout = cfg->reorder_timeout;
1782 session->offset = cfg->offset;
1783 session->l2specific_type = cfg->l2specific_type;
1784 session->l2specific_len = cfg->l2specific_len;
1785 session->cookie_len = cfg->cookie_len;
1786 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1787 session->peer_cookie_len = cfg->peer_cookie_len;
1788 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001789 }
1790
James Chapmanf7faffa2010-04-02 06:18:49 +00001791 if (tunnel->version == L2TP_HDR_VER_2)
1792 session->build_header = l2tp_build_l2tpv2_header;
1793 else
1794 session->build_header = l2tp_build_l2tpv3_header;
1795
1796 l2tp_session_set_header_len(session, tunnel->version);
1797
James Chapmanfd558d12010-04-02 06:18:33 +00001798 /* Bump the reference count. The session context is deleted
1799 * only when this drops to zero.
1800 */
1801 l2tp_session_inc_refcount(session);
1802 l2tp_tunnel_inc_refcount(tunnel);
1803
1804 /* Ensure tunnel socket isn't deleted */
1805 sock_hold(tunnel->sock);
1806
1807 /* Add session to the tunnel's hash list */
1808 write_lock_bh(&tunnel->hlist_lock);
1809 hlist_add_head(&session->hlist,
1810 l2tp_session_id_hash(tunnel, session_id));
1811 write_unlock_bh(&tunnel->hlist_lock);
1812
James Chapmanf7faffa2010-04-02 06:18:49 +00001813 /* And to the global session list if L2TPv3 */
1814 if (tunnel->version != L2TP_HDR_VER_2) {
1815 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1816
James Chapmane02d4942010-04-02 06:19:16 +00001817 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1818 hlist_add_head_rcu(&session->global_hlist,
1819 l2tp_session_id_hash_2(pn, session_id));
1820 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001821 }
1822
James Chapmanfd558d12010-04-02 06:18:33 +00001823 /* Ignore management session in session count value */
1824 if (session->session_id != 0)
1825 atomic_inc(&l2tp_session_count);
1826 }
1827
1828 return session;
1829}
1830EXPORT_SYMBOL_GPL(l2tp_session_create);
1831
1832/*****************************************************************************
1833 * Init and cleanup
1834 *****************************************************************************/
1835
1836static __net_init int l2tp_init_net(struct net *net)
1837{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001838 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001839 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001840
James Chapmanfd558d12010-04-02 06:18:33 +00001841 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001842 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001843
James Chapmanf7faffa2010-04-02 06:18:49 +00001844 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1845 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1846
James Chapmane02d4942010-04-02 06:19:16 +00001847 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001848
James Chapmanfd558d12010-04-02 06:18:33 +00001849 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001850}
1851
Tom Parkin167eb172013-01-31 23:43:03 +00001852static __net_exit void l2tp_exit_net(struct net *net)
1853{
1854 struct l2tp_net *pn = l2tp_pernet(net);
1855 struct l2tp_tunnel *tunnel = NULL;
1856
1857 rcu_read_lock_bh();
1858 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1859 (void)l2tp_tunnel_delete(tunnel);
1860 }
1861 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001862
1863 flush_workqueue(l2tp_wq);
1864 rcu_barrier();
Tom Parkin167eb172013-01-31 23:43:03 +00001865}
1866
James Chapmanfd558d12010-04-02 06:18:33 +00001867static struct pernet_operations l2tp_net_ops = {
1868 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001869 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001870 .id = &l2tp_net_id,
1871 .size = sizeof(struct l2tp_net),
1872};
1873
1874static int __init l2tp_init(void)
1875{
1876 int rc = 0;
1877
1878 rc = register_pernet_device(&l2tp_net_ops);
1879 if (rc)
1880 goto out;
1881
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001882 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001883 if (!l2tp_wq) {
1884 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001885 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001886 rc = -ENOMEM;
1887 goto out;
1888 }
1889
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001890 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001891
1892out:
1893 return rc;
1894}
1895
1896static void __exit l2tp_exit(void)
1897{
1898 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001899 if (l2tp_wq) {
1900 destroy_workqueue(l2tp_wq);
1901 l2tp_wq = NULL;
1902 }
James Chapmanfd558d12010-04-02 06:18:33 +00001903}
1904
1905module_init(l2tp_init);
1906module_exit(l2tp_exit);
1907
1908MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1909MODULE_DESCRIPTION("L2TP core");
1910MODULE_LICENSE("GPL");
1911MODULE_VERSION(L2TP_DRV_VERSION);
1912