blob: dcab9ddc4388000fa808e8ff8e00b4f6f228e3de [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
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)
151#define l2tp_tunnel_dec_refcount(_t)
152do { \
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
281struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
282{
283 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000284 struct l2tp_session *session;
285 int count = 0;
286
287 read_lock_bh(&tunnel->hlist_lock);
288 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800289 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000290 if (++count > nth) {
291 read_unlock_bh(&tunnel->hlist_lock);
292 return session;
293 }
294 }
295 }
296
297 read_unlock_bh(&tunnel->hlist_lock);
298
299 return NULL;
300}
301EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
302
James Chapman309795f2010-04-02 06:19:10 +0000303/* Lookup a session by interface name.
304 * This is very inefficient but is only used by management interfaces.
305 */
306struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
307{
308 struct l2tp_net *pn = l2tp_pernet(net);
309 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000310 struct l2tp_session *session;
311
James Chapmane02d4942010-04-02 06:19:16 +0000312 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000313 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800314 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000315 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000316 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000317 return session;
318 }
319 }
320 }
321
James Chapmane02d4942010-04-02 06:19:16 +0000322 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000323
324 return NULL;
325}
326EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
327
James Chapmanfd558d12010-04-02 06:18:33 +0000328/* Lookup a tunnel by id
329 */
330struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
331{
332 struct l2tp_tunnel *tunnel;
333 struct l2tp_net *pn = l2tp_pernet(net);
334
James Chapmane02d4942010-04-02 06:19:16 +0000335 rcu_read_lock_bh();
336 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000337 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000338 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000339 return tunnel;
340 }
341 }
James Chapmane02d4942010-04-02 06:19:16 +0000342 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000343
344 return NULL;
345}
346EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
347
348struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
349{
350 struct l2tp_net *pn = l2tp_pernet(net);
351 struct l2tp_tunnel *tunnel;
352 int count = 0;
353
James Chapmane02d4942010-04-02 06:19:16 +0000354 rcu_read_lock_bh();
355 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000356 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000357 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000358 return tunnel;
359 }
360 }
361
James Chapmane02d4942010-04-02 06:19:16 +0000362 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000363
364 return NULL;
365}
366EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
367
368/*****************************************************************************
369 * Receive data handling
370 *****************************************************************************/
371
372/* Queue a skb in order. We come here only if the skb has an L2TP sequence
373 * number.
374 */
375static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
376{
377 struct sk_buff *skbp;
378 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000379 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000380
381 spin_lock_bh(&session->reorder_q.lock);
382 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
383 if (L2TP_SKB_CB(skbp)->ns > ns) {
384 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000385 l2tp_dbg(session, L2TP_MSG_SEQ,
386 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
387 session->name, ns, L2TP_SKB_CB(skbp)->ns,
388 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000389 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000390 goto out;
391 }
392 }
393
394 __skb_queue_tail(&session->reorder_q, skb);
395
396out:
397 spin_unlock_bh(&session->reorder_q.lock);
398}
399
400/* Dequeue a single skb.
401 */
402static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
403{
404 struct l2tp_tunnel *tunnel = session->tunnel;
405 int length = L2TP_SKB_CB(skb)->length;
406
407 /* We're about to requeue the skb, so return resources
408 * to its current owner (a socket receive buffer).
409 */
410 skb_orphan(skb);
411
Tom Parkin7b7c0712013-03-19 06:11:22 +0000412 atomic_long_inc(&tunnel->stats.rx_packets);
413 atomic_long_add(length, &tunnel->stats.rx_bytes);
414 atomic_long_inc(&session->stats.rx_packets);
415 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000416
417 if (L2TP_SKB_CB(skb)->has_seq) {
418 /* Bump our Nr */
419 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100420 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000421
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000422 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
423 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000424 }
425
426 /* call private receive handler */
427 if (session->recv_skb != NULL)
428 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
429 else
430 kfree_skb(skb);
431
432 if (session->deref)
433 (*session->deref)(session);
434}
435
436/* Dequeue skbs from the session's reorder_q, subject to packet order.
437 * Skbs that have been in the queue for too long are simply discarded.
438 */
439static void l2tp_recv_dequeue(struct l2tp_session *session)
440{
441 struct sk_buff *skb;
442 struct sk_buff *tmp;
443
444 /* If the pkt at the head of the queue has the nr that we
445 * expect to send up next, dequeue it and any other
446 * in-sequence packets behind it.
447 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000448start:
James Chapmanfd558d12010-04-02 06:18:33 +0000449 spin_lock_bh(&session->reorder_q.lock);
450 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
451 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000452 atomic_long_inc(&session->stats.rx_seq_discards);
453 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000454 l2tp_dbg(session, L2TP_MSG_SEQ,
455 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
456 session->name, L2TP_SKB_CB(skb)->ns,
457 L2TP_SKB_CB(skb)->length, session->nr,
458 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000459 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000460 __skb_unlink(skb, &session->reorder_q);
461 kfree_skb(skb);
462 if (session->deref)
463 (*session->deref)(session);
464 continue;
465 }
466
467 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000468 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000469 l2tp_dbg(session, L2TP_MSG_SEQ,
470 "%s: advancing nr to next pkt: %u -> %u",
471 session->name, session->nr,
472 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000473 session->reorder_skip = 0;
474 session->nr = L2TP_SKB_CB(skb)->ns;
475 }
James Chapmanfd558d12010-04-02 06:18:33 +0000476 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000477 l2tp_dbg(session, L2TP_MSG_SEQ,
478 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
479 session->name, L2TP_SKB_CB(skb)->ns,
480 L2TP_SKB_CB(skb)->length, session->nr,
481 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000482 goto out;
483 }
484 }
485 __skb_unlink(skb, &session->reorder_q);
486
487 /* Process the skb. We release the queue lock while we
488 * do so to let other contexts process the queue.
489 */
490 spin_unlock_bh(&session->reorder_q.lock);
491 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000492 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000493 }
494
495out:
496 spin_unlock_bh(&session->reorder_q.lock);
497}
498
499static inline int l2tp_verify_udp_checksum(struct sock *sk,
500 struct sk_buff *skb)
501{
502 struct udphdr *uh = udp_hdr(skb);
503 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000504 __wsum psum;
505
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000506 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000507 return 0;
508
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000509#if IS_ENABLED(CONFIG_IPV6)
David S. Miller8d8a51e2013-10-08 15:44:26 -0400510 if (sk->sk_family == PF_INET6 && !l2tp_tunnel(sk)->v4mapped) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000511 if (!uh->check) {
512 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
513 return 1;
514 }
515 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
516 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
517 &ipv6_hdr(skb)->daddr, ulen,
518 IPPROTO_UDP, skb->csum)) {
519 skb->ip_summed = CHECKSUM_UNNECESSARY;
520 return 0;
521 }
522 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
523 &ipv6_hdr(skb)->daddr,
524 skb->len, IPPROTO_UDP,
525 0));
526 } else
527#endif
528 {
529 struct inet_sock *inet;
530 if (!uh->check)
531 return 0;
532 inet = inet_sk(sk);
533 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
534 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000535
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000536 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
537 !csum_fold(csum_add(psum, skb->csum)))
538 return 0;
539 skb->csum = psum;
540 }
James Chapmanfd558d12010-04-02 06:18:33 +0000541
542 return __skb_checksum_complete(skb);
543}
544
James Chapman8a1631d2013-07-02 20:28:59 +0100545static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
546{
547 u32 nws;
548
549 if (nr >= session->nr)
550 nws = nr - session->nr;
551 else
552 nws = (session->nr_max + 1) - (session->nr - nr);
553
554 return nws < session->nr_window_size;
555}
556
James Chapmanb6dc01a2013-07-02 20:28:58 +0100557/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
558 * acceptable, else non-zero.
559 */
560static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
561{
James Chapman8a1631d2013-07-02 20:28:59 +0100562 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
563 /* Packet sequence number is outside allowed window.
564 * Discard it.
565 */
566 l2tp_dbg(session, L2TP_MSG_SEQ,
567 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
568 session->name, L2TP_SKB_CB(skb)->ns,
569 L2TP_SKB_CB(skb)->length, session->nr);
570 goto discard;
571 }
572
James Chapmanb6dc01a2013-07-02 20:28:58 +0100573 if (session->reorder_timeout != 0) {
574 /* Packet reordering enabled. Add skb to session's
575 * reorder queue, in order of ns.
576 */
577 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100578 goto out;
579 }
580
581 /* Packet reordering disabled. Discard out-of-sequence packets, while
582 * tracking the number if in-sequence packets after the first OOS packet
583 * is seen. After nr_oos_count_max in-sequence packets, reset the
584 * sequence number to re-enable packet reception.
585 */
586 if (L2TP_SKB_CB(skb)->ns == session->nr) {
587 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100588 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100589 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
590 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
591
592 if (nr_oos == nr_next)
593 session->nr_oos_count++;
594 else
595 session->nr_oos_count = 0;
596
597 session->nr_oos = nr_oos;
598 if (session->nr_oos_count > session->nr_oos_count_max) {
599 session->reorder_skip = 1;
600 l2tp_dbg(session, L2TP_MSG_SEQ,
601 "%s: %d oos packets received. Resetting sequence numbers\n",
602 session->name, session->nr_oos_count);
603 }
604 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100605 atomic_long_inc(&session->stats.rx_seq_discards);
606 l2tp_dbg(session, L2TP_MSG_SEQ,
607 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
608 session->name, L2TP_SKB_CB(skb)->ns,
609 L2TP_SKB_CB(skb)->length, session->nr,
610 skb_queue_len(&session->reorder_q));
611 goto discard;
612 }
613 skb_queue_tail(&session->reorder_q, skb);
614 }
615
James Chapmana0dbd822013-07-02 20:29:00 +0100616out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100617 return 0;
618
619discard:
620 return 1;
621}
622
James Chapmanf7faffa2010-04-02 06:18:49 +0000623/* Do receive processing of L2TP data frames. We handle both L2TPv2
624 * and L2TPv3 data frames here.
625 *
626 * L2TPv2 Data Message Header
627 *
628 * 0 1 2 3
629 * 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
630 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
632 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
633 * | Tunnel ID | Session ID |
634 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
635 * | Ns (opt) | Nr (opt) |
636 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
637 * | Offset Size (opt) | Offset pad... (opt)
638 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
639 *
640 * Data frames are marked by T=0. All other fields are the same as
641 * those in L2TP control frames.
642 *
643 * L2TPv3 Data Message Header
644 *
645 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
646 * | L2TP Session Header |
647 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648 * | L2-Specific Sublayer |
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 * | Tunnel Payload ...
651 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 *
653 * L2TPv3 Session Header Over IP
654 *
655 * 0 1 2 3
656 * 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
657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
658 * | Session ID |
659 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
660 * | Cookie (optional, maximum 64 bits)...
661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
662 * |
663 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
664 *
665 * L2TPv3 L2-Specific Sublayer Format
666 *
667 * 0 1 2 3
668 * 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
669 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670 * |x|S|x|x|x|x|x|x| Sequence Number |
671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
672 *
673 * Cookie value, sublayer format and offset (pad) are negotiated with
674 * the peer when the session is set up. Unlike L2TPv2, we do not need
675 * to parse the packet header to determine if optional fields are
676 * present.
677 *
678 * Caller must already have parsed the frame and determined that it is
679 * a data (not control) frame before coming here. Fields up to the
680 * session-id have already been parsed and ptr points to the data
681 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000682 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000683void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
684 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
685 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000686{
James Chapmanf7faffa2010-04-02 06:18:49 +0000687 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000688 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000689 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000690
691 /* The ref count is increased since we now hold a pointer to
692 * the session. Take care to decrement the refcnt when exiting
693 * this function from now on...
694 */
695 l2tp_session_inc_refcount(session);
696 if (session->ref)
697 (*session->ref)(session);
698
James Chapmanf7faffa2010-04-02 06:18:49 +0000699 /* Parse and check optional cookie */
700 if (session->peer_cookie_len > 0) {
701 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000702 l2tp_info(tunnel, L2TP_MSG_DATA,
703 "%s: cookie mismatch (%u/%u). Discarding.\n",
704 tunnel->name, tunnel->tunnel_id,
705 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000706 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000707 goto discard;
708 }
709 ptr += session->peer_cookie_len;
710 }
711
James Chapmanfd558d12010-04-02 06:18:33 +0000712 /* Handle the optional sequence numbers. Sequence numbers are
713 * in different places for L2TPv2 and L2TPv3.
714 *
715 * If we are the LAC, enable/disable sequence numbers under
716 * the control of the LNS. If no sequence numbers present but
717 * we were expecting them, discard frame.
718 */
719 ns = nr = 0;
720 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000721 if (tunnel->version == L2TP_HDR_VER_2) {
722 if (hdrflags & L2TP_HDRFLAG_S) {
723 ns = ntohs(*(__be16 *) ptr);
724 ptr += 2;
725 nr = ntohs(*(__be16 *) ptr);
726 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000727
James Chapmanf7faffa2010-04-02 06:18:49 +0000728 /* Store L2TP info in the skb */
729 L2TP_SKB_CB(skb)->ns = ns;
730 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000731
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000732 l2tp_dbg(session, L2TP_MSG_SEQ,
733 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
734 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000735 }
736 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
737 u32 l2h = ntohl(*(__be32 *) ptr);
738
739 if (l2h & 0x40000000) {
740 ns = l2h & 0x00ffffff;
741
742 /* Store L2TP info in the skb */
743 L2TP_SKB_CB(skb)->ns = ns;
744 L2TP_SKB_CB(skb)->has_seq = 1;
745
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000746 l2tp_dbg(session, L2TP_MSG_SEQ,
747 "%s: recv data ns=%u, session nr=%u\n",
748 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000749 }
James Chapmanfd558d12010-04-02 06:18:33 +0000750 }
751
James Chapmanf7faffa2010-04-02 06:18:49 +0000752 /* Advance past L2-specific header, if present */
753 ptr += session->l2specific_len;
754
James Chapmanfd558d12010-04-02 06:18:33 +0000755 if (L2TP_SKB_CB(skb)->has_seq) {
756 /* Received a packet with sequence numbers. If we're the LNS,
757 * check if we sre sending sequence numbers and if not,
758 * configure it so.
759 */
760 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000761 l2tp_info(session, L2TP_MSG_SEQ,
762 "%s: requested to enable seq numbers by LNS\n",
763 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000764 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000765 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000766 }
767 } else {
768 /* No sequence numbers.
769 * If user has configured mandatory sequence numbers, discard.
770 */
771 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000772 l2tp_warn(session, L2TP_MSG_SEQ,
773 "%s: recv data has no seq numbers when required. Discarding.\n",
774 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000775 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000776 goto discard;
777 }
778
779 /* If we're the LAC and we're sending sequence numbers, the
780 * LNS has requested that we no longer send sequence numbers.
781 * If we're the LNS and we're sending sequence numbers, the
782 * LAC is broken. Discard the frame.
783 */
784 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000785 l2tp_info(session, L2TP_MSG_SEQ,
786 "%s: requested to disable seq numbers by LNS\n",
787 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000788 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000789 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000790 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000791 l2tp_warn(session, L2TP_MSG_SEQ,
792 "%s: recv data has no seq numbers when required. Discarding.\n",
793 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000794 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000795 goto discard;
796 }
797 }
798
James Chapmanf7faffa2010-04-02 06:18:49 +0000799 /* Session data offset is handled differently for L2TPv2 and
800 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
801 * the header. For L2TPv3, the offset is negotiated using AVPs
802 * in the session setup control protocol.
803 */
804 if (tunnel->version == L2TP_HDR_VER_2) {
805 /* If offset bit set, skip it. */
806 if (hdrflags & L2TP_HDRFLAG_O) {
807 offset = ntohs(*(__be16 *)ptr);
808 ptr += 2 + offset;
809 }
810 } else
811 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000812
813 offset = ptr - optr;
814 if (!pskb_may_pull(skb, offset))
815 goto discard;
816
817 __skb_pull(skb, offset);
818
819 /* If caller wants to process the payload before we queue the
820 * packet, do so now.
821 */
822 if (payload_hook)
823 if ((*payload_hook)(skb))
824 goto discard;
825
826 /* Prepare skb for adding to the session's reorder_q. Hold
827 * packets for max reorder_timeout or 1 second if not
828 * reordering.
829 */
830 L2TP_SKB_CB(skb)->length = length;
831 L2TP_SKB_CB(skb)->expires = jiffies +
832 (session->reorder_timeout ? session->reorder_timeout : HZ);
833
834 /* Add packet to the session's receive queue. Reordering is done here, if
835 * enabled. Saved L2TP protocol info is stored in skb->sb[].
836 */
837 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100838 if (l2tp_recv_data_seq(session, skb))
839 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000840 } else {
841 /* No sequence numbers. Add the skb to the tail of the
842 * reorder queue. This ensures that it will be
843 * delivered after all previous sequenced skbs.
844 */
845 skb_queue_tail(&session->reorder_q, skb);
846 }
847
848 /* Try to dequeue as many skbs from reorder_q as we can. */
849 l2tp_recv_dequeue(session);
850
851 l2tp_session_dec_refcount(session);
852
James Chapmanf7faffa2010-04-02 06:18:49 +0000853 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000854
855discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000856 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000857 kfree_skb(skb);
858
859 if (session->deref)
860 (*session->deref)(session);
861
862 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000863}
864EXPORT_SYMBOL(l2tp_recv_common);
865
Tom Parkin48f72f92013-03-19 06:11:19 +0000866/* Drop skbs from the session's reorder_q
867 */
868int l2tp_session_queue_purge(struct l2tp_session *session)
869{
870 struct sk_buff *skb = NULL;
871 BUG_ON(!session);
872 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
873 while ((skb = skb_dequeue(&session->reorder_q))) {
874 atomic_long_inc(&session->stats.rx_errors);
875 kfree_skb(skb);
876 if (session->deref)
877 (*session->deref)(session);
878 }
879 return 0;
880}
881EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
882
James Chapmanf7faffa2010-04-02 06:18:49 +0000883/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
884 * here. The skb is not on a list when we get here.
885 * Returns 0 if the packet was a data packet and was successfully passed on.
886 * Returns 1 if the packet was not a good data packet and could not be
887 * forwarded. All such packets are passed up to userspace to deal with.
888 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000889static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
890 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000891{
892 struct l2tp_session *session = NULL;
893 unsigned char *ptr, *optr;
894 u16 hdrflags;
895 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000896 u16 version;
897 int length;
898
899 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
900 goto discard_bad_csum;
901
902 /* UDP always verifies the packet length. */
903 __skb_pull(skb, sizeof(struct udphdr));
904
905 /* Short packet? */
906 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000907 l2tp_info(tunnel, L2TP_MSG_DATA,
908 "%s: recv short packet (len=%d)\n",
909 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000910 goto error;
911 }
912
James Chapmanf7faffa2010-04-02 06:18:49 +0000913 /* Trace packet contents, if enabled */
914 if (tunnel->debug & L2TP_MSG_DATA) {
915 length = min(32u, skb->len);
916 if (!pskb_may_pull(skb, length))
917 goto error;
918
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000919 pr_debug("%s: recv\n", tunnel->name);
920 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000921 }
922
Eric Dumazete50e7052011-11-08 13:59:44 -0500923 /* Point to L2TP header */
924 optr = ptr = skb->data;
925
James Chapmanf7faffa2010-04-02 06:18:49 +0000926 /* Get L2TP header flags */
927 hdrflags = ntohs(*(__be16 *) ptr);
928
929 /* Check protocol version */
930 version = hdrflags & L2TP_HDR_VER_MASK;
931 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000932 l2tp_info(tunnel, L2TP_MSG_DATA,
933 "%s: recv protocol version mismatch: got %d expected %d\n",
934 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000935 goto error;
936 }
937
938 /* Get length of L2TP packet */
939 length = skb->len;
940
941 /* If type is control packet, it is handled by userspace. */
942 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000943 l2tp_dbg(tunnel, L2TP_MSG_DATA,
944 "%s: recv control packet, len=%d\n",
945 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000946 goto error;
947 }
948
949 /* Skip flags */
950 ptr += 2;
951
952 if (tunnel->version == L2TP_HDR_VER_2) {
953 /* If length is present, skip it */
954 if (hdrflags & L2TP_HDRFLAG_L)
955 ptr += 2;
956
957 /* Extract tunnel and session ID */
958 tunnel_id = ntohs(*(__be16 *) ptr);
959 ptr += 2;
960 session_id = ntohs(*(__be16 *) ptr);
961 ptr += 2;
962 } else {
963 ptr += 2; /* skip reserved bits */
964 tunnel_id = tunnel->tunnel_id;
965 session_id = ntohl(*(__be32 *) ptr);
966 ptr += 4;
967 }
968
969 /* Find the session context */
970 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000971 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000972 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000973 l2tp_info(tunnel, L2TP_MSG_DATA,
974 "%s: no session found (%u/%u). Passing up.\n",
975 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000976 goto error;
977 }
978
979 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000980
981 return 0;
982
983discard_bad_csum:
984 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
985 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000986 atomic_long_inc(&tunnel->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000987 kfree_skb(skb);
988
989 return 0;
990
991error:
992 /* Put UDP header back */
993 __skb_push(skb, sizeof(struct udphdr));
994
995 return 1;
996}
James Chapmanfd558d12010-04-02 06:18:33 +0000997
998/* UDP encapsulation receive handler. See net/ipv4/udp.c.
999 * Return codes:
1000 * 0 : success.
1001 * <0: error
1002 * >0: skb should be passed up to userspace as UDP.
1003 */
1004int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1005{
1006 struct l2tp_tunnel *tunnel;
1007
1008 tunnel = l2tp_sock_to_tunnel(sk);
1009 if (tunnel == NULL)
1010 goto pass_up;
1011
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001012 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1013 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001014
1015 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1016 goto pass_up_put;
1017
1018 sock_put(sk);
1019 return 0;
1020
1021pass_up_put:
1022 sock_put(sk);
1023pass_up:
1024 return 1;
1025}
1026EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1027
1028/************************************************************************
1029 * Transmit handling
1030 ***********************************************************************/
1031
1032/* Build an L2TP header for the session into the buffer provided.
1033 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001034static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001035{
James Chapmanf7faffa2010-04-02 06:18:49 +00001036 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001037 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001038 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001039 u16 flags = L2TP_HDR_VER_2;
1040 u32 tunnel_id = tunnel->peer_tunnel_id;
1041 u32 session_id = session->peer_session_id;
1042
1043 if (session->send_seq)
1044 flags |= L2TP_HDRFLAG_S;
1045
1046 /* Setup L2TP header. */
1047 *bufp++ = htons(flags);
1048 *bufp++ = htons(tunnel_id);
1049 *bufp++ = htons(session_id);
1050 if (session->send_seq) {
1051 *bufp++ = htons(session->ns);
1052 *bufp++ = 0;
1053 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001054 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001055 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1056 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001057 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001058
1059 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001060}
1061
James Chapmanf7faffa2010-04-02 06:18:49 +00001062static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001063{
James Chapman0d767512010-04-02 06:19:00 +00001064 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001065 char *bufp = buf;
1066 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001067
James Chapman0d767512010-04-02 06:19:00 +00001068 /* Setup L2TP header. The header differs slightly for UDP and
1069 * IP encapsulations. For UDP, there is 4 bytes of flags.
1070 */
1071 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1072 u16 flags = L2TP_HDR_VER_3;
1073 *((__be16 *) bufp) = htons(flags);
1074 bufp += 2;
1075 *((__be16 *) bufp) = 0;
1076 bufp += 2;
1077 }
1078
James Chapmanf7faffa2010-04-02 06:18:49 +00001079 *((__be32 *) bufp) = htonl(session->peer_session_id);
1080 bufp += 4;
1081 if (session->cookie_len) {
1082 memcpy(bufp, &session->cookie[0], session->cookie_len);
1083 bufp += session->cookie_len;
1084 }
1085 if (session->l2specific_len) {
1086 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1087 u32 l2h = 0;
1088 if (session->send_seq) {
1089 l2h = 0x40000000 | session->ns;
1090 session->ns++;
1091 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001092 l2tp_dbg(session, L2TP_MSG_SEQ,
1093 "%s: updated ns to %u\n",
1094 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001095 }
1096
1097 *((__be32 *) bufp) = htonl(l2h);
1098 }
1099 bufp += session->l2specific_len;
1100 }
1101 if (session->offset)
1102 bufp += session->offset;
1103
1104 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001105}
James Chapmanfd558d12010-04-02 06:18:33 +00001106
stephen hemmingerfc130842010-10-21 07:50:46 +00001107static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001108 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001109{
1110 struct l2tp_tunnel *tunnel = session->tunnel;
1111 unsigned int len = skb->len;
1112 int error;
1113
1114 /* Debug */
1115 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001116 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1117 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001118 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001119 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1120 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001121
1122 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001123 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1124 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001125
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001126 pr_debug("%s: xmit\n", session->name);
1127 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1128 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001129 }
1130
1131 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001132 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001133#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001134 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001135 error = inet6_csk_xmit(skb, NULL);
1136 else
1137#endif
1138 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001139
1140 /* Update stats */
1141 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001142 atomic_long_inc(&tunnel->stats.tx_packets);
1143 atomic_long_add(len, &tunnel->stats.tx_bytes);
1144 atomic_long_inc(&session->stats.tx_packets);
1145 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001146 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001147 atomic_long_inc(&tunnel->stats.tx_errors);
1148 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001149 }
1150
1151 return 0;
1152}
James Chapmanfd558d12010-04-02 06:18:33 +00001153
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001154#if IS_ENABLED(CONFIG_IPV6)
1155static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1156 int udp_len)
1157{
1158 struct ipv6_pinfo *np = inet6_sk(sk);
1159 struct udphdr *uh = udp_hdr(skb);
1160
1161 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1162 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1163 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1164 skb->ip_summed = CHECKSUM_UNNECESSARY;
Eric Dumazetefe42082013-10-03 15:42:29 -07001165 uh->check = csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr, udp_len,
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001166 IPPROTO_UDP, csum);
1167 if (uh->check == 0)
1168 uh->check = CSUM_MANGLED_0;
1169 } else {
1170 skb->ip_summed = CHECKSUM_PARTIAL;
1171 skb->csum_start = skb_transport_header(skb) - skb->head;
1172 skb->csum_offset = offsetof(struct udphdr, check);
Eric Dumazetefe42082013-10-03 15:42:29 -07001173 uh->check = ~csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr,
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001174 udp_len, IPPROTO_UDP, 0);
1175 }
1176}
1177#endif
1178
James Chapmanfd558d12010-04-02 06:18:33 +00001179/* If caller requires the skb to have a ppp header, the header must be
1180 * inserted in the skb data before calling this function.
1181 */
1182int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1183{
1184 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001185 struct l2tp_tunnel *tunnel = session->tunnel;
1186 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001187 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001188 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001189 struct inet_sock *inet;
1190 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001191 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001192 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1193 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001194 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001195
1196 /* Check that there's enough headroom in the skb to insert IP,
1197 * UDP and L2TP headers. If not enough, expand it to
1198 * make room. Adjust truesize.
1199 */
1200 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001201 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001202 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001203 kfree_skb(skb);
1204 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001205 }
James Chapmanfd558d12010-04-02 06:18:33 +00001206
James Chapmanfd558d12010-04-02 06:18:33 +00001207 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001208 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001209
James Chapman0d767512010-04-02 06:19:00 +00001210 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001211 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1212 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1213 IPSKB_REROUTED);
1214 nf_reset(skb);
1215
David S. Miller6af88da2011-05-08 13:45:20 -07001216 bh_lock_sock(sk);
1217 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001218 kfree_skb(skb);
1219 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001220 goto out_unlock;
1221 }
1222
James Chapmanfd558d12010-04-02 06:18:33 +00001223 /* Get routing info from the tunnel socket */
1224 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001225 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001226
David S. Millerd9d8da82011-05-06 22:23:20 -07001227 inet = inet_sk(sk);
1228 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001229 switch (tunnel->encap) {
1230 case L2TP_ENCAPTYPE_UDP:
1231 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001232 __skb_push(skb, sizeof(*uh));
1233 skb_reset_transport_header(skb);
1234 uh = udp_hdr(skb);
1235 uh->source = inet->inet_sport;
1236 uh->dest = inet->inet_dport;
1237 udp_len = uhlen + hdr_len + data_len;
1238 uh->len = htons(udp_len);
1239 uh->check = 0;
1240
1241 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001242#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001243 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001244 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1245 else
1246#endif
James Chapman0d767512010-04-02 06:19:00 +00001247 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1248 skb->ip_summed = CHECKSUM_NONE;
1249 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1250 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1251 skb->ip_summed = CHECKSUM_COMPLETE;
1252 csum = skb_checksum(skb, 0, udp_len, 0);
1253 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1254 inet->inet_daddr,
1255 udp_len, IPPROTO_UDP, csum);
1256 if (uh->check == 0)
1257 uh->check = CSUM_MANGLED_0;
1258 } else {
1259 skb->ip_summed = CHECKSUM_PARTIAL;
1260 skb->csum_start = skb_transport_header(skb) - skb->head;
1261 skb->csum_offset = offsetof(struct udphdr, check);
1262 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1263 inet->inet_daddr,
1264 udp_len, IPPROTO_UDP, 0);
1265 }
1266 break;
1267
1268 case L2TP_ENCAPTYPE_IP:
1269 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001270 }
1271
David S. Millerd9d8da82011-05-06 22:23:20 -07001272 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001273out_unlock:
1274 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001275
Eric Dumazetb8c84302012-06-28 20:15:13 +00001276 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001277}
1278EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1279
1280/*****************************************************************************
1281 * Tinnel and session create/destroy.
1282 *****************************************************************************/
1283
1284/* Tunnel socket destruct hook.
1285 * The tunnel context is deleted only when all session sockets have been
1286 * closed.
1287 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001288static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001289{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001290 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001291 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001292
James Chapmanfd558d12010-04-02 06:18:33 +00001293 if (tunnel == NULL)
1294 goto end;
1295
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001296 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001297
James Chapmanfd558d12010-04-02 06:18:33 +00001298
Tom Parkinf8ccac02013-01-31 23:43:00 +00001299 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001300 switch (tunnel->encap) {
1301 case L2TP_ENCAPTYPE_UDP:
1302 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1303 (udp_sk(sk))->encap_type = 0;
1304 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001305 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001306 break;
1307 case L2TP_ENCAPTYPE_IP:
1308 break;
1309 }
James Chapmanfd558d12010-04-02 06:18:33 +00001310
1311 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001312 sk->sk_destruct = tunnel->old_sk_destruct;
1313 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001314 tunnel->sock = NULL;
1315
1316 /* Remove the tunnel struct from the tunnel list */
1317 pn = l2tp_pernet(tunnel->l2tp_net);
1318 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1319 list_del_rcu(&tunnel->list);
1320 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1321 atomic_dec(&l2tp_tunnel_count);
1322
1323 l2tp_tunnel_closeall(tunnel);
1324 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001325
1326 /* Call the original destructor */
1327 if (sk->sk_destruct)
1328 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001329end:
1330 return;
1331}
James Chapmanfd558d12010-04-02 06:18:33 +00001332
1333/* When the tunnel is closed, all the attached sessions need to go too.
1334 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001335void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001336{
1337 int hash;
1338 struct hlist_node *walk;
1339 struct hlist_node *tmp;
1340 struct l2tp_session *session;
1341
1342 BUG_ON(tunnel == NULL);
1343
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001344 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1345 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001346
1347 write_lock_bh(&tunnel->hlist_lock);
1348 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1349again:
1350 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1351 session = hlist_entry(walk, struct l2tp_session, hlist);
1352
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001353 l2tp_info(session, L2TP_MSG_CONTROL,
1354 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001355
1356 hlist_del_init(&session->hlist);
1357
James Chapmanfd558d12010-04-02 06:18:33 +00001358 if (session->ref != NULL)
1359 (*session->ref)(session);
1360
1361 write_unlock_bh(&tunnel->hlist_lock);
1362
Tom Parkinf6e16b22013-03-19 06:11:23 +00001363 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001364 l2tp_session_queue_purge(session);
1365
James Chapmanfd558d12010-04-02 06:18:33 +00001366 if (session->session_close != NULL)
1367 (*session->session_close)(session);
1368
1369 if (session->deref != NULL)
1370 (*session->deref)(session);
1371
Tom Parkin9980d002013-03-19 06:11:13 +00001372 l2tp_session_dec_refcount(session);
1373
James Chapmanfd558d12010-04-02 06:18:33 +00001374 write_lock_bh(&tunnel->hlist_lock);
1375
1376 /* Now restart from the beginning of this hash
1377 * chain. We always remove a session from the
1378 * list so we are guaranteed to make forward
1379 * progress.
1380 */
1381 goto again;
1382 }
1383 }
1384 write_unlock_bh(&tunnel->hlist_lock);
1385}
Tom Parkine34f4c72013-03-19 06:11:14 +00001386EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001387
Tom Parkin9980d002013-03-19 06:11:13 +00001388/* Tunnel socket destroy hook for UDP encapsulation */
1389static void l2tp_udp_encap_destroy(struct sock *sk)
1390{
1391 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1392 if (tunnel) {
1393 l2tp_tunnel_closeall(tunnel);
1394 sock_put(sk);
1395 }
1396}
1397
James Chapmanfd558d12010-04-02 06:18:33 +00001398/* Really kill the tunnel.
1399 * Come here only when all sessions have been cleared from the tunnel.
1400 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001401static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001402{
James Chapmanfd558d12010-04-02 06:18:33 +00001403 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1404 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001405 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001406 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001407}
James Chapmanfd558d12010-04-02 06:18:33 +00001408
Tom Parkinf8ccac02013-01-31 23:43:00 +00001409/* Workqueue tunnel deletion function */
1410static void l2tp_tunnel_del_work(struct work_struct *work)
1411{
1412 struct l2tp_tunnel *tunnel = NULL;
1413 struct socket *sock = NULL;
1414 struct sock *sk = NULL;
1415
1416 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1417 sk = l2tp_tunnel_sock_lookup(tunnel);
1418 if (!sk)
1419 return;
1420
1421 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001422
Tom Parkin02d13ed2013-03-19 06:11:18 +00001423 /* If the tunnel socket was created by userspace, then go through the
1424 * inet layer to shut the socket down, and let userspace close it.
1425 * Otherwise, if we created the socket directly within the kernel, use
1426 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001427 * In either case the tunnel resources are freed in the socket
1428 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001429 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001430 if (tunnel->fd >= 0) {
1431 if (sock)
1432 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001433 } else {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001434 if (sock)
1435 kernel_sock_shutdown(sock, SHUT_RDWR);
1436 sk_release_kernel(sk);
Tom Parkin167eb172013-01-31 23:43:03 +00001437 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001438
1439 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001440}
James Chapmanfd558d12010-04-02 06:18:33 +00001441
James Chapman789a4a22010-04-02 06:19:40 +00001442/* Create a socket for the tunnel, if one isn't set up by
1443 * userspace. This is used for static tunnels where there is no
1444 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001445 *
1446 * Since we don't want these sockets to keep a namespace alive by
1447 * themselves, we drop the socket's namespace refcount after creation.
1448 * These sockets are freed when the namespace exits using the pernet
1449 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001450 */
Tom Parkin167eb172013-01-31 23:43:03 +00001451static int l2tp_tunnel_sock_create(struct net *net,
1452 u32 tunnel_id,
1453 u32 peer_tunnel_id,
1454 struct l2tp_tunnel_cfg *cfg,
1455 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001456{
1457 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001458 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001459 struct sockaddr_in udp_addr = {0};
1460 struct sockaddr_l2tpip ip_addr = {0};
1461#if IS_ENABLED(CONFIG_IPV6)
1462 struct sockaddr_in6 udp6_addr = {0};
1463 struct sockaddr_l2tpip6 ip6_addr = {0};
1464#endif
James Chapman789a4a22010-04-02 06:19:40 +00001465
1466 switch (cfg->encap) {
1467 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001468#if IS_ENABLED(CONFIG_IPV6)
1469 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001470 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001471 if (err < 0)
1472 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001473
Tom Parkin167eb172013-01-31 23:43:03 +00001474 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001475
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001476 udp6_addr.sin6_family = AF_INET6;
1477 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1478 sizeof(udp6_addr.sin6_addr));
1479 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1480 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1481 sizeof(udp6_addr));
1482 if (err < 0)
1483 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001484
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001485 udp6_addr.sin6_family = AF_INET6;
1486 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1487 sizeof(udp6_addr.sin6_addr));
1488 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1489 err = kernel_connect(sock,
1490 (struct sockaddr *) &udp6_addr,
1491 sizeof(udp6_addr), 0);
1492 if (err < 0)
1493 goto out;
1494 } else
1495#endif
1496 {
Tom Parkin167eb172013-01-31 23:43:03 +00001497 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001498 if (err < 0)
1499 goto out;
1500
Tom Parkin167eb172013-01-31 23:43:03 +00001501 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001502
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001503 udp_addr.sin_family = AF_INET;
1504 udp_addr.sin_addr = cfg->local_ip;
1505 udp_addr.sin_port = htons(cfg->local_udp_port);
1506 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1507 sizeof(udp_addr));
1508 if (err < 0)
1509 goto out;
1510
1511 udp_addr.sin_family = AF_INET;
1512 udp_addr.sin_addr = cfg->peer_ip;
1513 udp_addr.sin_port = htons(cfg->peer_udp_port);
1514 err = kernel_connect(sock,
1515 (struct sockaddr *) &udp_addr,
1516 sizeof(udp_addr), 0);
1517 if (err < 0)
1518 goto out;
1519 }
James Chapman789a4a22010-04-02 06:19:40 +00001520
1521 if (!cfg->use_udp_checksums)
1522 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1523
1524 break;
1525
1526 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001527#if IS_ENABLED(CONFIG_IPV6)
1528 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001529 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1530 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001531 if (err < 0)
1532 goto out;
1533
Tom Parkin167eb172013-01-31 23:43:03 +00001534 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001535
James Chapman5dac94e2012-04-29 21:48:55 +00001536 ip6_addr.l2tp_family = AF_INET6;
1537 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1538 sizeof(ip6_addr.l2tp_addr));
1539 ip6_addr.l2tp_conn_id = tunnel_id;
1540 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1541 sizeof(ip6_addr));
1542 if (err < 0)
1543 goto out;
1544
1545 ip6_addr.l2tp_family = AF_INET6;
1546 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1547 sizeof(ip6_addr.l2tp_addr));
1548 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1549 err = kernel_connect(sock,
1550 (struct sockaddr *) &ip6_addr,
1551 sizeof(ip6_addr), 0);
1552 if (err < 0)
1553 goto out;
1554 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001555#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001556 {
Tom Parkin167eb172013-01-31 23:43:03 +00001557 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1558 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001559 if (err < 0)
1560 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001561
Tom Parkin167eb172013-01-31 23:43:03 +00001562 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001563
James Chapman5dac94e2012-04-29 21:48:55 +00001564 ip_addr.l2tp_family = AF_INET;
1565 ip_addr.l2tp_addr = cfg->local_ip;
1566 ip_addr.l2tp_conn_id = tunnel_id;
1567 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1568 sizeof(ip_addr));
1569 if (err < 0)
1570 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001571
James Chapman5dac94e2012-04-29 21:48:55 +00001572 ip_addr.l2tp_family = AF_INET;
1573 ip_addr.l2tp_addr = cfg->peer_ip;
1574 ip_addr.l2tp_conn_id = peer_tunnel_id;
1575 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1576 sizeof(ip_addr), 0);
1577 if (err < 0)
1578 goto out;
1579 }
James Chapman789a4a22010-04-02 06:19:40 +00001580 break;
1581
1582 default:
1583 goto out;
1584 }
1585
1586out:
Tom Parkin167eb172013-01-31 23:43:03 +00001587 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001588 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001589 kernel_sock_shutdown(sock, SHUT_RDWR);
1590 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001591 *sockp = NULL;
1592 }
1593
1594 return err;
1595}
1596
Eric Dumazet37159ef2012-09-04 07:18:57 +00001597static struct lock_class_key l2tp_socket_class;
1598
James Chapmanfd558d12010-04-02 06:18:33 +00001599int 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)
1600{
1601 struct l2tp_tunnel *tunnel = NULL;
1602 int err;
1603 struct socket *sock = NULL;
1604 struct sock *sk = NULL;
1605 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001606 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001607
1608 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001609 * the userspace L2TP daemon. If not specified, create a
1610 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001611 */
James Chapman789a4a22010-04-02 06:19:40 +00001612 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001613 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1614 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001615 if (err < 0)
1616 goto err;
1617 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001618 sock = sockfd_lookup(fd, &err);
1619 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001620 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001621 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001622 err = -EBADF;
1623 goto err;
1624 }
1625
1626 /* Reject namespace mismatches */
1627 if (!net_eq(sock_net(sock->sk), net)) {
1628 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1629 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001630 goto err;
1631 }
James Chapmanfd558d12010-04-02 06:18:33 +00001632 }
1633
1634 sk = sock->sk;
1635
James Chapman0d767512010-04-02 06:19:00 +00001636 if (cfg != NULL)
1637 encap = cfg->encap;
1638
James Chapmanfd558d12010-04-02 06:18:33 +00001639 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001640 switch (encap) {
1641 case L2TP_ENCAPTYPE_UDP:
1642 err = -EPROTONOSUPPORT;
1643 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001644 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001645 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1646 goto err;
1647 }
1648 break;
1649 case L2TP_ENCAPTYPE_IP:
1650 err = -EPROTONOSUPPORT;
1651 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001652 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001653 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1654 goto err;
1655 }
1656 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001657 }
1658
1659 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001660 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001661 if (tunnel != NULL) {
1662 /* This socket has already been prepped */
1663 err = -EBUSY;
1664 goto err;
1665 }
1666
James Chapmanfd558d12010-04-02 06:18:33 +00001667 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1668 if (tunnel == NULL) {
1669 err = -ENOMEM;
1670 goto err;
1671 }
1672
1673 tunnel->version = version;
1674 tunnel->tunnel_id = tunnel_id;
1675 tunnel->peer_tunnel_id = peer_tunnel_id;
1676 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1677
1678 tunnel->magic = L2TP_TUNNEL_MAGIC;
1679 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1680 rwlock_init(&tunnel->hlist_lock);
1681
1682 /* The net we belong to */
1683 tunnel->l2tp_net = net;
1684 pn = l2tp_pernet(net);
1685
James Chapman0d767512010-04-02 06:19:00 +00001686 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001687 tunnel->debug = cfg->debug;
1688
François Cachereule18503f2013-10-02 10:16:02 +02001689#if IS_ENABLED(CONFIG_IPV6)
1690 if (sk->sk_family == PF_INET6) {
1691 struct ipv6_pinfo *np = inet6_sk(sk);
1692
1693 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001694 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001695 struct inet_sock *inet = inet_sk(sk);
1696
1697 tunnel->v4mapped = true;
1698 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001699 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1700 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001701 } else {
1702 tunnel->v4mapped = false;
1703 }
1704 }
1705#endif
1706
James Chapmanfd558d12010-04-02 06:18:33 +00001707 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001708 tunnel->encap = encap;
1709 if (encap == L2TP_ENCAPTYPE_UDP) {
1710 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1711 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1712 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001713 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001714#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001715 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001716 udpv6_encap_enable();
1717 else
1718#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001719 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001720 }
James Chapmanfd558d12010-04-02 06:18:33 +00001721
1722 sk->sk_user_data = tunnel;
1723
1724 /* Hook on the tunnel socket destructor so that we can cleanup
1725 * if the tunnel socket goes away.
1726 */
1727 tunnel->old_sk_destruct = sk->sk_destruct;
1728 sk->sk_destruct = &l2tp_tunnel_destruct;
1729 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001730 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001731 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1732
James Chapmanfd558d12010-04-02 06:18:33 +00001733 sk->sk_allocation = GFP_ATOMIC;
1734
Tom Parkinf8ccac02013-01-31 23:43:00 +00001735 /* Init delete workqueue struct */
1736 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1737
James Chapmanfd558d12010-04-02 06:18:33 +00001738 /* Add tunnel to our list */
1739 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001740 atomic_inc(&l2tp_tunnel_count);
1741
1742 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001743 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001744 */
1745 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001746 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1747 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1748 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001749
1750 err = 0;
1751err:
1752 if (tunnelp)
1753 *tunnelp = tunnel;
1754
James Chapman789a4a22010-04-02 06:19:40 +00001755 /* If tunnel's socket was created by the kernel, it doesn't
1756 * have a file.
1757 */
1758 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001759 sockfd_put(sock);
1760
1761 return err;
1762}
1763EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1764
James Chapman309795f2010-04-02 06:19:10 +00001765/* This function is used by the netlink TUNNEL_DELETE command.
1766 */
1767int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1768{
Tom Parkin2b551c62013-03-19 06:11:16 +00001769 l2tp_tunnel_closeall(tunnel);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001770 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001771}
1772EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1773
James Chapmanfd558d12010-04-02 06:18:33 +00001774/* Really kill the session.
1775 */
1776void l2tp_session_free(struct l2tp_session *session)
1777{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001778 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001779
1780 BUG_ON(atomic_read(&session->ref_count) != 0);
1781
Tom Parkinf6e16b22013-03-19 06:11:23 +00001782 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001783 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001784 if (session->session_id != 0)
1785 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001786 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001787 session->tunnel = NULL;
1788 l2tp_tunnel_dec_refcount(tunnel);
1789 }
1790
1791 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001792}
1793EXPORT_SYMBOL_GPL(l2tp_session_free);
1794
Tom Parkinf6e16b22013-03-19 06:11:23 +00001795/* Remove an l2tp session from l2tp_core's hash lists.
1796 * Provides a tidyup interface for pseudowire code which can't just route all
1797 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1798 * callback.
1799 */
1800void __l2tp_session_unhash(struct l2tp_session *session)
1801{
1802 struct l2tp_tunnel *tunnel = session->tunnel;
1803
1804 /* Remove the session from core hashes */
1805 if (tunnel) {
1806 /* Remove from the per-tunnel hash */
1807 write_lock_bh(&tunnel->hlist_lock);
1808 hlist_del_init(&session->hlist);
1809 write_unlock_bh(&tunnel->hlist_lock);
1810
1811 /* For L2TPv3 we have a per-net hash: remove from there, too */
1812 if (tunnel->version != L2TP_HDR_VER_2) {
1813 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1814 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1815 hlist_del_init_rcu(&session->global_hlist);
1816 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1817 synchronize_rcu();
1818 }
1819 }
1820}
1821EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1822
James Chapman309795f2010-04-02 06:19:10 +00001823/* This function is used by the netlink SESSION_DELETE command and by
1824 pseudowire modules.
1825 */
1826int l2tp_session_delete(struct l2tp_session *session)
1827{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001828 if (session->ref)
1829 (*session->ref)(session);
1830 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001831 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001832 if (session->session_close != NULL)
1833 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001834 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001835 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001836 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001837 return 0;
1838}
1839EXPORT_SYMBOL_GPL(l2tp_session_delete);
1840
James Chapmanf7faffa2010-04-02 06:18:49 +00001841/* We come here whenever a session's send_seq, cookie_len or
1842 * l2specific_len parameters are set.
1843 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001844static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001845{
1846 if (version == L2TP_HDR_VER_2) {
1847 session->hdr_len = 6;
1848 if (session->send_seq)
1849 session->hdr_len += 4;
1850 } else {
James Chapman0d767512010-04-02 06:19:00 +00001851 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1852 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1853 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001854 }
1855
1856}
James Chapmanf7faffa2010-04-02 06:18:49 +00001857
James Chapmanfd558d12010-04-02 06:18:33 +00001858struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1859{
1860 struct l2tp_session *session;
1861
1862 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1863 if (session != NULL) {
1864 session->magic = L2TP_SESSION_MAGIC;
1865 session->tunnel = tunnel;
1866
1867 session->session_id = session_id;
1868 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001869 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001870 if (tunnel->version == L2TP_HDR_VER_2)
1871 session->nr_max = 0xffff;
1872 else
1873 session->nr_max = 0xffffff;
1874 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001875 session->nr_oos_count_max = 4;
1876
1877 /* Use NR of first received packet */
1878 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001879
1880 sprintf(&session->name[0], "sess %u/%u",
1881 tunnel->tunnel_id, session->session_id);
1882
1883 skb_queue_head_init(&session->reorder_q);
1884
1885 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001886 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001887
1888 /* Inherit debug options from tunnel */
1889 session->debug = tunnel->debug;
1890
1891 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001892 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001893 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001894 session->mtu = cfg->mtu;
1895 session->mru = cfg->mru;
1896 session->send_seq = cfg->send_seq;
1897 session->recv_seq = cfg->recv_seq;
1898 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001899 session->reorder_timeout = cfg->reorder_timeout;
1900 session->offset = cfg->offset;
1901 session->l2specific_type = cfg->l2specific_type;
1902 session->l2specific_len = cfg->l2specific_len;
1903 session->cookie_len = cfg->cookie_len;
1904 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1905 session->peer_cookie_len = cfg->peer_cookie_len;
1906 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001907 }
1908
James Chapmanf7faffa2010-04-02 06:18:49 +00001909 if (tunnel->version == L2TP_HDR_VER_2)
1910 session->build_header = l2tp_build_l2tpv2_header;
1911 else
1912 session->build_header = l2tp_build_l2tpv3_header;
1913
1914 l2tp_session_set_header_len(session, tunnel->version);
1915
James Chapmanfd558d12010-04-02 06:18:33 +00001916 /* Bump the reference count. The session context is deleted
1917 * only when this drops to zero.
1918 */
1919 l2tp_session_inc_refcount(session);
1920 l2tp_tunnel_inc_refcount(tunnel);
1921
1922 /* Ensure tunnel socket isn't deleted */
1923 sock_hold(tunnel->sock);
1924
1925 /* Add session to the tunnel's hash list */
1926 write_lock_bh(&tunnel->hlist_lock);
1927 hlist_add_head(&session->hlist,
1928 l2tp_session_id_hash(tunnel, session_id));
1929 write_unlock_bh(&tunnel->hlist_lock);
1930
James Chapmanf7faffa2010-04-02 06:18:49 +00001931 /* And to the global session list if L2TPv3 */
1932 if (tunnel->version != L2TP_HDR_VER_2) {
1933 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1934
James Chapmane02d4942010-04-02 06:19:16 +00001935 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1936 hlist_add_head_rcu(&session->global_hlist,
1937 l2tp_session_id_hash_2(pn, session_id));
1938 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001939 }
1940
James Chapmanfd558d12010-04-02 06:18:33 +00001941 /* Ignore management session in session count value */
1942 if (session->session_id != 0)
1943 atomic_inc(&l2tp_session_count);
1944 }
1945
1946 return session;
1947}
1948EXPORT_SYMBOL_GPL(l2tp_session_create);
1949
1950/*****************************************************************************
1951 * Init and cleanup
1952 *****************************************************************************/
1953
1954static __net_init int l2tp_init_net(struct net *net)
1955{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001956 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001957 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001958
James Chapmanfd558d12010-04-02 06:18:33 +00001959 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001960 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001961
James Chapmanf7faffa2010-04-02 06:18:49 +00001962 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1963 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1964
James Chapmane02d4942010-04-02 06:19:16 +00001965 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001966
James Chapmanfd558d12010-04-02 06:18:33 +00001967 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001968}
1969
Tom Parkin167eb172013-01-31 23:43:03 +00001970static __net_exit void l2tp_exit_net(struct net *net)
1971{
1972 struct l2tp_net *pn = l2tp_pernet(net);
1973 struct l2tp_tunnel *tunnel = NULL;
1974
1975 rcu_read_lock_bh();
1976 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1977 (void)l2tp_tunnel_delete(tunnel);
1978 }
1979 rcu_read_unlock_bh();
1980}
1981
James Chapmanfd558d12010-04-02 06:18:33 +00001982static struct pernet_operations l2tp_net_ops = {
1983 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001984 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001985 .id = &l2tp_net_id,
1986 .size = sizeof(struct l2tp_net),
1987};
1988
1989static int __init l2tp_init(void)
1990{
1991 int rc = 0;
1992
1993 rc = register_pernet_device(&l2tp_net_ops);
1994 if (rc)
1995 goto out;
1996
Tom Parkinf8ccac02013-01-31 23:43:00 +00001997 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1998 if (!l2tp_wq) {
1999 pr_err("alloc_workqueue failed\n");
2000 rc = -ENOMEM;
2001 goto out;
2002 }
2003
Joe Perchesa4ca44f2012-05-16 09:55:56 +00002004 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00002005
2006out:
2007 return rc;
2008}
2009
2010static void __exit l2tp_exit(void)
2011{
2012 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00002013 if (l2tp_wq) {
2014 destroy_workqueue(l2tp_wq);
2015 l2tp_wq = NULL;
2016 }
James Chapmanfd558d12010-04-02 06:18:33 +00002017}
2018
2019module_init(l2tp_init);
2020module_exit(l2tp_exit);
2021
2022MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
2023MODULE_DESCRIPTION("L2TP core");
2024MODULE_LICENSE("GPL");
2025MODULE_VERSION(L2TP_DRV_VERSION);
2026