blob: 69c316dd02dc790f5d18813e4106a50fa496c27e [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>
James Chapman309795f2010-04-02 06:19:10 +000055#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000056#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000057#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000058#include <net/inet6_connection_sock.h>
59#include <net/inet_ecn.h>
60#include <net/ip6_route.h>
David S. Millerd499bd22012-04-30 13:21:28 -040061#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000062
63#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000065
66#include "l2tp_core.h"
67
68#define L2TP_DRV_VERSION "V2.0"
69
70/* L2TP header constants */
71#define L2TP_HDRFLAG_T 0x8000
72#define L2TP_HDRFLAG_L 0x4000
73#define L2TP_HDRFLAG_S 0x0800
74#define L2TP_HDRFLAG_O 0x0200
75#define L2TP_HDRFLAG_P 0x0100
76
77#define L2TP_HDR_VER_MASK 0x000F
78#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000079#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000080
81/* L2TPv3 default L2-specific sublayer */
82#define L2TP_SLFLAG_S 0x40000000
83#define L2TP_SL_SEQ_MASK 0x00ffffff
84
85#define L2TP_HDR_SIZE_SEQ 10
86#define L2TP_HDR_SIZE_NOSEQ 6
87
88/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0
90
James Chapmanfd558d12010-04-02 06:18:33 +000091/* Private data stored for received packets in the skb.
92 */
93struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000094 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000095 u16 has_seq;
96 u16 length;
97 unsigned long expires;
98};
99
100#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101
102static atomic_t l2tp_tunnel_count;
103static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000104static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000105
106/* per-net private data for this module */
107static unsigned int l2tp_net_id;
108struct l2tp_net {
109 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000110 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000111 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000112 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000113};
114
stephen hemmingerfc130842010-10-21 07:50:46 +0000115static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
116static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000117
James Chapmanfd558d12010-04-02 06:18:33 +0000118static inline struct l2tp_net *l2tp_pernet(struct net *net)
119{
120 BUG_ON(!net);
121
122 return net_generic(net, l2tp_net_id);
123}
124
stephen hemmingerfc130842010-10-21 07:50:46 +0000125/* Tunnel reference counts. Incremented per session that is added to
126 * the tunnel.
127 */
128static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
129{
130 atomic_inc(&tunnel->ref_count);
131}
132
133static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 if (atomic_dec_and_test(&tunnel->ref_count))
136 l2tp_tunnel_free(tunnel);
137}
138#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000139#define l2tp_tunnel_inc_refcount(_t) \
140do { \
141 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
142 __func__, __LINE__, (_t)->name, \
143 atomic_read(&_t->ref_count)); \
144 l2tp_tunnel_inc_refcount_1(_t); \
145} while (0)
146#define l2tp_tunnel_dec_refcount(_t)
147do { \
148 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
149 __func__, __LINE__, (_t)->name, \
150 atomic_read(&_t->ref_count)); \
151 l2tp_tunnel_dec_refcount_1(_t); \
152} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000153#else
154#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
155#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
156#endif
157
James Chapmanf7faffa2010-04-02 06:18:49 +0000158/* Session hash global list for L2TPv3.
159 * The session_id SHOULD be random according to RFC3931, but several
160 * L2TP implementations use incrementing session_ids. So we do a real
161 * hash on the session_id, rather than a simple bitmask.
162 */
163static inline struct hlist_head *
164l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
165{
166 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
167
168}
169
Tom Parkin80d84ef2013-01-22 05:13:48 +0000170/* Lookup the tunnel socket, possibly involving the fs code if the socket is
171 * owned by userspace. A struct sock returned from this function must be
172 * released using l2tp_tunnel_sock_put once you're done with it.
173 */
174struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
175{
176 int err = 0;
177 struct socket *sock = NULL;
178 struct sock *sk = NULL;
179
180 if (!tunnel)
181 goto out;
182
183 if (tunnel->fd >= 0) {
184 /* Socket is owned by userspace, who might be in the process
185 * of closing it. Look the socket up using the fd to ensure
186 * consistency.
187 */
188 sock = sockfd_lookup(tunnel->fd, &err);
189 if (sock)
190 sk = sock->sk;
191 } else {
192 /* Socket is owned by kernelspace */
193 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000194 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000195 }
196
197out:
198 return sk;
199}
200EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
201
202/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
203void l2tp_tunnel_sock_put(struct sock *sk)
204{
205 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
206 if (tunnel) {
207 if (tunnel->fd >= 0) {
208 /* Socket is owned by userspace */
209 sockfd_put(sk->sk_socket);
210 }
211 sock_put(sk);
212 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000213 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000214}
215EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
216
James Chapmanf7faffa2010-04-02 06:18:49 +0000217/* Lookup a session by id in the global session list
218 */
219static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
220{
221 struct l2tp_net *pn = l2tp_pernet(net);
222 struct hlist_head *session_list =
223 l2tp_session_id_hash_2(pn, session_id);
224 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000225
James Chapmane02d4942010-04-02 06:19:16 +0000226 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800227 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000228 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000229 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000230 return session;
231 }
232 }
James Chapmane02d4942010-04-02 06:19:16 +0000233 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000234
235 return NULL;
236}
237
James Chapmanfd558d12010-04-02 06:18:33 +0000238/* Session hash list.
239 * The session_id SHOULD be random according to RFC2661, but several
240 * L2TP implementations (Cisco and Microsoft) use incrementing
241 * session_ids. So we do a real hash on the session_id, rather than a
242 * simple bitmask.
243 */
244static inline struct hlist_head *
245l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
246{
247 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
248}
249
250/* Lookup a session by id
251 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000252struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000253{
James Chapmanf7faffa2010-04-02 06:18:49 +0000254 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000255 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000256
James Chapmanf7faffa2010-04-02 06:18:49 +0000257 /* In L2TPv3, session_ids are unique over all tunnels and we
258 * sometimes need to look them up before we know the
259 * tunnel.
260 */
261 if (tunnel == NULL)
262 return l2tp_session_find_2(net, session_id);
263
264 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000265 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800266 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000267 if (session->session_id == session_id) {
268 read_unlock_bh(&tunnel->hlist_lock);
269 return session;
270 }
271 }
272 read_unlock_bh(&tunnel->hlist_lock);
273
274 return NULL;
275}
276EXPORT_SYMBOL_GPL(l2tp_session_find);
277
278struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
279{
280 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000281 struct l2tp_session *session;
282 int count = 0;
283
284 read_lock_bh(&tunnel->hlist_lock);
285 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800286 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000287 if (++count > nth) {
288 read_unlock_bh(&tunnel->hlist_lock);
289 return session;
290 }
291 }
292 }
293
294 read_unlock_bh(&tunnel->hlist_lock);
295
296 return NULL;
297}
298EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
299
James Chapman309795f2010-04-02 06:19:10 +0000300/* Lookup a session by interface name.
301 * This is very inefficient but is only used by management interfaces.
302 */
303struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
304{
305 struct l2tp_net *pn = l2tp_pernet(net);
306 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000307 struct l2tp_session *session;
308
James Chapmane02d4942010-04-02 06:19:16 +0000309 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000310 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800311 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000312 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000313 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000314 return session;
315 }
316 }
317 }
318
James Chapmane02d4942010-04-02 06:19:16 +0000319 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000320
321 return NULL;
322}
323EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
324
James Chapmanfd558d12010-04-02 06:18:33 +0000325/* Lookup a tunnel by id
326 */
327struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
328{
329 struct l2tp_tunnel *tunnel;
330 struct l2tp_net *pn = l2tp_pernet(net);
331
James Chapmane02d4942010-04-02 06:19:16 +0000332 rcu_read_lock_bh();
333 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000334 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000335 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000336 return tunnel;
337 }
338 }
James Chapmane02d4942010-04-02 06:19:16 +0000339 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000340
341 return NULL;
342}
343EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
344
345struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
346{
347 struct l2tp_net *pn = l2tp_pernet(net);
348 struct l2tp_tunnel *tunnel;
349 int count = 0;
350
James Chapmane02d4942010-04-02 06:19:16 +0000351 rcu_read_lock_bh();
352 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000353 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000354 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000355 return tunnel;
356 }
357 }
358
James Chapmane02d4942010-04-02 06:19:16 +0000359 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000360
361 return NULL;
362}
363EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
364
365/*****************************************************************************
366 * Receive data handling
367 *****************************************************************************/
368
369/* Queue a skb in order. We come here only if the skb has an L2TP sequence
370 * number.
371 */
372static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
373{
374 struct sk_buff *skbp;
375 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000376 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapman5de7aee2012-04-29 21:48:46 +0000377 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000378
379 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000380 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000381 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
382 if (L2TP_SKB_CB(skbp)->ns > ns) {
383 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000384 l2tp_dbg(session, L2TP_MSG_SEQ,
385 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
386 session->name, ns, L2TP_SKB_CB(skbp)->ns,
387 skb_queue_len(&session->reorder_q));
James Chapman5de7aee2012-04-29 21:48:46 +0000388 u64_stats_update_begin(&sstats->syncp);
389 sstats->rx_oos_packets++;
390 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000391 goto out;
392 }
393 }
394
395 __skb_queue_tail(&session->reorder_q, skb);
396
397out:
398 spin_unlock_bh(&session->reorder_q.lock);
399}
400
401/* Dequeue a single skb.
402 */
403static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
404{
405 struct l2tp_tunnel *tunnel = session->tunnel;
406 int length = L2TP_SKB_CB(skb)->length;
James Chapman5de7aee2012-04-29 21:48:46 +0000407 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000408
409 /* We're about to requeue the skb, so return resources
410 * to its current owner (a socket receive buffer).
411 */
412 skb_orphan(skb);
413
James Chapman5de7aee2012-04-29 21:48:46 +0000414 tstats = &tunnel->stats;
415 u64_stats_update_begin(&tstats->syncp);
416 sstats = &session->stats;
417 u64_stats_update_begin(&sstats->syncp);
418 tstats->rx_packets++;
419 tstats->rx_bytes += length;
420 sstats->rx_packets++;
421 sstats->rx_bytes += length;
422 u64_stats_update_end(&tstats->syncp);
423 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000424
425 if (L2TP_SKB_CB(skb)->has_seq) {
426 /* Bump our Nr */
427 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000428 if (tunnel->version == L2TP_HDR_VER_2)
429 session->nr &= 0xffff;
430 else
431 session->nr &= 0xffffff;
432
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000433 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
434 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000435 }
436
437 /* call private receive handler */
438 if (session->recv_skb != NULL)
439 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
440 else
441 kfree_skb(skb);
442
443 if (session->deref)
444 (*session->deref)(session);
445}
446
447/* Dequeue skbs from the session's reorder_q, subject to packet order.
448 * Skbs that have been in the queue for too long are simply discarded.
449 */
450static void l2tp_recv_dequeue(struct l2tp_session *session)
451{
452 struct sk_buff *skb;
453 struct sk_buff *tmp;
James Chapman5de7aee2012-04-29 21:48:46 +0000454 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000455
456 /* If the pkt at the head of the queue has the nr that we
457 * expect to send up next, dequeue it and any other
458 * in-sequence packets behind it.
459 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000460start:
James Chapmanfd558d12010-04-02 06:18:33 +0000461 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000462 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000463 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
464 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
James Chapman5de7aee2012-04-29 21:48:46 +0000465 u64_stats_update_begin(&sstats->syncp);
466 sstats->rx_seq_discards++;
467 sstats->rx_errors++;
468 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000469 l2tp_dbg(session, L2TP_MSG_SEQ,
470 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
471 session->name, L2TP_SKB_CB(skb)->ns,
472 L2TP_SKB_CB(skb)->length, session->nr,
473 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000474 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000475 __skb_unlink(skb, &session->reorder_q);
476 kfree_skb(skb);
477 if (session->deref)
478 (*session->deref)(session);
479 continue;
480 }
481
482 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000483 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000484 l2tp_dbg(session, L2TP_MSG_SEQ,
485 "%s: advancing nr to next pkt: %u -> %u",
486 session->name, session->nr,
487 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000488 session->reorder_skip = 0;
489 session->nr = L2TP_SKB_CB(skb)->ns;
490 }
James Chapmanfd558d12010-04-02 06:18:33 +0000491 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000492 l2tp_dbg(session, L2TP_MSG_SEQ,
493 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
494 session->name, L2TP_SKB_CB(skb)->ns,
495 L2TP_SKB_CB(skb)->length, session->nr,
496 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000497 goto out;
498 }
499 }
500 __skb_unlink(skb, &session->reorder_q);
501
502 /* Process the skb. We release the queue lock while we
503 * do so to let other contexts process the queue.
504 */
505 spin_unlock_bh(&session->reorder_q.lock);
506 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000507 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000508 }
509
510out:
511 spin_unlock_bh(&session->reorder_q.lock);
512}
513
514static inline int l2tp_verify_udp_checksum(struct sock *sk,
515 struct sk_buff *skb)
516{
517 struct udphdr *uh = udp_hdr(skb);
518 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000519 __wsum psum;
520
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000521 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000522 return 0;
523
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000524#if IS_ENABLED(CONFIG_IPV6)
525 if (sk->sk_family == PF_INET6) {
526 if (!uh->check) {
527 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
528 return 1;
529 }
530 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
531 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
532 &ipv6_hdr(skb)->daddr, ulen,
533 IPPROTO_UDP, skb->csum)) {
534 skb->ip_summed = CHECKSUM_UNNECESSARY;
535 return 0;
536 }
537 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
538 &ipv6_hdr(skb)->daddr,
539 skb->len, IPPROTO_UDP,
540 0));
541 } else
542#endif
543 {
544 struct inet_sock *inet;
545 if (!uh->check)
546 return 0;
547 inet = inet_sk(sk);
548 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
549 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000550
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000551 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
552 !csum_fold(csum_add(psum, skb->csum)))
553 return 0;
554 skb->csum = psum;
555 }
James Chapmanfd558d12010-04-02 06:18:33 +0000556
557 return __skb_checksum_complete(skb);
558}
559
James Chapmanf7faffa2010-04-02 06:18:49 +0000560/* Do receive processing of L2TP data frames. We handle both L2TPv2
561 * and L2TPv3 data frames here.
562 *
563 * L2TPv2 Data Message Header
564 *
565 * 0 1 2 3
566 * 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
567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
568 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570 * | Tunnel ID | Session ID |
571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
572 * | Ns (opt) | Nr (opt) |
573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
574 * | Offset Size (opt) | Offset pad... (opt)
575 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
576 *
577 * Data frames are marked by T=0. All other fields are the same as
578 * those in L2TP control frames.
579 *
580 * L2TPv3 Data Message Header
581 *
582 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
583 * | L2TP Session Header |
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 * | L2-Specific Sublayer |
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | Tunnel Payload ...
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 *
590 * L2TPv3 Session Header Over IP
591 *
592 * 0 1 2 3
593 * 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
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * | Session ID |
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | Cookie (optional, maximum 64 bits)...
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * |
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 *
602 * L2TPv3 L2-Specific Sublayer Format
603 *
604 * 0 1 2 3
605 * 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
606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607 * |x|S|x|x|x|x|x|x| Sequence Number |
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 *
610 * Cookie value, sublayer format and offset (pad) are negotiated with
611 * the peer when the session is set up. Unlike L2TPv2, we do not need
612 * to parse the packet header to determine if optional fields are
613 * present.
614 *
615 * Caller must already have parsed the frame and determined that it is
616 * a data (not control) frame before coming here. Fields up to the
617 * session-id have already been parsed and ptr points to the data
618 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000619 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000620void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
621 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
622 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000623{
James Chapmanf7faffa2010-04-02 06:18:49 +0000624 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000625 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000626 u32 ns, nr;
James Chapman5de7aee2012-04-29 21:48:46 +0000627 struct l2tp_stats *sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000628
629 /* The ref count is increased since we now hold a pointer to
630 * the session. Take care to decrement the refcnt when exiting
631 * this function from now on...
632 */
633 l2tp_session_inc_refcount(session);
634 if (session->ref)
635 (*session->ref)(session);
636
James Chapmanf7faffa2010-04-02 06:18:49 +0000637 /* Parse and check optional cookie */
638 if (session->peer_cookie_len > 0) {
639 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000640 l2tp_info(tunnel, L2TP_MSG_DATA,
641 "%s: cookie mismatch (%u/%u). Discarding.\n",
642 tunnel->name, tunnel->tunnel_id,
643 session->session_id);
James Chapman5de7aee2012-04-29 21:48:46 +0000644 u64_stats_update_begin(&sstats->syncp);
645 sstats->rx_cookie_discards++;
646 u64_stats_update_end(&sstats->syncp);
James Chapmanf7faffa2010-04-02 06:18:49 +0000647 goto discard;
648 }
649 ptr += session->peer_cookie_len;
650 }
651
James Chapmanfd558d12010-04-02 06:18:33 +0000652 /* Handle the optional sequence numbers. Sequence numbers are
653 * in different places for L2TPv2 and L2TPv3.
654 *
655 * If we are the LAC, enable/disable sequence numbers under
656 * the control of the LNS. If no sequence numbers present but
657 * we were expecting them, discard frame.
658 */
659 ns = nr = 0;
660 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000661 if (tunnel->version == L2TP_HDR_VER_2) {
662 if (hdrflags & L2TP_HDRFLAG_S) {
663 ns = ntohs(*(__be16 *) ptr);
664 ptr += 2;
665 nr = ntohs(*(__be16 *) ptr);
666 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000667
James Chapmanf7faffa2010-04-02 06:18:49 +0000668 /* Store L2TP info in the skb */
669 L2TP_SKB_CB(skb)->ns = ns;
670 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000671
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000672 l2tp_dbg(session, L2TP_MSG_SEQ,
673 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
674 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000675 }
676 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
677 u32 l2h = ntohl(*(__be32 *) ptr);
678
679 if (l2h & 0x40000000) {
680 ns = l2h & 0x00ffffff;
681
682 /* Store L2TP info in the skb */
683 L2TP_SKB_CB(skb)->ns = ns;
684 L2TP_SKB_CB(skb)->has_seq = 1;
685
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000686 l2tp_dbg(session, L2TP_MSG_SEQ,
687 "%s: recv data ns=%u, session nr=%u\n",
688 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000689 }
James Chapmanfd558d12010-04-02 06:18:33 +0000690 }
691
James Chapmanf7faffa2010-04-02 06:18:49 +0000692 /* Advance past L2-specific header, if present */
693 ptr += session->l2specific_len;
694
James Chapmanfd558d12010-04-02 06:18:33 +0000695 if (L2TP_SKB_CB(skb)->has_seq) {
696 /* Received a packet with sequence numbers. If we're the LNS,
697 * check if we sre sending sequence numbers and if not,
698 * configure it so.
699 */
700 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000701 l2tp_info(session, L2TP_MSG_SEQ,
702 "%s: requested to enable seq numbers by LNS\n",
703 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000704 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000705 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000706 }
707 } else {
708 /* No sequence numbers.
709 * If user has configured mandatory sequence numbers, discard.
710 */
711 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000712 l2tp_warn(session, L2TP_MSG_SEQ,
713 "%s: recv data has no seq numbers when required. Discarding.\n",
714 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000715 u64_stats_update_begin(&sstats->syncp);
716 sstats->rx_seq_discards++;
717 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000718 goto discard;
719 }
720
721 /* If we're the LAC and we're sending sequence numbers, the
722 * LNS has requested that we no longer send sequence numbers.
723 * If we're the LNS and we're sending sequence numbers, the
724 * LAC is broken. Discard the frame.
725 */
726 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000727 l2tp_info(session, L2TP_MSG_SEQ,
728 "%s: requested to disable seq numbers by LNS\n",
729 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000730 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000731 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000732 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000733 l2tp_warn(session, L2TP_MSG_SEQ,
734 "%s: recv data has no seq numbers when required. Discarding.\n",
735 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000736 u64_stats_update_begin(&sstats->syncp);
737 sstats->rx_seq_discards++;
738 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000739 goto discard;
740 }
741 }
742
James Chapmanf7faffa2010-04-02 06:18:49 +0000743 /* Session data offset is handled differently for L2TPv2 and
744 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
745 * the header. For L2TPv3, the offset is negotiated using AVPs
746 * in the session setup control protocol.
747 */
748 if (tunnel->version == L2TP_HDR_VER_2) {
749 /* If offset bit set, skip it. */
750 if (hdrflags & L2TP_HDRFLAG_O) {
751 offset = ntohs(*(__be16 *)ptr);
752 ptr += 2 + offset;
753 }
754 } else
755 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000756
757 offset = ptr - optr;
758 if (!pskb_may_pull(skb, offset))
759 goto discard;
760
761 __skb_pull(skb, offset);
762
763 /* If caller wants to process the payload before we queue the
764 * packet, do so now.
765 */
766 if (payload_hook)
767 if ((*payload_hook)(skb))
768 goto discard;
769
770 /* Prepare skb for adding to the session's reorder_q. Hold
771 * packets for max reorder_timeout or 1 second if not
772 * reordering.
773 */
774 L2TP_SKB_CB(skb)->length = length;
775 L2TP_SKB_CB(skb)->expires = jiffies +
776 (session->reorder_timeout ? session->reorder_timeout : HZ);
777
778 /* Add packet to the session's receive queue. Reordering is done here, if
779 * enabled. Saved L2TP protocol info is stored in skb->sb[].
780 */
781 if (L2TP_SKB_CB(skb)->has_seq) {
782 if (session->reorder_timeout != 0) {
783 /* Packet reordering enabled. Add skb to session's
784 * reorder queue, in order of ns.
785 */
786 l2tp_recv_queue_skb(session, skb);
787 } else {
788 /* Packet reordering disabled. Discard out-of-sequence
789 * packets
790 */
791 if (L2TP_SKB_CB(skb)->ns != session->nr) {
James Chapman5de7aee2012-04-29 21:48:46 +0000792 u64_stats_update_begin(&sstats->syncp);
793 sstats->rx_seq_discards++;
794 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000795 l2tp_dbg(session, L2TP_MSG_SEQ,
796 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
797 session->name, L2TP_SKB_CB(skb)->ns,
798 L2TP_SKB_CB(skb)->length, session->nr,
799 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000800 goto discard;
801 }
802 skb_queue_tail(&session->reorder_q, skb);
803 }
804 } else {
805 /* No sequence numbers. Add the skb to the tail of the
806 * reorder queue. This ensures that it will be
807 * delivered after all previous sequenced skbs.
808 */
809 skb_queue_tail(&session->reorder_q, skb);
810 }
811
812 /* Try to dequeue as many skbs from reorder_q as we can. */
813 l2tp_recv_dequeue(session);
814
815 l2tp_session_dec_refcount(session);
816
James Chapmanf7faffa2010-04-02 06:18:49 +0000817 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000818
819discard:
James Chapman5de7aee2012-04-29 21:48:46 +0000820 u64_stats_update_begin(&sstats->syncp);
821 sstats->rx_errors++;
822 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000823 kfree_skb(skb);
824
825 if (session->deref)
826 (*session->deref)(session);
827
828 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000829}
830EXPORT_SYMBOL(l2tp_recv_common);
831
Tom Parkin48f72f92013-03-19 06:11:19 +0000832/* Drop skbs from the session's reorder_q
833 */
834int l2tp_session_queue_purge(struct l2tp_session *session)
835{
836 struct sk_buff *skb = NULL;
837 BUG_ON(!session);
838 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
839 while ((skb = skb_dequeue(&session->reorder_q))) {
840 atomic_long_inc(&session->stats.rx_errors);
841 kfree_skb(skb);
842 if (session->deref)
843 (*session->deref)(session);
844 }
845 return 0;
846}
847EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
848
James Chapmanf7faffa2010-04-02 06:18:49 +0000849/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
850 * here. The skb is not on a list when we get here.
851 * Returns 0 if the packet was a data packet and was successfully passed on.
852 * Returns 1 if the packet was not a good data packet and could not be
853 * forwarded. All such packets are passed up to userspace to deal with.
854 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000855static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
856 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000857{
858 struct l2tp_session *session = NULL;
859 unsigned char *ptr, *optr;
860 u16 hdrflags;
861 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000862 u16 version;
863 int length;
James Chapman5de7aee2012-04-29 21:48:46 +0000864 struct l2tp_stats *tstats;
James Chapmanf7faffa2010-04-02 06:18:49 +0000865
866 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
867 goto discard_bad_csum;
868
869 /* UDP always verifies the packet length. */
870 __skb_pull(skb, sizeof(struct udphdr));
871
872 /* Short packet? */
873 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000874 l2tp_info(tunnel, L2TP_MSG_DATA,
875 "%s: recv short packet (len=%d)\n",
876 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000877 goto error;
878 }
879
James Chapmanf7faffa2010-04-02 06:18:49 +0000880 /* Trace packet contents, if enabled */
881 if (tunnel->debug & L2TP_MSG_DATA) {
882 length = min(32u, skb->len);
883 if (!pskb_may_pull(skb, length))
884 goto error;
885
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000886 pr_debug("%s: recv\n", tunnel->name);
887 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000888 }
889
Eric Dumazete50e7052011-11-08 13:59:44 -0500890 /* Point to L2TP header */
891 optr = ptr = skb->data;
892
James Chapmanf7faffa2010-04-02 06:18:49 +0000893 /* Get L2TP header flags */
894 hdrflags = ntohs(*(__be16 *) ptr);
895
896 /* Check protocol version */
897 version = hdrflags & L2TP_HDR_VER_MASK;
898 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000899 l2tp_info(tunnel, L2TP_MSG_DATA,
900 "%s: recv protocol version mismatch: got %d expected %d\n",
901 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000902 goto error;
903 }
904
905 /* Get length of L2TP packet */
906 length = skb->len;
907
908 /* If type is control packet, it is handled by userspace. */
909 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000910 l2tp_dbg(tunnel, L2TP_MSG_DATA,
911 "%s: recv control packet, len=%d\n",
912 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000913 goto error;
914 }
915
916 /* Skip flags */
917 ptr += 2;
918
919 if (tunnel->version == L2TP_HDR_VER_2) {
920 /* If length is present, skip it */
921 if (hdrflags & L2TP_HDRFLAG_L)
922 ptr += 2;
923
924 /* Extract tunnel and session ID */
925 tunnel_id = ntohs(*(__be16 *) ptr);
926 ptr += 2;
927 session_id = ntohs(*(__be16 *) ptr);
928 ptr += 2;
929 } else {
930 ptr += 2; /* skip reserved bits */
931 tunnel_id = tunnel->tunnel_id;
932 session_id = ntohl(*(__be32 *) ptr);
933 ptr += 4;
934 }
935
936 /* Find the session context */
937 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000938 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000939 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000940 l2tp_info(tunnel, L2TP_MSG_DATA,
941 "%s: no session found (%u/%u). Passing up.\n",
942 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000943 goto error;
944 }
945
946 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000947
948 return 0;
949
950discard_bad_csum:
951 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
952 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
James Chapman5de7aee2012-04-29 21:48:46 +0000953 tstats = &tunnel->stats;
954 u64_stats_update_begin(&tstats->syncp);
955 tstats->rx_errors++;
956 u64_stats_update_end(&tstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000957 kfree_skb(skb);
958
959 return 0;
960
961error:
962 /* Put UDP header back */
963 __skb_push(skb, sizeof(struct udphdr));
964
965 return 1;
966}
James Chapmanfd558d12010-04-02 06:18:33 +0000967
968/* UDP encapsulation receive handler. See net/ipv4/udp.c.
969 * Return codes:
970 * 0 : success.
971 * <0: error
972 * >0: skb should be passed up to userspace as UDP.
973 */
974int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
975{
976 struct l2tp_tunnel *tunnel;
977
978 tunnel = l2tp_sock_to_tunnel(sk);
979 if (tunnel == NULL)
980 goto pass_up;
981
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000982 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
983 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000984
985 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
986 goto pass_up_put;
987
988 sock_put(sk);
989 return 0;
990
991pass_up_put:
992 sock_put(sk);
993pass_up:
994 return 1;
995}
996EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
997
998/************************************************************************
999 * Transmit handling
1000 ***********************************************************************/
1001
1002/* Build an L2TP header for the session into the buffer provided.
1003 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001004static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001005{
James Chapmanf7faffa2010-04-02 06:18:49 +00001006 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001007 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001008 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001009 u16 flags = L2TP_HDR_VER_2;
1010 u32 tunnel_id = tunnel->peer_tunnel_id;
1011 u32 session_id = session->peer_session_id;
1012
1013 if (session->send_seq)
1014 flags |= L2TP_HDRFLAG_S;
1015
1016 /* Setup L2TP header. */
1017 *bufp++ = htons(flags);
1018 *bufp++ = htons(tunnel_id);
1019 *bufp++ = htons(session_id);
1020 if (session->send_seq) {
1021 *bufp++ = htons(session->ns);
1022 *bufp++ = 0;
1023 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001024 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001025 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1026 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001027 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001028
1029 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001030}
1031
James Chapmanf7faffa2010-04-02 06:18:49 +00001032static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001033{
James Chapman0d767512010-04-02 06:19:00 +00001034 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001035 char *bufp = buf;
1036 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001037
James Chapman0d767512010-04-02 06:19:00 +00001038 /* Setup L2TP header. The header differs slightly for UDP and
1039 * IP encapsulations. For UDP, there is 4 bytes of flags.
1040 */
1041 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1042 u16 flags = L2TP_HDR_VER_3;
1043 *((__be16 *) bufp) = htons(flags);
1044 bufp += 2;
1045 *((__be16 *) bufp) = 0;
1046 bufp += 2;
1047 }
1048
James Chapmanf7faffa2010-04-02 06:18:49 +00001049 *((__be32 *) bufp) = htonl(session->peer_session_id);
1050 bufp += 4;
1051 if (session->cookie_len) {
1052 memcpy(bufp, &session->cookie[0], session->cookie_len);
1053 bufp += session->cookie_len;
1054 }
1055 if (session->l2specific_len) {
1056 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1057 u32 l2h = 0;
1058 if (session->send_seq) {
1059 l2h = 0x40000000 | session->ns;
1060 session->ns++;
1061 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001062 l2tp_dbg(session, L2TP_MSG_SEQ,
1063 "%s: updated ns to %u\n",
1064 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001065 }
1066
1067 *((__be32 *) bufp) = htonl(l2h);
1068 }
1069 bufp += session->l2specific_len;
1070 }
1071 if (session->offset)
1072 bufp += session->offset;
1073
1074 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001075}
James Chapmanfd558d12010-04-02 06:18:33 +00001076
stephen hemmingerfc130842010-10-21 07:50:46 +00001077static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001078 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001079{
1080 struct l2tp_tunnel *tunnel = session->tunnel;
1081 unsigned int len = skb->len;
1082 int error;
James Chapman5de7aee2012-04-29 21:48:46 +00001083 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +00001084
1085 /* Debug */
1086 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001087 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1088 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001089 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001090 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1091 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001092
1093 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001094 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1095 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001096
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001097 pr_debug("%s: xmit\n", session->name);
1098 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1099 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001100 }
1101
1102 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001103 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001104#if IS_ENABLED(CONFIG_IPV6)
1105 if (skb->sk->sk_family == PF_INET6)
1106 error = inet6_csk_xmit(skb, NULL);
1107 else
1108#endif
1109 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001110
1111 /* Update stats */
James Chapman5de7aee2012-04-29 21:48:46 +00001112 tstats = &tunnel->stats;
1113 u64_stats_update_begin(&tstats->syncp);
1114 sstats = &session->stats;
1115 u64_stats_update_begin(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001116 if (error >= 0) {
James Chapman5de7aee2012-04-29 21:48:46 +00001117 tstats->tx_packets++;
1118 tstats->tx_bytes += len;
1119 sstats->tx_packets++;
1120 sstats->tx_bytes += len;
James Chapmanfd558d12010-04-02 06:18:33 +00001121 } else {
James Chapman5de7aee2012-04-29 21:48:46 +00001122 tstats->tx_errors++;
1123 sstats->tx_errors++;
James Chapmanfd558d12010-04-02 06:18:33 +00001124 }
James Chapman5de7aee2012-04-29 21:48:46 +00001125 u64_stats_update_end(&tstats->syncp);
1126 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001127
1128 return 0;
1129}
James Chapmanfd558d12010-04-02 06:18:33 +00001130
1131/* Automatically called when the skb is freed.
1132 */
1133static void l2tp_sock_wfree(struct sk_buff *skb)
1134{
1135 sock_put(skb->sk);
1136}
1137
1138/* For data skbs that we transmit, we associate with the tunnel socket
1139 * but don't do accounting.
1140 */
1141static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1142{
1143 sock_hold(sk);
1144 skb->sk = sk;
1145 skb->destructor = l2tp_sock_wfree;
1146}
1147
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001148#if IS_ENABLED(CONFIG_IPV6)
1149static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1150 int udp_len)
1151{
1152 struct ipv6_pinfo *np = inet6_sk(sk);
1153 struct udphdr *uh = udp_hdr(skb);
1154
1155 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1156 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1157 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1158 skb->ip_summed = CHECKSUM_UNNECESSARY;
1159 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1160 IPPROTO_UDP, csum);
1161 if (uh->check == 0)
1162 uh->check = CSUM_MANGLED_0;
1163 } else {
1164 skb->ip_summed = CHECKSUM_PARTIAL;
1165 skb->csum_start = skb_transport_header(skb) - skb->head;
1166 skb->csum_offset = offsetof(struct udphdr, check);
1167 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1168 udp_len, IPPROTO_UDP, 0);
1169 }
1170}
1171#endif
1172
James Chapmanfd558d12010-04-02 06:18:33 +00001173/* If caller requires the skb to have a ppp header, the header must be
1174 * inserted in the skb data before calling this function.
1175 */
1176int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1177{
1178 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001179 struct l2tp_tunnel *tunnel = session->tunnel;
1180 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001181 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001182 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001183 struct inet_sock *inet;
1184 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001185 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001186 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1187 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001188 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001189
1190 /* Check that there's enough headroom in the skb to insert IP,
1191 * UDP and L2TP headers. If not enough, expand it to
1192 * make room. Adjust truesize.
1193 */
1194 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001195 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001196 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001197 kfree_skb(skb);
1198 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001199 }
James Chapmanfd558d12010-04-02 06:18:33 +00001200
James Chapmanfd558d12010-04-02 06:18:33 +00001201 skb_orphan(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001202 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001203 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001204
James Chapman0d767512010-04-02 06:19:00 +00001205 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001206 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1207 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1208 IPSKB_REROUTED);
1209 nf_reset(skb);
1210
David S. Miller6af88da2011-05-08 13:45:20 -07001211 bh_lock_sock(sk);
1212 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001213 kfree_skb(skb);
1214 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001215 goto out_unlock;
1216 }
1217
James Chapmanfd558d12010-04-02 06:18:33 +00001218 /* Get routing info from the tunnel socket */
1219 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001220 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001221
David S. Millerd9d8da82011-05-06 22:23:20 -07001222 inet = inet_sk(sk);
1223 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001224 switch (tunnel->encap) {
1225 case L2TP_ENCAPTYPE_UDP:
1226 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001227 __skb_push(skb, sizeof(*uh));
1228 skb_reset_transport_header(skb);
1229 uh = udp_hdr(skb);
1230 uh->source = inet->inet_sport;
1231 uh->dest = inet->inet_dport;
1232 udp_len = uhlen + hdr_len + data_len;
1233 uh->len = htons(udp_len);
1234 uh->check = 0;
1235
1236 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001237#if IS_ENABLED(CONFIG_IPV6)
1238 if (sk->sk_family == PF_INET6)
1239 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1240 else
1241#endif
James Chapman0d767512010-04-02 06:19:00 +00001242 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1243 skb->ip_summed = CHECKSUM_NONE;
1244 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1245 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1246 skb->ip_summed = CHECKSUM_COMPLETE;
1247 csum = skb_checksum(skb, 0, udp_len, 0);
1248 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1249 inet->inet_daddr,
1250 udp_len, IPPROTO_UDP, csum);
1251 if (uh->check == 0)
1252 uh->check = CSUM_MANGLED_0;
1253 } else {
1254 skb->ip_summed = CHECKSUM_PARTIAL;
1255 skb->csum_start = skb_transport_header(skb) - skb->head;
1256 skb->csum_offset = offsetof(struct udphdr, check);
1257 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1258 inet->inet_daddr,
1259 udp_len, IPPROTO_UDP, 0);
1260 }
1261 break;
1262
1263 case L2TP_ENCAPTYPE_IP:
1264 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001265 }
1266
James Chapman0d767512010-04-02 06:19:00 +00001267 l2tp_skb_set_owner_w(skb, sk);
1268
David S. Millerd9d8da82011-05-06 22:23:20 -07001269 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001270out_unlock:
1271 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001272
Eric Dumazetb8c84302012-06-28 20:15:13 +00001273 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001274}
1275EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1276
1277/*****************************************************************************
1278 * Tinnel and session create/destroy.
1279 *****************************************************************************/
1280
1281/* Tunnel socket destruct hook.
1282 * The tunnel context is deleted only when all session sockets have been
1283 * closed.
1284 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001285static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001286{
1287 struct l2tp_tunnel *tunnel;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001288 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001289
1290 tunnel = sk->sk_user_data;
1291 if (tunnel == NULL)
1292 goto end;
1293
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001294 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001295
James Chapmanfd558d12010-04-02 06:18:33 +00001296
Tom Parkinf8ccac02013-01-31 23:43:00 +00001297 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001298 switch (tunnel->encap) {
1299 case L2TP_ENCAPTYPE_UDP:
1300 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1301 (udp_sk(sk))->encap_type = 0;
1302 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001303 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001304 break;
1305 case L2TP_ENCAPTYPE_IP:
1306 break;
1307 }
James Chapmanfd558d12010-04-02 06:18:33 +00001308
1309 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001310 sk->sk_destruct = tunnel->old_sk_destruct;
1311 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001312 tunnel->sock = NULL;
1313
1314 /* Remove the tunnel struct from the tunnel list */
1315 pn = l2tp_pernet(tunnel->l2tp_net);
1316 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1317 list_del_rcu(&tunnel->list);
1318 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1319 atomic_dec(&l2tp_tunnel_count);
1320
1321 l2tp_tunnel_closeall(tunnel);
1322 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001323
1324 /* Call the original destructor */
1325 if (sk->sk_destruct)
1326 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001327end:
1328 return;
1329}
James Chapmanfd558d12010-04-02 06:18:33 +00001330
1331/* When the tunnel is closed, all the attached sessions need to go too.
1332 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001333void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001334{
1335 int hash;
1336 struct hlist_node *walk;
1337 struct hlist_node *tmp;
1338 struct l2tp_session *session;
1339
1340 BUG_ON(tunnel == NULL);
1341
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001342 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1343 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001344
1345 write_lock_bh(&tunnel->hlist_lock);
1346 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1347again:
1348 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1349 session = hlist_entry(walk, struct l2tp_session, hlist);
1350
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001351 l2tp_info(session, L2TP_MSG_CONTROL,
1352 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001353
1354 hlist_del_init(&session->hlist);
1355
1356 /* Since we should hold the sock lock while
1357 * doing any unbinding, we need to release the
1358 * lock we're holding before taking that lock.
1359 * Hold a reference to the sock so it doesn't
1360 * disappear as we're jumping between locks.
1361 */
1362 if (session->ref != NULL)
1363 (*session->ref)(session);
1364
1365 write_unlock_bh(&tunnel->hlist_lock);
1366
James Chapmanf7faffa2010-04-02 06:18:49 +00001367 if (tunnel->version != L2TP_HDR_VER_2) {
1368 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1369
James Chapmane02d4942010-04-02 06:19:16 +00001370 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1371 hlist_del_init_rcu(&session->global_hlist);
1372 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1373 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001374 }
1375
James Chapmanfd558d12010-04-02 06:18:33 +00001376 if (session->session_close != NULL)
1377 (*session->session_close)(session);
1378
1379 if (session->deref != NULL)
1380 (*session->deref)(session);
1381
Tom Parkin9980d002013-03-19 06:11:13 +00001382 l2tp_session_dec_refcount(session);
1383
James Chapmanfd558d12010-04-02 06:18:33 +00001384 write_lock_bh(&tunnel->hlist_lock);
1385
1386 /* Now restart from the beginning of this hash
1387 * chain. We always remove a session from the
1388 * list so we are guaranteed to make forward
1389 * progress.
1390 */
1391 goto again;
1392 }
1393 }
1394 write_unlock_bh(&tunnel->hlist_lock);
1395}
Tom Parkine34f4c72013-03-19 06:11:14 +00001396EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001397
Tom Parkin9980d002013-03-19 06:11:13 +00001398/* Tunnel socket destroy hook for UDP encapsulation */
1399static void l2tp_udp_encap_destroy(struct sock *sk)
1400{
1401 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1402 if (tunnel) {
1403 l2tp_tunnel_closeall(tunnel);
1404 sock_put(sk);
1405 }
1406}
1407
James Chapmanfd558d12010-04-02 06:18:33 +00001408/* Really kill the tunnel.
1409 * Come here only when all sessions have been cleared from the tunnel.
1410 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001411static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001412{
James Chapmanfd558d12010-04-02 06:18:33 +00001413 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1414 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001415 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001416 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001417}
James Chapmanfd558d12010-04-02 06:18:33 +00001418
Tom Parkinf8ccac02013-01-31 23:43:00 +00001419/* Workqueue tunnel deletion function */
1420static void l2tp_tunnel_del_work(struct work_struct *work)
1421{
1422 struct l2tp_tunnel *tunnel = NULL;
1423 struct socket *sock = NULL;
1424 struct sock *sk = NULL;
1425
1426 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1427 sk = l2tp_tunnel_sock_lookup(tunnel);
1428 if (!sk)
1429 return;
1430
1431 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001432
Tom Parkin02d13ed2013-03-19 06:11:18 +00001433 /* If the tunnel socket was created by userspace, then go through the
1434 * inet layer to shut the socket down, and let userspace close it.
1435 * Otherwise, if we created the socket directly within the kernel, use
1436 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001437 * In either case the tunnel resources are freed in the socket
1438 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001439 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001440 if (tunnel->fd >= 0) {
1441 if (sock)
1442 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001443 } else {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001444 if (sock)
1445 kernel_sock_shutdown(sock, SHUT_RDWR);
1446 sk_release_kernel(sk);
Tom Parkin167eb172013-01-31 23:43:03 +00001447 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001448
1449 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001450}
James Chapmanfd558d12010-04-02 06:18:33 +00001451
James Chapman789a4a22010-04-02 06:19:40 +00001452/* Create a socket for the tunnel, if one isn't set up by
1453 * userspace. This is used for static tunnels where there is no
1454 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001455 *
1456 * Since we don't want these sockets to keep a namespace alive by
1457 * themselves, we drop the socket's namespace refcount after creation.
1458 * These sockets are freed when the namespace exits using the pernet
1459 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001460 */
Tom Parkin167eb172013-01-31 23:43:03 +00001461static int l2tp_tunnel_sock_create(struct net *net,
1462 u32 tunnel_id,
1463 u32 peer_tunnel_id,
1464 struct l2tp_tunnel_cfg *cfg,
1465 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001466{
1467 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001468 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001469 struct sockaddr_in udp_addr = {0};
1470 struct sockaddr_l2tpip ip_addr = {0};
1471#if IS_ENABLED(CONFIG_IPV6)
1472 struct sockaddr_in6 udp6_addr = {0};
1473 struct sockaddr_l2tpip6 ip6_addr = {0};
1474#endif
James Chapman789a4a22010-04-02 06:19:40 +00001475
1476 switch (cfg->encap) {
1477 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001478#if IS_ENABLED(CONFIG_IPV6)
1479 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001480 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001481 if (err < 0)
1482 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001483
Tom Parkin167eb172013-01-31 23:43:03 +00001484 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001485
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001486 udp6_addr.sin6_family = AF_INET6;
1487 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1488 sizeof(udp6_addr.sin6_addr));
1489 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1490 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1491 sizeof(udp6_addr));
1492 if (err < 0)
1493 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001494
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001495 udp6_addr.sin6_family = AF_INET6;
1496 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1497 sizeof(udp6_addr.sin6_addr));
1498 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1499 err = kernel_connect(sock,
1500 (struct sockaddr *) &udp6_addr,
1501 sizeof(udp6_addr), 0);
1502 if (err < 0)
1503 goto out;
1504 } else
1505#endif
1506 {
Tom Parkin167eb172013-01-31 23:43:03 +00001507 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001508 if (err < 0)
1509 goto out;
1510
Tom Parkin167eb172013-01-31 23:43:03 +00001511 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001512
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001513 udp_addr.sin_family = AF_INET;
1514 udp_addr.sin_addr = cfg->local_ip;
1515 udp_addr.sin_port = htons(cfg->local_udp_port);
1516 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1517 sizeof(udp_addr));
1518 if (err < 0)
1519 goto out;
1520
1521 udp_addr.sin_family = AF_INET;
1522 udp_addr.sin_addr = cfg->peer_ip;
1523 udp_addr.sin_port = htons(cfg->peer_udp_port);
1524 err = kernel_connect(sock,
1525 (struct sockaddr *) &udp_addr,
1526 sizeof(udp_addr), 0);
1527 if (err < 0)
1528 goto out;
1529 }
James Chapman789a4a22010-04-02 06:19:40 +00001530
1531 if (!cfg->use_udp_checksums)
1532 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1533
1534 break;
1535
1536 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001537#if IS_ENABLED(CONFIG_IPV6)
1538 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001539 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1540 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001541 if (err < 0)
1542 goto out;
1543
Tom Parkin167eb172013-01-31 23:43:03 +00001544 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001545
James Chapman5dac94e2012-04-29 21:48:55 +00001546 ip6_addr.l2tp_family = AF_INET6;
1547 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1548 sizeof(ip6_addr.l2tp_addr));
1549 ip6_addr.l2tp_conn_id = tunnel_id;
1550 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1551 sizeof(ip6_addr));
1552 if (err < 0)
1553 goto out;
1554
1555 ip6_addr.l2tp_family = AF_INET6;
1556 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1557 sizeof(ip6_addr.l2tp_addr));
1558 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1559 err = kernel_connect(sock,
1560 (struct sockaddr *) &ip6_addr,
1561 sizeof(ip6_addr), 0);
1562 if (err < 0)
1563 goto out;
1564 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001565#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001566 {
Tom Parkin167eb172013-01-31 23:43:03 +00001567 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1568 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001569 if (err < 0)
1570 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001571
Tom Parkin167eb172013-01-31 23:43:03 +00001572 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001573
James Chapman5dac94e2012-04-29 21:48:55 +00001574 ip_addr.l2tp_family = AF_INET;
1575 ip_addr.l2tp_addr = cfg->local_ip;
1576 ip_addr.l2tp_conn_id = tunnel_id;
1577 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1578 sizeof(ip_addr));
1579 if (err < 0)
1580 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001581
James Chapman5dac94e2012-04-29 21:48:55 +00001582 ip_addr.l2tp_family = AF_INET;
1583 ip_addr.l2tp_addr = cfg->peer_ip;
1584 ip_addr.l2tp_conn_id = peer_tunnel_id;
1585 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1586 sizeof(ip_addr), 0);
1587 if (err < 0)
1588 goto out;
1589 }
James Chapman789a4a22010-04-02 06:19:40 +00001590 break;
1591
1592 default:
1593 goto out;
1594 }
1595
1596out:
Tom Parkin167eb172013-01-31 23:43:03 +00001597 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001598 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001599 kernel_sock_shutdown(sock, SHUT_RDWR);
1600 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001601 *sockp = NULL;
1602 }
1603
1604 return err;
1605}
1606
Eric Dumazet37159ef2012-09-04 07:18:57 +00001607static struct lock_class_key l2tp_socket_class;
1608
James Chapmanfd558d12010-04-02 06:18:33 +00001609int 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)
1610{
1611 struct l2tp_tunnel *tunnel = NULL;
1612 int err;
1613 struct socket *sock = NULL;
1614 struct sock *sk = NULL;
1615 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001616 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001617
1618 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001619 * the userspace L2TP daemon. If not specified, create a
1620 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001621 */
James Chapman789a4a22010-04-02 06:19:40 +00001622 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001623 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1624 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001625 if (err < 0)
1626 goto err;
1627 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001628 sock = sockfd_lookup(fd, &err);
1629 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001630 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001631 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001632 err = -EBADF;
1633 goto err;
1634 }
1635
1636 /* Reject namespace mismatches */
1637 if (!net_eq(sock_net(sock->sk), net)) {
1638 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1639 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001640 goto err;
1641 }
James Chapmanfd558d12010-04-02 06:18:33 +00001642 }
1643
1644 sk = sock->sk;
1645
James Chapman0d767512010-04-02 06:19:00 +00001646 if (cfg != NULL)
1647 encap = cfg->encap;
1648
James Chapmanfd558d12010-04-02 06:18:33 +00001649 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001650 switch (encap) {
1651 case L2TP_ENCAPTYPE_UDP:
1652 err = -EPROTONOSUPPORT;
1653 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001654 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001655 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1656 goto err;
1657 }
1658 break;
1659 case L2TP_ENCAPTYPE_IP:
1660 err = -EPROTONOSUPPORT;
1661 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001662 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001663 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1664 goto err;
1665 }
1666 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001667 }
1668
1669 /* Check if this socket has already been prepped */
1670 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1671 if (tunnel != NULL) {
1672 /* This socket has already been prepped */
1673 err = -EBUSY;
1674 goto err;
1675 }
1676
James Chapmanfd558d12010-04-02 06:18:33 +00001677 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1678 if (tunnel == NULL) {
1679 err = -ENOMEM;
1680 goto err;
1681 }
1682
1683 tunnel->version = version;
1684 tunnel->tunnel_id = tunnel_id;
1685 tunnel->peer_tunnel_id = peer_tunnel_id;
1686 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1687
1688 tunnel->magic = L2TP_TUNNEL_MAGIC;
1689 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1690 rwlock_init(&tunnel->hlist_lock);
1691
1692 /* The net we belong to */
1693 tunnel->l2tp_net = net;
1694 pn = l2tp_pernet(net);
1695
James Chapman0d767512010-04-02 06:19:00 +00001696 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001697 tunnel->debug = cfg->debug;
1698
1699 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001700 tunnel->encap = encap;
1701 if (encap == L2TP_ENCAPTYPE_UDP) {
1702 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1703 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1704 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001705 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001706#if IS_ENABLED(CONFIG_IPV6)
1707 if (sk->sk_family == PF_INET6)
1708 udpv6_encap_enable();
1709 else
1710#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001711 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001712 }
James Chapmanfd558d12010-04-02 06:18:33 +00001713
1714 sk->sk_user_data = tunnel;
1715
1716 /* Hook on the tunnel socket destructor so that we can cleanup
1717 * if the tunnel socket goes away.
1718 */
1719 tunnel->old_sk_destruct = sk->sk_destruct;
1720 sk->sk_destruct = &l2tp_tunnel_destruct;
1721 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001722 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001723 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1724
James Chapmanfd558d12010-04-02 06:18:33 +00001725 sk->sk_allocation = GFP_ATOMIC;
1726
Tom Parkinf8ccac02013-01-31 23:43:00 +00001727 /* Init delete workqueue struct */
1728 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1729
James Chapmanfd558d12010-04-02 06:18:33 +00001730 /* Add tunnel to our list */
1731 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001732 atomic_inc(&l2tp_tunnel_count);
1733
1734 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001735 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001736 */
1737 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001738 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1739 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1740 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001741
1742 err = 0;
1743err:
1744 if (tunnelp)
1745 *tunnelp = tunnel;
1746
James Chapman789a4a22010-04-02 06:19:40 +00001747 /* If tunnel's socket was created by the kernel, it doesn't
1748 * have a file.
1749 */
1750 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001751 sockfd_put(sock);
1752
1753 return err;
1754}
1755EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1756
James Chapman309795f2010-04-02 06:19:10 +00001757/* This function is used by the netlink TUNNEL_DELETE command.
1758 */
1759int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1760{
Tom Parkin2b551c62013-03-19 06:11:16 +00001761 l2tp_tunnel_closeall(tunnel);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001762 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001763}
1764EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1765
James Chapmanfd558d12010-04-02 06:18:33 +00001766/* Really kill the session.
1767 */
1768void l2tp_session_free(struct l2tp_session *session)
1769{
1770 struct l2tp_tunnel *tunnel;
1771
1772 BUG_ON(atomic_read(&session->ref_count) != 0);
1773
1774 tunnel = session->tunnel;
1775 if (tunnel != NULL) {
1776 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1777
1778 /* Delete the session from the hash */
1779 write_lock_bh(&tunnel->hlist_lock);
1780 hlist_del_init(&session->hlist);
1781 write_unlock_bh(&tunnel->hlist_lock);
1782
James Chapmanf7faffa2010-04-02 06:18:49 +00001783 /* Unlink from the global hash if not L2TPv2 */
1784 if (tunnel->version != L2TP_HDR_VER_2) {
1785 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1786
James Chapmane02d4942010-04-02 06:19:16 +00001787 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1788 hlist_del_init_rcu(&session->global_hlist);
1789 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1790 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001791 }
1792
James Chapmanfd558d12010-04-02 06:18:33 +00001793 if (session->session_id != 0)
1794 atomic_dec(&l2tp_session_count);
1795
1796 sock_put(tunnel->sock);
1797
1798 /* This will delete the tunnel context if this
1799 * is the last session on the tunnel.
1800 */
1801 session->tunnel = NULL;
1802 l2tp_tunnel_dec_refcount(tunnel);
1803 }
1804
1805 kfree(session);
1806
1807 return;
1808}
1809EXPORT_SYMBOL_GPL(l2tp_session_free);
1810
James Chapman309795f2010-04-02 06:19:10 +00001811/* This function is used by the netlink SESSION_DELETE command and by
1812 pseudowire modules.
1813 */
1814int l2tp_session_delete(struct l2tp_session *session)
1815{
1816 if (session->session_close != NULL)
1817 (*session->session_close)(session);
1818
1819 l2tp_session_dec_refcount(session);
1820
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(l2tp_session_delete);
1824
1825
James Chapmanf7faffa2010-04-02 06:18:49 +00001826/* We come here whenever a session's send_seq, cookie_len or
1827 * l2specific_len parameters are set.
1828 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001829static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001830{
1831 if (version == L2TP_HDR_VER_2) {
1832 session->hdr_len = 6;
1833 if (session->send_seq)
1834 session->hdr_len += 4;
1835 } else {
James Chapman0d767512010-04-02 06:19:00 +00001836 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1837 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1838 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001839 }
1840
1841}
James Chapmanf7faffa2010-04-02 06:18:49 +00001842
James Chapmanfd558d12010-04-02 06:18:33 +00001843struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1844{
1845 struct l2tp_session *session;
1846
1847 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1848 if (session != NULL) {
1849 session->magic = L2TP_SESSION_MAGIC;
1850 session->tunnel = tunnel;
1851
1852 session->session_id = session_id;
1853 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001854 session->nr = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001855
1856 sprintf(&session->name[0], "sess %u/%u",
1857 tunnel->tunnel_id, session->session_id);
1858
1859 skb_queue_head_init(&session->reorder_q);
1860
1861 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001862 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001863
1864 /* Inherit debug options from tunnel */
1865 session->debug = tunnel->debug;
1866
1867 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001868 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001869 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001870 session->mtu = cfg->mtu;
1871 session->mru = cfg->mru;
1872 session->send_seq = cfg->send_seq;
1873 session->recv_seq = cfg->recv_seq;
1874 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001875 session->reorder_timeout = cfg->reorder_timeout;
1876 session->offset = cfg->offset;
1877 session->l2specific_type = cfg->l2specific_type;
1878 session->l2specific_len = cfg->l2specific_len;
1879 session->cookie_len = cfg->cookie_len;
1880 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1881 session->peer_cookie_len = cfg->peer_cookie_len;
1882 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001883 }
1884
James Chapmanf7faffa2010-04-02 06:18:49 +00001885 if (tunnel->version == L2TP_HDR_VER_2)
1886 session->build_header = l2tp_build_l2tpv2_header;
1887 else
1888 session->build_header = l2tp_build_l2tpv3_header;
1889
1890 l2tp_session_set_header_len(session, tunnel->version);
1891
James Chapmanfd558d12010-04-02 06:18:33 +00001892 /* Bump the reference count. The session context is deleted
1893 * only when this drops to zero.
1894 */
1895 l2tp_session_inc_refcount(session);
1896 l2tp_tunnel_inc_refcount(tunnel);
1897
1898 /* Ensure tunnel socket isn't deleted */
1899 sock_hold(tunnel->sock);
1900
1901 /* Add session to the tunnel's hash list */
1902 write_lock_bh(&tunnel->hlist_lock);
1903 hlist_add_head(&session->hlist,
1904 l2tp_session_id_hash(tunnel, session_id));
1905 write_unlock_bh(&tunnel->hlist_lock);
1906
James Chapmanf7faffa2010-04-02 06:18:49 +00001907 /* And to the global session list if L2TPv3 */
1908 if (tunnel->version != L2TP_HDR_VER_2) {
1909 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1910
James Chapmane02d4942010-04-02 06:19:16 +00001911 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1912 hlist_add_head_rcu(&session->global_hlist,
1913 l2tp_session_id_hash_2(pn, session_id));
1914 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001915 }
1916
James Chapmanfd558d12010-04-02 06:18:33 +00001917 /* Ignore management session in session count value */
1918 if (session->session_id != 0)
1919 atomic_inc(&l2tp_session_count);
1920 }
1921
1922 return session;
1923}
1924EXPORT_SYMBOL_GPL(l2tp_session_create);
1925
1926/*****************************************************************************
1927 * Init and cleanup
1928 *****************************************************************************/
1929
1930static __net_init int l2tp_init_net(struct net *net)
1931{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001932 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001933 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001934
James Chapmanfd558d12010-04-02 06:18:33 +00001935 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001936 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001937
James Chapmanf7faffa2010-04-02 06:18:49 +00001938 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1939 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1940
James Chapmane02d4942010-04-02 06:19:16 +00001941 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001942
James Chapmanfd558d12010-04-02 06:18:33 +00001943 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001944}
1945
Tom Parkin167eb172013-01-31 23:43:03 +00001946static __net_exit void l2tp_exit_net(struct net *net)
1947{
1948 struct l2tp_net *pn = l2tp_pernet(net);
1949 struct l2tp_tunnel *tunnel = NULL;
1950
1951 rcu_read_lock_bh();
1952 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1953 (void)l2tp_tunnel_delete(tunnel);
1954 }
1955 rcu_read_unlock_bh();
1956}
1957
James Chapmanfd558d12010-04-02 06:18:33 +00001958static struct pernet_operations l2tp_net_ops = {
1959 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001960 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001961 .id = &l2tp_net_id,
1962 .size = sizeof(struct l2tp_net),
1963};
1964
1965static int __init l2tp_init(void)
1966{
1967 int rc = 0;
1968
1969 rc = register_pernet_device(&l2tp_net_ops);
1970 if (rc)
1971 goto out;
1972
Tom Parkinf8ccac02013-01-31 23:43:00 +00001973 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1974 if (!l2tp_wq) {
1975 pr_err("alloc_workqueue failed\n");
1976 rc = -ENOMEM;
1977 goto out;
1978 }
1979
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001980 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001981
1982out:
1983 return rc;
1984}
1985
1986static void __exit l2tp_exit(void)
1987{
1988 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001989 if (l2tp_wq) {
1990 destroy_workqueue(l2tp_wq);
1991 l2tp_wq = NULL;
1992 }
James Chapmanfd558d12010-04-02 06:18:33 +00001993}
1994
1995module_init(l2tp_init);
1996module_exit(l2tp_exit);
1997
1998MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1999MODULE_DESCRIPTION("L2TP core");
2000MODULE_LICENSE("GPL");
2001MODULE_VERSION(L2TP_DRV_VERSION);
2002