blob: b6ba65fa3c50477b26ea0100f55c409e340941ed [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);
117static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
118
James Chapmanfd558d12010-04-02 06:18:33 +0000119static inline struct l2tp_net *l2tp_pernet(struct net *net)
120{
121 BUG_ON(!net);
122
123 return net_generic(net, l2tp_net_id);
124}
125
stephen hemmingerfc130842010-10-21 07:50:46 +0000126/* Tunnel reference counts. Incremented per session that is added to
127 * the tunnel.
128 */
129static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
130{
131 atomic_inc(&tunnel->ref_count);
132}
133
134static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
135{
136 if (atomic_dec_and_test(&tunnel->ref_count))
137 l2tp_tunnel_free(tunnel);
138}
139#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000140#define l2tp_tunnel_inc_refcount(_t) \
141do { \
142 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
143 __func__, __LINE__, (_t)->name, \
144 atomic_read(&_t->ref_count)); \
145 l2tp_tunnel_inc_refcount_1(_t); \
146} while (0)
147#define l2tp_tunnel_dec_refcount(_t)
148do { \
149 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
150 __func__, __LINE__, (_t)->name, \
151 atomic_read(&_t->ref_count)); \
152 l2tp_tunnel_dec_refcount_1(_t); \
153} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000154#else
155#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
156#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
157#endif
158
James Chapmanf7faffa2010-04-02 06:18:49 +0000159/* Session hash global list for L2TPv3.
160 * The session_id SHOULD be random according to RFC3931, but several
161 * L2TP implementations use incrementing session_ids. So we do a real
162 * hash on the session_id, rather than a simple bitmask.
163 */
164static inline struct hlist_head *
165l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
166{
167 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
168
169}
170
Tom Parkin80d84ef2013-01-22 05:13:48 +0000171/* Lookup the tunnel socket, possibly involving the fs code if the socket is
172 * owned by userspace. A struct sock returned from this function must be
173 * released using l2tp_tunnel_sock_put once you're done with it.
174 */
175struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
176{
177 int err = 0;
178 struct socket *sock = NULL;
179 struct sock *sk = NULL;
180
181 if (!tunnel)
182 goto out;
183
184 if (tunnel->fd >= 0) {
185 /* Socket is owned by userspace, who might be in the process
186 * of closing it. Look the socket up using the fd to ensure
187 * consistency.
188 */
189 sock = sockfd_lookup(tunnel->fd, &err);
190 if (sock)
191 sk = sock->sk;
192 } else {
193 /* Socket is owned by kernelspace */
194 sk = tunnel->sock;
195 }
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 }
213}
214EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
215
James Chapmanf7faffa2010-04-02 06:18:49 +0000216/* Lookup a session by id in the global session list
217 */
218static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
219{
220 struct l2tp_net *pn = l2tp_pernet(net);
221 struct hlist_head *session_list =
222 l2tp_session_id_hash_2(pn, session_id);
223 struct l2tp_session *session;
224 struct hlist_node *walk;
225
James Chapmane02d4942010-04-02 06:19:16 +0000226 rcu_read_lock_bh();
227 hlist_for_each_entry_rcu(session, walk, 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;
256 struct hlist_node *walk;
257
James Chapmanf7faffa2010-04-02 06:18:49 +0000258 /* In L2TPv3, session_ids are unique over all tunnels and we
259 * sometimes need to look them up before we know the
260 * tunnel.
261 */
262 if (tunnel == NULL)
263 return l2tp_session_find_2(net, session_id);
264
265 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000266 read_lock_bh(&tunnel->hlist_lock);
267 hlist_for_each_entry(session, walk, session_list, hlist) {
268 if (session->session_id == session_id) {
269 read_unlock_bh(&tunnel->hlist_lock);
270 return session;
271 }
272 }
273 read_unlock_bh(&tunnel->hlist_lock);
274
275 return NULL;
276}
277EXPORT_SYMBOL_GPL(l2tp_session_find);
278
279struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
280{
281 int hash;
282 struct hlist_node *walk;
283 struct l2tp_session *session;
284 int count = 0;
285
286 read_lock_bh(&tunnel->hlist_lock);
287 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
288 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
289 if (++count > nth) {
290 read_unlock_bh(&tunnel->hlist_lock);
291 return session;
292 }
293 }
294 }
295
296 read_unlock_bh(&tunnel->hlist_lock);
297
298 return NULL;
299}
300EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
301
James Chapman309795f2010-04-02 06:19:10 +0000302/* Lookup a session by interface name.
303 * This is very inefficient but is only used by management interfaces.
304 */
305struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
306{
307 struct l2tp_net *pn = l2tp_pernet(net);
308 int hash;
309 struct hlist_node *walk;
310 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++) {
James Chapmane02d4942010-04-02 06:19:16 +0000314 hlist_for_each_entry_rcu(session, walk, &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 Chapman5de7aee2012-04-29 21:48:46 +0000380 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000381
382 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000383 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000384 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
385 if (L2TP_SKB_CB(skbp)->ns > ns) {
386 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000387 l2tp_dbg(session, L2TP_MSG_SEQ,
388 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
389 session->name, ns, L2TP_SKB_CB(skbp)->ns,
390 skb_queue_len(&session->reorder_q));
James Chapman5de7aee2012-04-29 21:48:46 +0000391 u64_stats_update_begin(&sstats->syncp);
392 sstats->rx_oos_packets++;
393 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000394 goto out;
395 }
396 }
397
398 __skb_queue_tail(&session->reorder_q, skb);
399
400out:
401 spin_unlock_bh(&session->reorder_q.lock);
402}
403
404/* Dequeue a single skb.
405 */
406static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
407{
408 struct l2tp_tunnel *tunnel = session->tunnel;
409 int length = L2TP_SKB_CB(skb)->length;
James Chapman5de7aee2012-04-29 21:48:46 +0000410 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000411
412 /* We're about to requeue the skb, so return resources
413 * to its current owner (a socket receive buffer).
414 */
415 skb_orphan(skb);
416
James Chapman5de7aee2012-04-29 21:48:46 +0000417 tstats = &tunnel->stats;
418 u64_stats_update_begin(&tstats->syncp);
419 sstats = &session->stats;
420 u64_stats_update_begin(&sstats->syncp);
421 tstats->rx_packets++;
422 tstats->rx_bytes += length;
423 sstats->rx_packets++;
424 sstats->rx_bytes += length;
425 u64_stats_update_end(&tstats->syncp);
426 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000427
428 if (L2TP_SKB_CB(skb)->has_seq) {
429 /* Bump our Nr */
430 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000431 if (tunnel->version == L2TP_HDR_VER_2)
432 session->nr &= 0xffff;
433 else
434 session->nr &= 0xffffff;
435
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000436 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
437 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000438 }
439
440 /* call private receive handler */
441 if (session->recv_skb != NULL)
442 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
443 else
444 kfree_skb(skb);
445
446 if (session->deref)
447 (*session->deref)(session);
448}
449
450/* Dequeue skbs from the session's reorder_q, subject to packet order.
451 * Skbs that have been in the queue for too long are simply discarded.
452 */
453static void l2tp_recv_dequeue(struct l2tp_session *session)
454{
455 struct sk_buff *skb;
456 struct sk_buff *tmp;
James Chapman5de7aee2012-04-29 21:48:46 +0000457 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000458
459 /* If the pkt at the head of the queue has the nr that we
460 * expect to send up next, dequeue it and any other
461 * in-sequence packets behind it.
462 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000463start:
James Chapmanfd558d12010-04-02 06:18:33 +0000464 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000465 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000466 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
467 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
James Chapman5de7aee2012-04-29 21:48:46 +0000468 u64_stats_update_begin(&sstats->syncp);
469 sstats->rx_seq_discards++;
470 sstats->rx_errors++;
471 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000472 l2tp_dbg(session, L2TP_MSG_SEQ,
473 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
474 session->name, L2TP_SKB_CB(skb)->ns,
475 L2TP_SKB_CB(skb)->length, session->nr,
476 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000477 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000478 __skb_unlink(skb, &session->reorder_q);
479 kfree_skb(skb);
480 if (session->deref)
481 (*session->deref)(session);
482 continue;
483 }
484
485 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000486 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000487 l2tp_dbg(session, L2TP_MSG_SEQ,
488 "%s: advancing nr to next pkt: %u -> %u",
489 session->name, session->nr,
490 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000491 session->reorder_skip = 0;
492 session->nr = L2TP_SKB_CB(skb)->ns;
493 }
James Chapmanfd558d12010-04-02 06:18:33 +0000494 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000495 l2tp_dbg(session, L2TP_MSG_SEQ,
496 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
497 session->name, L2TP_SKB_CB(skb)->ns,
498 L2TP_SKB_CB(skb)->length, session->nr,
499 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000500 goto out;
501 }
502 }
503 __skb_unlink(skb, &session->reorder_q);
504
505 /* Process the skb. We release the queue lock while we
506 * do so to let other contexts process the queue.
507 */
508 spin_unlock_bh(&session->reorder_q.lock);
509 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000510 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000511 }
512
513out:
514 spin_unlock_bh(&session->reorder_q.lock);
515}
516
517static inline int l2tp_verify_udp_checksum(struct sock *sk,
518 struct sk_buff *skb)
519{
520 struct udphdr *uh = udp_hdr(skb);
521 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000522 __wsum psum;
523
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000524 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000525 return 0;
526
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000527#if IS_ENABLED(CONFIG_IPV6)
528 if (sk->sk_family == PF_INET6) {
529 if (!uh->check) {
530 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
531 return 1;
532 }
533 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
534 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
535 &ipv6_hdr(skb)->daddr, ulen,
536 IPPROTO_UDP, skb->csum)) {
537 skb->ip_summed = CHECKSUM_UNNECESSARY;
538 return 0;
539 }
540 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
541 &ipv6_hdr(skb)->daddr,
542 skb->len, IPPROTO_UDP,
543 0));
544 } else
545#endif
546 {
547 struct inet_sock *inet;
548 if (!uh->check)
549 return 0;
550 inet = inet_sk(sk);
551 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
552 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000553
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000554 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
555 !csum_fold(csum_add(psum, skb->csum)))
556 return 0;
557 skb->csum = psum;
558 }
James Chapmanfd558d12010-04-02 06:18:33 +0000559
560 return __skb_checksum_complete(skb);
561}
562
James Chapmanf7faffa2010-04-02 06:18:49 +0000563/* Do receive processing of L2TP data frames. We handle both L2TPv2
564 * and L2TPv3 data frames here.
565 *
566 * L2TPv2 Data Message Header
567 *
568 * 0 1 2 3
569 * 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
570 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
571 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
573 * | Tunnel ID | Session ID |
574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
575 * | Ns (opt) | Nr (opt) |
576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577 * | Offset Size (opt) | Offset pad... (opt)
578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
579 *
580 * Data frames are marked by T=0. All other fields are the same as
581 * those in L2TP control frames.
582 *
583 * L2TPv3 Data Message Header
584 *
585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * | L2TP Session Header |
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * | L2-Specific Sublayer |
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * | Tunnel Payload ...
591 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 *
593 * L2TPv3 Session Header Over IP
594 *
595 * 0 1 2 3
596 * 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
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * | Session ID |
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * | Cookie (optional, maximum 64 bits)...
601 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 * |
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 *
605 * L2TPv3 L2-Specific Sublayer Format
606 *
607 * 0 1 2 3
608 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 * |x|S|x|x|x|x|x|x| Sequence Number |
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 *
613 * Cookie value, sublayer format and offset (pad) are negotiated with
614 * the peer when the session is set up. Unlike L2TPv2, we do not need
615 * to parse the packet header to determine if optional fields are
616 * present.
617 *
618 * Caller must already have parsed the frame and determined that it is
619 * a data (not control) frame before coming here. Fields up to the
620 * session-id have already been parsed and ptr points to the data
621 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000622 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000623void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
624 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
625 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000626{
James Chapmanf7faffa2010-04-02 06:18:49 +0000627 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000628 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000629 u32 ns, nr;
James Chapman5de7aee2012-04-29 21:48:46 +0000630 struct l2tp_stats *sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000631
632 /* The ref count is increased since we now hold a pointer to
633 * the session. Take care to decrement the refcnt when exiting
634 * this function from now on...
635 */
636 l2tp_session_inc_refcount(session);
637 if (session->ref)
638 (*session->ref)(session);
639
James Chapmanf7faffa2010-04-02 06:18:49 +0000640 /* Parse and check optional cookie */
641 if (session->peer_cookie_len > 0) {
642 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000643 l2tp_info(tunnel, L2TP_MSG_DATA,
644 "%s: cookie mismatch (%u/%u). Discarding.\n",
645 tunnel->name, tunnel->tunnel_id,
646 session->session_id);
James Chapman5de7aee2012-04-29 21:48:46 +0000647 u64_stats_update_begin(&sstats->syncp);
648 sstats->rx_cookie_discards++;
649 u64_stats_update_end(&sstats->syncp);
James Chapmanf7faffa2010-04-02 06:18:49 +0000650 goto discard;
651 }
652 ptr += session->peer_cookie_len;
653 }
654
James Chapmanfd558d12010-04-02 06:18:33 +0000655 /* Handle the optional sequence numbers. Sequence numbers are
656 * in different places for L2TPv2 and L2TPv3.
657 *
658 * If we are the LAC, enable/disable sequence numbers under
659 * the control of the LNS. If no sequence numbers present but
660 * we were expecting them, discard frame.
661 */
662 ns = nr = 0;
663 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000664 if (tunnel->version == L2TP_HDR_VER_2) {
665 if (hdrflags & L2TP_HDRFLAG_S) {
666 ns = ntohs(*(__be16 *) ptr);
667 ptr += 2;
668 nr = ntohs(*(__be16 *) ptr);
669 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000670
James Chapmanf7faffa2010-04-02 06:18:49 +0000671 /* Store L2TP info in the skb */
672 L2TP_SKB_CB(skb)->ns = ns;
673 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000674
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000675 l2tp_dbg(session, L2TP_MSG_SEQ,
676 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
677 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000678 }
679 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
680 u32 l2h = ntohl(*(__be32 *) ptr);
681
682 if (l2h & 0x40000000) {
683 ns = l2h & 0x00ffffff;
684
685 /* Store L2TP info in the skb */
686 L2TP_SKB_CB(skb)->ns = ns;
687 L2TP_SKB_CB(skb)->has_seq = 1;
688
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000689 l2tp_dbg(session, L2TP_MSG_SEQ,
690 "%s: recv data ns=%u, session nr=%u\n",
691 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000692 }
James Chapmanfd558d12010-04-02 06:18:33 +0000693 }
694
James Chapmanf7faffa2010-04-02 06:18:49 +0000695 /* Advance past L2-specific header, if present */
696 ptr += session->l2specific_len;
697
James Chapmanfd558d12010-04-02 06:18:33 +0000698 if (L2TP_SKB_CB(skb)->has_seq) {
699 /* Received a packet with sequence numbers. If we're the LNS,
700 * check if we sre sending sequence numbers and if not,
701 * configure it so.
702 */
703 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000704 l2tp_info(session, L2TP_MSG_SEQ,
705 "%s: requested to enable seq numbers by LNS\n",
706 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000707 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000708 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000709 }
710 } else {
711 /* No sequence numbers.
712 * If user has configured mandatory sequence numbers, discard.
713 */
714 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000715 l2tp_warn(session, L2TP_MSG_SEQ,
716 "%s: recv data has no seq numbers when required. Discarding.\n",
717 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000718 u64_stats_update_begin(&sstats->syncp);
719 sstats->rx_seq_discards++;
720 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000721 goto discard;
722 }
723
724 /* If we're the LAC and we're sending sequence numbers, the
725 * LNS has requested that we no longer send sequence numbers.
726 * If we're the LNS and we're sending sequence numbers, the
727 * LAC is broken. Discard the frame.
728 */
729 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000730 l2tp_info(session, L2TP_MSG_SEQ,
731 "%s: requested to disable seq numbers by LNS\n",
732 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000733 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000734 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000735 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000736 l2tp_warn(session, L2TP_MSG_SEQ,
737 "%s: recv data has no seq numbers when required. Discarding.\n",
738 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000739 u64_stats_update_begin(&sstats->syncp);
740 sstats->rx_seq_discards++;
741 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000742 goto discard;
743 }
744 }
745
James Chapmanf7faffa2010-04-02 06:18:49 +0000746 /* Session data offset is handled differently for L2TPv2 and
747 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
748 * the header. For L2TPv3, the offset is negotiated using AVPs
749 * in the session setup control protocol.
750 */
751 if (tunnel->version == L2TP_HDR_VER_2) {
752 /* If offset bit set, skip it. */
753 if (hdrflags & L2TP_HDRFLAG_O) {
754 offset = ntohs(*(__be16 *)ptr);
755 ptr += 2 + offset;
756 }
757 } else
758 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000759
760 offset = ptr - optr;
761 if (!pskb_may_pull(skb, offset))
762 goto discard;
763
764 __skb_pull(skb, offset);
765
766 /* If caller wants to process the payload before we queue the
767 * packet, do so now.
768 */
769 if (payload_hook)
770 if ((*payload_hook)(skb))
771 goto discard;
772
773 /* Prepare skb for adding to the session's reorder_q. Hold
774 * packets for max reorder_timeout or 1 second if not
775 * reordering.
776 */
777 L2TP_SKB_CB(skb)->length = length;
778 L2TP_SKB_CB(skb)->expires = jiffies +
779 (session->reorder_timeout ? session->reorder_timeout : HZ);
780
781 /* Add packet to the session's receive queue. Reordering is done here, if
782 * enabled. Saved L2TP protocol info is stored in skb->sb[].
783 */
784 if (L2TP_SKB_CB(skb)->has_seq) {
785 if (session->reorder_timeout != 0) {
786 /* Packet reordering enabled. Add skb to session's
787 * reorder queue, in order of ns.
788 */
789 l2tp_recv_queue_skb(session, skb);
790 } else {
791 /* Packet reordering disabled. Discard out-of-sequence
792 * packets
793 */
794 if (L2TP_SKB_CB(skb)->ns != session->nr) {
James Chapman5de7aee2012-04-29 21:48:46 +0000795 u64_stats_update_begin(&sstats->syncp);
796 sstats->rx_seq_discards++;
797 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000798 l2tp_dbg(session, L2TP_MSG_SEQ,
799 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
800 session->name, L2TP_SKB_CB(skb)->ns,
801 L2TP_SKB_CB(skb)->length, session->nr,
802 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000803 goto discard;
804 }
805 skb_queue_tail(&session->reorder_q, skb);
806 }
807 } else {
808 /* No sequence numbers. Add the skb to the tail of the
809 * reorder queue. This ensures that it will be
810 * delivered after all previous sequenced skbs.
811 */
812 skb_queue_tail(&session->reorder_q, skb);
813 }
814
815 /* Try to dequeue as many skbs from reorder_q as we can. */
816 l2tp_recv_dequeue(session);
817
818 l2tp_session_dec_refcount(session);
819
James Chapmanf7faffa2010-04-02 06:18:49 +0000820 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000821
822discard:
James Chapman5de7aee2012-04-29 21:48:46 +0000823 u64_stats_update_begin(&sstats->syncp);
824 sstats->rx_errors++;
825 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000826 kfree_skb(skb);
827
828 if (session->deref)
829 (*session->deref)(session);
830
831 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000832}
833EXPORT_SYMBOL(l2tp_recv_common);
834
835/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
836 * here. The skb is not on a list when we get here.
837 * Returns 0 if the packet was a data packet and was successfully passed on.
838 * Returns 1 if the packet was not a good data packet and could not be
839 * forwarded. All such packets are passed up to userspace to deal with.
840 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000841static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
842 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000843{
844 struct l2tp_session *session = NULL;
845 unsigned char *ptr, *optr;
846 u16 hdrflags;
847 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000848 u16 version;
849 int length;
James Chapman5de7aee2012-04-29 21:48:46 +0000850 struct l2tp_stats *tstats;
James Chapmanf7faffa2010-04-02 06:18:49 +0000851
852 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
853 goto discard_bad_csum;
854
855 /* UDP always verifies the packet length. */
856 __skb_pull(skb, sizeof(struct udphdr));
857
858 /* Short packet? */
859 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000860 l2tp_info(tunnel, L2TP_MSG_DATA,
861 "%s: recv short packet (len=%d)\n",
862 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000863 goto error;
864 }
865
James Chapmanf7faffa2010-04-02 06:18:49 +0000866 /* Trace packet contents, if enabled */
867 if (tunnel->debug & L2TP_MSG_DATA) {
868 length = min(32u, skb->len);
869 if (!pskb_may_pull(skb, length))
870 goto error;
871
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000872 pr_debug("%s: recv\n", tunnel->name);
873 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000874 }
875
Eric Dumazete50e7052011-11-08 13:59:44 -0500876 /* Point to L2TP header */
877 optr = ptr = skb->data;
878
James Chapmanf7faffa2010-04-02 06:18:49 +0000879 /* Get L2TP header flags */
880 hdrflags = ntohs(*(__be16 *) ptr);
881
882 /* Check protocol version */
883 version = hdrflags & L2TP_HDR_VER_MASK;
884 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000885 l2tp_info(tunnel, L2TP_MSG_DATA,
886 "%s: recv protocol version mismatch: got %d expected %d\n",
887 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000888 goto error;
889 }
890
891 /* Get length of L2TP packet */
892 length = skb->len;
893
894 /* If type is control packet, it is handled by userspace. */
895 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000896 l2tp_dbg(tunnel, L2TP_MSG_DATA,
897 "%s: recv control packet, len=%d\n",
898 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000899 goto error;
900 }
901
902 /* Skip flags */
903 ptr += 2;
904
905 if (tunnel->version == L2TP_HDR_VER_2) {
906 /* If length is present, skip it */
907 if (hdrflags & L2TP_HDRFLAG_L)
908 ptr += 2;
909
910 /* Extract tunnel and session ID */
911 tunnel_id = ntohs(*(__be16 *) ptr);
912 ptr += 2;
913 session_id = ntohs(*(__be16 *) ptr);
914 ptr += 2;
915 } else {
916 ptr += 2; /* skip reserved bits */
917 tunnel_id = tunnel->tunnel_id;
918 session_id = ntohl(*(__be32 *) ptr);
919 ptr += 4;
920 }
921
922 /* Find the session context */
923 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000924 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000925 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000926 l2tp_info(tunnel, L2TP_MSG_DATA,
927 "%s: no session found (%u/%u). Passing up.\n",
928 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000929 goto error;
930 }
931
932 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000933
934 return 0;
935
936discard_bad_csum:
937 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
938 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
James Chapman5de7aee2012-04-29 21:48:46 +0000939 tstats = &tunnel->stats;
940 u64_stats_update_begin(&tstats->syncp);
941 tstats->rx_errors++;
942 u64_stats_update_end(&tstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000943 kfree_skb(skb);
944
945 return 0;
946
947error:
948 /* Put UDP header back */
949 __skb_push(skb, sizeof(struct udphdr));
950
951 return 1;
952}
James Chapmanfd558d12010-04-02 06:18:33 +0000953
954/* UDP encapsulation receive handler. See net/ipv4/udp.c.
955 * Return codes:
956 * 0 : success.
957 * <0: error
958 * >0: skb should be passed up to userspace as UDP.
959 */
960int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
961{
962 struct l2tp_tunnel *tunnel;
963
964 tunnel = l2tp_sock_to_tunnel(sk);
965 if (tunnel == NULL)
966 goto pass_up;
967
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000968 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
969 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000970
971 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
972 goto pass_up_put;
973
974 sock_put(sk);
975 return 0;
976
977pass_up_put:
978 sock_put(sk);
979pass_up:
980 return 1;
981}
982EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
983
984/************************************************************************
985 * Transmit handling
986 ***********************************************************************/
987
988/* Build an L2TP header for the session into the buffer provided.
989 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000990static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000991{
James Chapmanf7faffa2010-04-02 06:18:49 +0000992 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000993 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000994 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000995 u16 flags = L2TP_HDR_VER_2;
996 u32 tunnel_id = tunnel->peer_tunnel_id;
997 u32 session_id = session->peer_session_id;
998
999 if (session->send_seq)
1000 flags |= L2TP_HDRFLAG_S;
1001
1002 /* Setup L2TP header. */
1003 *bufp++ = htons(flags);
1004 *bufp++ = htons(tunnel_id);
1005 *bufp++ = htons(session_id);
1006 if (session->send_seq) {
1007 *bufp++ = htons(session->ns);
1008 *bufp++ = 0;
1009 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001010 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001011 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1012 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001013 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001014
1015 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001016}
1017
James Chapmanf7faffa2010-04-02 06:18:49 +00001018static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001019{
James Chapman0d767512010-04-02 06:19:00 +00001020 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001021 char *bufp = buf;
1022 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001023
James Chapman0d767512010-04-02 06:19:00 +00001024 /* Setup L2TP header. The header differs slightly for UDP and
1025 * IP encapsulations. For UDP, there is 4 bytes of flags.
1026 */
1027 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1028 u16 flags = L2TP_HDR_VER_3;
1029 *((__be16 *) bufp) = htons(flags);
1030 bufp += 2;
1031 *((__be16 *) bufp) = 0;
1032 bufp += 2;
1033 }
1034
James Chapmanf7faffa2010-04-02 06:18:49 +00001035 *((__be32 *) bufp) = htonl(session->peer_session_id);
1036 bufp += 4;
1037 if (session->cookie_len) {
1038 memcpy(bufp, &session->cookie[0], session->cookie_len);
1039 bufp += session->cookie_len;
1040 }
1041 if (session->l2specific_len) {
1042 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1043 u32 l2h = 0;
1044 if (session->send_seq) {
1045 l2h = 0x40000000 | session->ns;
1046 session->ns++;
1047 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001048 l2tp_dbg(session, L2TP_MSG_SEQ,
1049 "%s: updated ns to %u\n",
1050 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001051 }
1052
1053 *((__be32 *) bufp) = htonl(l2h);
1054 }
1055 bufp += session->l2specific_len;
1056 }
1057 if (session->offset)
1058 bufp += session->offset;
1059
1060 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001061}
James Chapmanfd558d12010-04-02 06:18:33 +00001062
stephen hemmingerfc130842010-10-21 07:50:46 +00001063static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001064 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001065{
1066 struct l2tp_tunnel *tunnel = session->tunnel;
1067 unsigned int len = skb->len;
1068 int error;
James Chapman5de7aee2012-04-29 21:48:46 +00001069 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +00001070
1071 /* Debug */
1072 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001073 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1074 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001075 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001076 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1077 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001078
1079 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001080 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1081 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001082
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001083 pr_debug("%s: xmit\n", session->name);
1084 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1085 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001086 }
1087
1088 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001089 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001090#if IS_ENABLED(CONFIG_IPV6)
1091 if (skb->sk->sk_family == PF_INET6)
1092 error = inet6_csk_xmit(skb, NULL);
1093 else
1094#endif
1095 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001096
1097 /* Update stats */
James Chapman5de7aee2012-04-29 21:48:46 +00001098 tstats = &tunnel->stats;
1099 u64_stats_update_begin(&tstats->syncp);
1100 sstats = &session->stats;
1101 u64_stats_update_begin(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001102 if (error >= 0) {
James Chapman5de7aee2012-04-29 21:48:46 +00001103 tstats->tx_packets++;
1104 tstats->tx_bytes += len;
1105 sstats->tx_packets++;
1106 sstats->tx_bytes += len;
James Chapmanfd558d12010-04-02 06:18:33 +00001107 } else {
James Chapman5de7aee2012-04-29 21:48:46 +00001108 tstats->tx_errors++;
1109 sstats->tx_errors++;
James Chapmanfd558d12010-04-02 06:18:33 +00001110 }
James Chapman5de7aee2012-04-29 21:48:46 +00001111 u64_stats_update_end(&tstats->syncp);
1112 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001113
1114 return 0;
1115}
James Chapmanfd558d12010-04-02 06:18:33 +00001116
1117/* Automatically called when the skb is freed.
1118 */
1119static void l2tp_sock_wfree(struct sk_buff *skb)
1120{
1121 sock_put(skb->sk);
1122}
1123
1124/* For data skbs that we transmit, we associate with the tunnel socket
1125 * but don't do accounting.
1126 */
1127static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1128{
1129 sock_hold(sk);
1130 skb->sk = sk;
1131 skb->destructor = l2tp_sock_wfree;
1132}
1133
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001134#if IS_ENABLED(CONFIG_IPV6)
1135static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1136 int udp_len)
1137{
1138 struct ipv6_pinfo *np = inet6_sk(sk);
1139 struct udphdr *uh = udp_hdr(skb);
1140
1141 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1142 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1143 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1144 skb->ip_summed = CHECKSUM_UNNECESSARY;
1145 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1146 IPPROTO_UDP, csum);
1147 if (uh->check == 0)
1148 uh->check = CSUM_MANGLED_0;
1149 } else {
1150 skb->ip_summed = CHECKSUM_PARTIAL;
1151 skb->csum_start = skb_transport_header(skb) - skb->head;
1152 skb->csum_offset = offsetof(struct udphdr, check);
1153 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1154 udp_len, IPPROTO_UDP, 0);
1155 }
1156}
1157#endif
1158
James Chapmanfd558d12010-04-02 06:18:33 +00001159/* If caller requires the skb to have a ppp header, the header must be
1160 * inserted in the skb data before calling this function.
1161 */
1162int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1163{
1164 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001165 struct l2tp_tunnel *tunnel = session->tunnel;
1166 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001167 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001168 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001169 struct inet_sock *inet;
1170 __wsum csum;
1171 int old_headroom;
1172 int new_headroom;
1173 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001174 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1175 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001176 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001177
1178 /* Check that there's enough headroom in the skb to insert IP,
1179 * UDP and L2TP headers. If not enough, expand it to
1180 * make room. Adjust truesize.
1181 */
1182 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001183 uhlen + hdr_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001184 old_headroom = skb_headroom(skb);
Eric Dumazet835acf52011-10-07 05:35:46 +00001185 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001186 kfree_skb(skb);
1187 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001188 }
James Chapmanfd558d12010-04-02 06:18:33 +00001189
1190 new_headroom = skb_headroom(skb);
1191 skb_orphan(skb);
1192 skb->truesize += new_headroom - old_headroom;
1193
1194 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001195 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001196
James Chapman0d767512010-04-02 06:19:00 +00001197 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001198 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1199 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1200 IPSKB_REROUTED);
1201 nf_reset(skb);
1202
David S. Miller6af88da2011-05-08 13:45:20 -07001203 bh_lock_sock(sk);
1204 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001205 kfree_skb(skb);
1206 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001207 goto out_unlock;
1208 }
1209
James Chapmanfd558d12010-04-02 06:18:33 +00001210 /* Get routing info from the tunnel socket */
1211 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001212 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001213
David S. Millerd9d8da82011-05-06 22:23:20 -07001214 inet = inet_sk(sk);
1215 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001216 switch (tunnel->encap) {
1217 case L2TP_ENCAPTYPE_UDP:
1218 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001219 __skb_push(skb, sizeof(*uh));
1220 skb_reset_transport_header(skb);
1221 uh = udp_hdr(skb);
1222 uh->source = inet->inet_sport;
1223 uh->dest = inet->inet_dport;
1224 udp_len = uhlen + hdr_len + data_len;
1225 uh->len = htons(udp_len);
1226 uh->check = 0;
1227
1228 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001229#if IS_ENABLED(CONFIG_IPV6)
1230 if (sk->sk_family == PF_INET6)
1231 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1232 else
1233#endif
James Chapman0d767512010-04-02 06:19:00 +00001234 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1235 skb->ip_summed = CHECKSUM_NONE;
1236 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1237 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1238 skb->ip_summed = CHECKSUM_COMPLETE;
1239 csum = skb_checksum(skb, 0, udp_len, 0);
1240 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1241 inet->inet_daddr,
1242 udp_len, IPPROTO_UDP, csum);
1243 if (uh->check == 0)
1244 uh->check = CSUM_MANGLED_0;
1245 } else {
1246 skb->ip_summed = CHECKSUM_PARTIAL;
1247 skb->csum_start = skb_transport_header(skb) - skb->head;
1248 skb->csum_offset = offsetof(struct udphdr, check);
1249 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1250 inet->inet_daddr,
1251 udp_len, IPPROTO_UDP, 0);
1252 }
1253 break;
1254
1255 case L2TP_ENCAPTYPE_IP:
1256 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001257 }
1258
James Chapman0d767512010-04-02 06:19:00 +00001259 l2tp_skb_set_owner_w(skb, sk);
1260
David S. Millerd9d8da82011-05-06 22:23:20 -07001261 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001262out_unlock:
1263 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001264
Eric Dumazetb8c84302012-06-28 20:15:13 +00001265 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001266}
1267EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1268
1269/*****************************************************************************
1270 * Tinnel and session create/destroy.
1271 *****************************************************************************/
1272
1273/* Tunnel socket destruct hook.
1274 * The tunnel context is deleted only when all session sockets have been
1275 * closed.
1276 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001277static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001278{
1279 struct l2tp_tunnel *tunnel;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001280 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001281
1282 tunnel = sk->sk_user_data;
1283 if (tunnel == NULL)
1284 goto end;
1285
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001286 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001287
James Chapmanfd558d12010-04-02 06:18:33 +00001288
Tom Parkinf8ccac02013-01-31 23:43:00 +00001289 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001290 switch (tunnel->encap) {
1291 case L2TP_ENCAPTYPE_UDP:
1292 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1293 (udp_sk(sk))->encap_type = 0;
1294 (udp_sk(sk))->encap_rcv = NULL;
1295 break;
1296 case L2TP_ENCAPTYPE_IP:
1297 break;
1298 }
James Chapmanfd558d12010-04-02 06:18:33 +00001299
1300 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001301 sk->sk_destruct = tunnel->old_sk_destruct;
1302 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001303 tunnel->sock = NULL;
1304
1305 /* Remove the tunnel struct from the tunnel list */
1306 pn = l2tp_pernet(tunnel->l2tp_net);
1307 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1308 list_del_rcu(&tunnel->list);
1309 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1310 atomic_dec(&l2tp_tunnel_count);
1311
1312 l2tp_tunnel_closeall(tunnel);
1313 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001314
1315 /* Call the original destructor */
1316 if (sk->sk_destruct)
1317 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001318end:
1319 return;
1320}
James Chapmanfd558d12010-04-02 06:18:33 +00001321
1322/* When the tunnel is closed, all the attached sessions need to go too.
1323 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001324static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001325{
1326 int hash;
1327 struct hlist_node *walk;
1328 struct hlist_node *tmp;
1329 struct l2tp_session *session;
1330
1331 BUG_ON(tunnel == NULL);
1332
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001333 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1334 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001335
1336 write_lock_bh(&tunnel->hlist_lock);
1337 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1338again:
1339 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1340 session = hlist_entry(walk, struct l2tp_session, hlist);
1341
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001342 l2tp_info(session, L2TP_MSG_CONTROL,
1343 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001344
1345 hlist_del_init(&session->hlist);
1346
1347 /* Since we should hold the sock lock while
1348 * doing any unbinding, we need to release the
1349 * lock we're holding before taking that lock.
1350 * Hold a reference to the sock so it doesn't
1351 * disappear as we're jumping between locks.
1352 */
1353 if (session->ref != NULL)
1354 (*session->ref)(session);
1355
1356 write_unlock_bh(&tunnel->hlist_lock);
1357
James Chapmanf7faffa2010-04-02 06:18:49 +00001358 if (tunnel->version != L2TP_HDR_VER_2) {
1359 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1360
James Chapmane02d4942010-04-02 06:19:16 +00001361 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1362 hlist_del_init_rcu(&session->global_hlist);
1363 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1364 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001365 }
1366
James Chapmanfd558d12010-04-02 06:18:33 +00001367 if (session->session_close != NULL)
1368 (*session->session_close)(session);
1369
1370 if (session->deref != NULL)
1371 (*session->deref)(session);
1372
1373 write_lock_bh(&tunnel->hlist_lock);
1374
1375 /* Now restart from the beginning of this hash
1376 * chain. We always remove a session from the
1377 * list so we are guaranteed to make forward
1378 * progress.
1379 */
1380 goto again;
1381 }
1382 }
1383 write_unlock_bh(&tunnel->hlist_lock);
1384}
James Chapmanfd558d12010-04-02 06:18:33 +00001385
1386/* Really kill the tunnel.
1387 * Come here only when all sessions have been cleared from the tunnel.
1388 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001389static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001390{
James Chapmanfd558d12010-04-02 06:18:33 +00001391 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1392 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001393 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001394 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001395}
James Chapmanfd558d12010-04-02 06:18:33 +00001396
Tom Parkinf8ccac02013-01-31 23:43:00 +00001397/* Workqueue tunnel deletion function */
1398static void l2tp_tunnel_del_work(struct work_struct *work)
1399{
1400 struct l2tp_tunnel *tunnel = NULL;
1401 struct socket *sock = NULL;
1402 struct sock *sk = NULL;
1403
1404 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1405 sk = l2tp_tunnel_sock_lookup(tunnel);
1406 if (!sk)
1407 return;
1408
1409 sock = sk->sk_socket;
1410 BUG_ON(!sock);
1411
Tom Parkin167eb172013-01-31 23:43:03 +00001412 /* If the tunnel socket was created directly by the kernel, use the
1413 * sk_* API to release the socket now. Otherwise go through the
1414 * inet_* layer to shut the socket down, and let userspace close it.
1415 * In either case the tunnel resources are freed in the socket
1416 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001417 */
Tom Parkin167eb172013-01-31 23:43:03 +00001418 if (sock->file == NULL) {
1419 kernel_sock_shutdown(sock, SHUT_RDWR);
1420 sk_release_kernel(sk);
1421 } else {
1422 inet_shutdown(sock, 2);
1423 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001424
1425 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001426}
James Chapmanfd558d12010-04-02 06:18:33 +00001427
James Chapman789a4a22010-04-02 06:19:40 +00001428/* Create a socket for the tunnel, if one isn't set up by
1429 * userspace. This is used for static tunnels where there is no
1430 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001431 *
1432 * Since we don't want these sockets to keep a namespace alive by
1433 * themselves, we drop the socket's namespace refcount after creation.
1434 * These sockets are freed when the namespace exits using the pernet
1435 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001436 */
Tom Parkin167eb172013-01-31 23:43:03 +00001437static int l2tp_tunnel_sock_create(struct net *net,
1438 u32 tunnel_id,
1439 u32 peer_tunnel_id,
1440 struct l2tp_tunnel_cfg *cfg,
1441 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001442{
1443 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001444 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001445 struct sockaddr_in udp_addr = {0};
1446 struct sockaddr_l2tpip ip_addr = {0};
1447#if IS_ENABLED(CONFIG_IPV6)
1448 struct sockaddr_in6 udp6_addr = {0};
1449 struct sockaddr_l2tpip6 ip6_addr = {0};
1450#endif
James Chapman789a4a22010-04-02 06:19:40 +00001451
1452 switch (cfg->encap) {
1453 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001454#if IS_ENABLED(CONFIG_IPV6)
1455 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001456 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001457 if (err < 0)
1458 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001459
Tom Parkin167eb172013-01-31 23:43:03 +00001460 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001461
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001462 udp6_addr.sin6_family = AF_INET6;
1463 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1464 sizeof(udp6_addr.sin6_addr));
1465 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1466 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1467 sizeof(udp6_addr));
1468 if (err < 0)
1469 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001470
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001471 udp6_addr.sin6_family = AF_INET6;
1472 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1473 sizeof(udp6_addr.sin6_addr));
1474 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1475 err = kernel_connect(sock,
1476 (struct sockaddr *) &udp6_addr,
1477 sizeof(udp6_addr), 0);
1478 if (err < 0)
1479 goto out;
1480 } else
1481#endif
1482 {
Tom Parkin167eb172013-01-31 23:43:03 +00001483 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001484 if (err < 0)
1485 goto out;
1486
Tom Parkin167eb172013-01-31 23:43:03 +00001487 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001488
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001489 udp_addr.sin_family = AF_INET;
1490 udp_addr.sin_addr = cfg->local_ip;
1491 udp_addr.sin_port = htons(cfg->local_udp_port);
1492 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1493 sizeof(udp_addr));
1494 if (err < 0)
1495 goto out;
1496
1497 udp_addr.sin_family = AF_INET;
1498 udp_addr.sin_addr = cfg->peer_ip;
1499 udp_addr.sin_port = htons(cfg->peer_udp_port);
1500 err = kernel_connect(sock,
1501 (struct sockaddr *) &udp_addr,
1502 sizeof(udp_addr), 0);
1503 if (err < 0)
1504 goto out;
1505 }
James Chapman789a4a22010-04-02 06:19:40 +00001506
1507 if (!cfg->use_udp_checksums)
1508 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1509
1510 break;
1511
1512 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001513#if IS_ENABLED(CONFIG_IPV6)
1514 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001515 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1516 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001517 if (err < 0)
1518 goto out;
1519
Tom Parkin167eb172013-01-31 23:43:03 +00001520 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001521
James Chapman5dac94e2012-04-29 21:48:55 +00001522 ip6_addr.l2tp_family = AF_INET6;
1523 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1524 sizeof(ip6_addr.l2tp_addr));
1525 ip6_addr.l2tp_conn_id = tunnel_id;
1526 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1527 sizeof(ip6_addr));
1528 if (err < 0)
1529 goto out;
1530
1531 ip6_addr.l2tp_family = AF_INET6;
1532 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1533 sizeof(ip6_addr.l2tp_addr));
1534 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1535 err = kernel_connect(sock,
1536 (struct sockaddr *) &ip6_addr,
1537 sizeof(ip6_addr), 0);
1538 if (err < 0)
1539 goto out;
1540 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001541#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001542 {
Tom Parkin167eb172013-01-31 23:43:03 +00001543 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1544 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001545 if (err < 0)
1546 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001547
Tom Parkin167eb172013-01-31 23:43:03 +00001548 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001549
James Chapman5dac94e2012-04-29 21:48:55 +00001550 ip_addr.l2tp_family = AF_INET;
1551 ip_addr.l2tp_addr = cfg->local_ip;
1552 ip_addr.l2tp_conn_id = tunnel_id;
1553 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1554 sizeof(ip_addr));
1555 if (err < 0)
1556 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001557
James Chapman5dac94e2012-04-29 21:48:55 +00001558 ip_addr.l2tp_family = AF_INET;
1559 ip_addr.l2tp_addr = cfg->peer_ip;
1560 ip_addr.l2tp_conn_id = peer_tunnel_id;
1561 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1562 sizeof(ip_addr), 0);
1563 if (err < 0)
1564 goto out;
1565 }
James Chapman789a4a22010-04-02 06:19:40 +00001566 break;
1567
1568 default:
1569 goto out;
1570 }
1571
1572out:
Tom Parkin167eb172013-01-31 23:43:03 +00001573 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001574 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001575 kernel_sock_shutdown(sock, SHUT_RDWR);
1576 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001577 *sockp = NULL;
1578 }
1579
1580 return err;
1581}
1582
Eric Dumazet37159ef2012-09-04 07:18:57 +00001583static struct lock_class_key l2tp_socket_class;
1584
James Chapmanfd558d12010-04-02 06:18:33 +00001585int 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)
1586{
1587 struct l2tp_tunnel *tunnel = NULL;
1588 int err;
1589 struct socket *sock = NULL;
1590 struct sock *sk = NULL;
1591 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001592 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001593
1594 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001595 * the userspace L2TP daemon. If not specified, create a
1596 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001597 */
James Chapman789a4a22010-04-02 06:19:40 +00001598 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001599 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1600 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001601 if (err < 0)
1602 goto err;
1603 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001604 sock = sockfd_lookup(fd, &err);
1605 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001606 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001607 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001608 err = -EBADF;
1609 goto err;
1610 }
1611
1612 /* Reject namespace mismatches */
1613 if (!net_eq(sock_net(sock->sk), net)) {
1614 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1615 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001616 goto err;
1617 }
James Chapmanfd558d12010-04-02 06:18:33 +00001618 }
1619
1620 sk = sock->sk;
1621
James Chapman0d767512010-04-02 06:19:00 +00001622 if (cfg != NULL)
1623 encap = cfg->encap;
1624
James Chapmanfd558d12010-04-02 06:18:33 +00001625 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001626 switch (encap) {
1627 case L2TP_ENCAPTYPE_UDP:
1628 err = -EPROTONOSUPPORT;
1629 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001630 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001631 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1632 goto err;
1633 }
1634 break;
1635 case L2TP_ENCAPTYPE_IP:
1636 err = -EPROTONOSUPPORT;
1637 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001638 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001639 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1640 goto err;
1641 }
1642 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001643 }
1644
1645 /* Check if this socket has already been prepped */
1646 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1647 if (tunnel != NULL) {
1648 /* This socket has already been prepped */
1649 err = -EBUSY;
1650 goto err;
1651 }
1652
James Chapmanfd558d12010-04-02 06:18:33 +00001653 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1654 if (tunnel == NULL) {
1655 err = -ENOMEM;
1656 goto err;
1657 }
1658
1659 tunnel->version = version;
1660 tunnel->tunnel_id = tunnel_id;
1661 tunnel->peer_tunnel_id = peer_tunnel_id;
1662 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1663
1664 tunnel->magic = L2TP_TUNNEL_MAGIC;
1665 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1666 rwlock_init(&tunnel->hlist_lock);
1667
1668 /* The net we belong to */
1669 tunnel->l2tp_net = net;
1670 pn = l2tp_pernet(net);
1671
James Chapman0d767512010-04-02 06:19:00 +00001672 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001673 tunnel->debug = cfg->debug;
1674
1675 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001676 tunnel->encap = encap;
1677 if (encap == L2TP_ENCAPTYPE_UDP) {
1678 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1679 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1680 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001681#if IS_ENABLED(CONFIG_IPV6)
1682 if (sk->sk_family == PF_INET6)
1683 udpv6_encap_enable();
1684 else
1685#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001686 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001687 }
James Chapmanfd558d12010-04-02 06:18:33 +00001688
1689 sk->sk_user_data = tunnel;
1690
1691 /* Hook on the tunnel socket destructor so that we can cleanup
1692 * if the tunnel socket goes away.
1693 */
1694 tunnel->old_sk_destruct = sk->sk_destruct;
1695 sk->sk_destruct = &l2tp_tunnel_destruct;
1696 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001697 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001698 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1699
James Chapmanfd558d12010-04-02 06:18:33 +00001700 sk->sk_allocation = GFP_ATOMIC;
1701
Tom Parkinf8ccac02013-01-31 23:43:00 +00001702 /* Init delete workqueue struct */
1703 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1704
James Chapmanfd558d12010-04-02 06:18:33 +00001705 /* Add tunnel to our list */
1706 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001707 atomic_inc(&l2tp_tunnel_count);
1708
1709 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001710 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001711 */
1712 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001713 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1714 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1715 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001716
1717 err = 0;
1718err:
1719 if (tunnelp)
1720 *tunnelp = tunnel;
1721
James Chapman789a4a22010-04-02 06:19:40 +00001722 /* If tunnel's socket was created by the kernel, it doesn't
1723 * have a file.
1724 */
1725 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001726 sockfd_put(sock);
1727
1728 return err;
1729}
1730EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1731
James Chapman309795f2010-04-02 06:19:10 +00001732/* This function is used by the netlink TUNNEL_DELETE command.
1733 */
1734int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1735{
Tom Parkinf8ccac02013-01-31 23:43:00 +00001736 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001737}
1738EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1739
James Chapmanfd558d12010-04-02 06:18:33 +00001740/* Really kill the session.
1741 */
1742void l2tp_session_free(struct l2tp_session *session)
1743{
1744 struct l2tp_tunnel *tunnel;
1745
1746 BUG_ON(atomic_read(&session->ref_count) != 0);
1747
1748 tunnel = session->tunnel;
1749 if (tunnel != NULL) {
1750 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1751
1752 /* Delete the session from the hash */
1753 write_lock_bh(&tunnel->hlist_lock);
1754 hlist_del_init(&session->hlist);
1755 write_unlock_bh(&tunnel->hlist_lock);
1756
James Chapmanf7faffa2010-04-02 06:18:49 +00001757 /* Unlink from the global hash if not L2TPv2 */
1758 if (tunnel->version != L2TP_HDR_VER_2) {
1759 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1760
James Chapmane02d4942010-04-02 06:19:16 +00001761 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1762 hlist_del_init_rcu(&session->global_hlist);
1763 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1764 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001765 }
1766
James Chapmanfd558d12010-04-02 06:18:33 +00001767 if (session->session_id != 0)
1768 atomic_dec(&l2tp_session_count);
1769
1770 sock_put(tunnel->sock);
1771
1772 /* This will delete the tunnel context if this
1773 * is the last session on the tunnel.
1774 */
1775 session->tunnel = NULL;
1776 l2tp_tunnel_dec_refcount(tunnel);
1777 }
1778
1779 kfree(session);
1780
1781 return;
1782}
1783EXPORT_SYMBOL_GPL(l2tp_session_free);
1784
James Chapman309795f2010-04-02 06:19:10 +00001785/* This function is used by the netlink SESSION_DELETE command and by
1786 pseudowire modules.
1787 */
1788int l2tp_session_delete(struct l2tp_session *session)
1789{
1790 if (session->session_close != NULL)
1791 (*session->session_close)(session);
1792
1793 l2tp_session_dec_refcount(session);
1794
1795 return 0;
1796}
1797EXPORT_SYMBOL_GPL(l2tp_session_delete);
1798
1799
James Chapmanf7faffa2010-04-02 06:18:49 +00001800/* We come here whenever a session's send_seq, cookie_len or
1801 * l2specific_len parameters are set.
1802 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001803static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001804{
1805 if (version == L2TP_HDR_VER_2) {
1806 session->hdr_len = 6;
1807 if (session->send_seq)
1808 session->hdr_len += 4;
1809 } else {
James Chapman0d767512010-04-02 06:19:00 +00001810 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1811 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1812 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001813 }
1814
1815}
James Chapmanf7faffa2010-04-02 06:18:49 +00001816
James Chapmanfd558d12010-04-02 06:18:33 +00001817struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1818{
1819 struct l2tp_session *session;
1820
1821 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1822 if (session != NULL) {
1823 session->magic = L2TP_SESSION_MAGIC;
1824 session->tunnel = tunnel;
1825
1826 session->session_id = session_id;
1827 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001828 session->nr = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001829
1830 sprintf(&session->name[0], "sess %u/%u",
1831 tunnel->tunnel_id, session->session_id);
1832
1833 skb_queue_head_init(&session->reorder_q);
1834
1835 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001836 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001837
1838 /* Inherit debug options from tunnel */
1839 session->debug = tunnel->debug;
1840
1841 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001842 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001843 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001844 session->mtu = cfg->mtu;
1845 session->mru = cfg->mru;
1846 session->send_seq = cfg->send_seq;
1847 session->recv_seq = cfg->recv_seq;
1848 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001849 session->reorder_timeout = cfg->reorder_timeout;
1850 session->offset = cfg->offset;
1851 session->l2specific_type = cfg->l2specific_type;
1852 session->l2specific_len = cfg->l2specific_len;
1853 session->cookie_len = cfg->cookie_len;
1854 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1855 session->peer_cookie_len = cfg->peer_cookie_len;
1856 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001857 }
1858
James Chapmanf7faffa2010-04-02 06:18:49 +00001859 if (tunnel->version == L2TP_HDR_VER_2)
1860 session->build_header = l2tp_build_l2tpv2_header;
1861 else
1862 session->build_header = l2tp_build_l2tpv3_header;
1863
1864 l2tp_session_set_header_len(session, tunnel->version);
1865
James Chapmanfd558d12010-04-02 06:18:33 +00001866 /* Bump the reference count. The session context is deleted
1867 * only when this drops to zero.
1868 */
1869 l2tp_session_inc_refcount(session);
1870 l2tp_tunnel_inc_refcount(tunnel);
1871
1872 /* Ensure tunnel socket isn't deleted */
1873 sock_hold(tunnel->sock);
1874
1875 /* Add session to the tunnel's hash list */
1876 write_lock_bh(&tunnel->hlist_lock);
1877 hlist_add_head(&session->hlist,
1878 l2tp_session_id_hash(tunnel, session_id));
1879 write_unlock_bh(&tunnel->hlist_lock);
1880
James Chapmanf7faffa2010-04-02 06:18:49 +00001881 /* And to the global session list if L2TPv3 */
1882 if (tunnel->version != L2TP_HDR_VER_2) {
1883 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1884
James Chapmane02d4942010-04-02 06:19:16 +00001885 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1886 hlist_add_head_rcu(&session->global_hlist,
1887 l2tp_session_id_hash_2(pn, session_id));
1888 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001889 }
1890
James Chapmanfd558d12010-04-02 06:18:33 +00001891 /* Ignore management session in session count value */
1892 if (session->session_id != 0)
1893 atomic_inc(&l2tp_session_count);
1894 }
1895
1896 return session;
1897}
1898EXPORT_SYMBOL_GPL(l2tp_session_create);
1899
1900/*****************************************************************************
1901 * Init and cleanup
1902 *****************************************************************************/
1903
1904static __net_init int l2tp_init_net(struct net *net)
1905{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001906 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001907 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001908
James Chapmanfd558d12010-04-02 06:18:33 +00001909 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001910 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001911
James Chapmanf7faffa2010-04-02 06:18:49 +00001912 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1913 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1914
James Chapmane02d4942010-04-02 06:19:16 +00001915 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001916
James Chapmanfd558d12010-04-02 06:18:33 +00001917 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001918}
1919
Tom Parkin167eb172013-01-31 23:43:03 +00001920static __net_exit void l2tp_exit_net(struct net *net)
1921{
1922 struct l2tp_net *pn = l2tp_pernet(net);
1923 struct l2tp_tunnel *tunnel = NULL;
1924
1925 rcu_read_lock_bh();
1926 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1927 (void)l2tp_tunnel_delete(tunnel);
1928 }
1929 rcu_read_unlock_bh();
1930}
1931
James Chapmanfd558d12010-04-02 06:18:33 +00001932static struct pernet_operations l2tp_net_ops = {
1933 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001934 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001935 .id = &l2tp_net_id,
1936 .size = sizeof(struct l2tp_net),
1937};
1938
1939static int __init l2tp_init(void)
1940{
1941 int rc = 0;
1942
1943 rc = register_pernet_device(&l2tp_net_ops);
1944 if (rc)
1945 goto out;
1946
Tom Parkinf8ccac02013-01-31 23:43:00 +00001947 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1948 if (!l2tp_wq) {
1949 pr_err("alloc_workqueue failed\n");
1950 rc = -ENOMEM;
1951 goto out;
1952 }
1953
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001954 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001955
1956out:
1957 return rc;
1958}
1959
1960static void __exit l2tp_exit(void)
1961{
1962 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001963 if (l2tp_wq) {
1964 destroy_workqueue(l2tp_wq);
1965 l2tp_wq = NULL;
1966 }
James Chapmanfd558d12010-04-02 06:18:33 +00001967}
1968
1969module_init(l2tp_init);
1970module_exit(l2tp_exit);
1971
1972MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1973MODULE_DESCRIPTION("L2TP core");
1974MODULE_LICENSE("GPL");
1975MODULE_VERSION(L2TP_DRV_VERSION);
1976