blob: 5ca29659171da644b4020397265c6b07a4e94cfa [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*
2 * L2TP core.
3 *
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5 *
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
8 *
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
11 * Contributors:
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
20
Joe Perchesa4ca44f2012-05-16 09:55:56 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
James Chapmanfd558d12010-04-02 06:18:33 +000023#include <linux/module.h>
24#include <linux/string.h>
25#include <linux/list.h>
James Chapmane02d4942010-04-02 06:19:16 +000026#include <linux/rculist.h>
James Chapmanfd558d12010-04-02 06:18:33 +000027#include <linux/uaccess.h>
28
29#include <linux/kernel.h>
30#include <linux/spinlock.h>
31#include <linux/kthread.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/errno.h>
35#include <linux/jiffies.h>
36
37#include <linux/netdevice.h>
38#include <linux/net.h>
39#include <linux/inetdevice.h>
40#include <linux/skbuff.h>
41#include <linux/init.h>
James Chapman0d767512010-04-02 06:19:00 +000042#include <linux/in.h>
James Chapmanfd558d12010-04-02 06:18:33 +000043#include <linux/ip.h>
44#include <linux/udp.h>
James Chapman0d767512010-04-02 06:19:00 +000045#include <linux/l2tp.h>
James Chapmanfd558d12010-04-02 06:18:33 +000046#include <linux/hash.h>
47#include <linux/sort.h>
48#include <linux/file.h>
49#include <linux/nsproxy.h>
50#include <net/net_namespace.h>
51#include <net/netns/generic.h>
52#include <net/dst.h>
53#include <net/ip.h>
54#include <net/udp.h>
James Chapman309795f2010-04-02 06:19:10 +000055#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000056#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000057#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000058#include <net/inet6_connection_sock.h>
59#include <net/inet_ecn.h>
60#include <net/ip6_route.h>
David S. Millerd499bd22012-04-30 13:21:28 -040061#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000062
63#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000065
66#include "l2tp_core.h"
67
68#define L2TP_DRV_VERSION "V2.0"
69
70/* L2TP header constants */
71#define L2TP_HDRFLAG_T 0x8000
72#define L2TP_HDRFLAG_L 0x4000
73#define L2TP_HDRFLAG_S 0x0800
74#define L2TP_HDRFLAG_O 0x0200
75#define L2TP_HDRFLAG_P 0x0100
76
77#define L2TP_HDR_VER_MASK 0x000F
78#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000079#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000080
81/* L2TPv3 default L2-specific sublayer */
82#define L2TP_SLFLAG_S 0x40000000
83#define L2TP_SL_SEQ_MASK 0x00ffffff
84
85#define L2TP_HDR_SIZE_SEQ 10
86#define L2TP_HDR_SIZE_NOSEQ 6
87
88/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0
90
James Chapmanfd558d12010-04-02 06:18:33 +000091/* Private data stored for received packets in the skb.
92 */
93struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000094 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000095 u16 has_seq;
96 u16 length;
97 unsigned long expires;
98};
99
100#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101
102static atomic_t l2tp_tunnel_count;
103static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000104static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000105
106/* per-net private data for this module */
107static unsigned int l2tp_net_id;
108struct l2tp_net {
109 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000110 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000111 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000112 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000113};
114
stephen hemmingerfc130842010-10-21 07:50:46 +0000115static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
116static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000117
James Chapmanfd558d12010-04-02 06:18:33 +0000118static inline struct l2tp_net *l2tp_pernet(struct net *net)
119{
120 BUG_ON(!net);
121
122 return net_generic(net, l2tp_net_id);
123}
124
stephen hemmingerfc130842010-10-21 07:50:46 +0000125/* Tunnel reference counts. Incremented per session that is added to
126 * the tunnel.
127 */
128static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
129{
130 atomic_inc(&tunnel->ref_count);
131}
132
133static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 if (atomic_dec_and_test(&tunnel->ref_count))
136 l2tp_tunnel_free(tunnel);
137}
138#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000139#define l2tp_tunnel_inc_refcount(_t) \
140do { \
141 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
142 __func__, __LINE__, (_t)->name, \
143 atomic_read(&_t->ref_count)); \
144 l2tp_tunnel_inc_refcount_1(_t); \
145} while (0)
146#define l2tp_tunnel_dec_refcount(_t)
147do { \
148 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
149 __func__, __LINE__, (_t)->name, \
150 atomic_read(&_t->ref_count)); \
151 l2tp_tunnel_dec_refcount_1(_t); \
152} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000153#else
154#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
155#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
156#endif
157
James Chapmanf7faffa2010-04-02 06:18:49 +0000158/* Session hash global list for L2TPv3.
159 * The session_id SHOULD be random according to RFC3931, but several
160 * L2TP implementations use incrementing session_ids. So we do a real
161 * hash on the session_id, rather than a simple bitmask.
162 */
163static inline struct hlist_head *
164l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
165{
166 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
167
168}
169
Tom Parkin80d84ef2013-01-22 05:13:48 +0000170/* Lookup the tunnel socket, possibly involving the fs code if the socket is
171 * owned by userspace. A struct sock returned from this function must be
172 * released using l2tp_tunnel_sock_put once you're done with it.
173 */
174struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
175{
176 int err = 0;
177 struct socket *sock = NULL;
178 struct sock *sk = NULL;
179
180 if (!tunnel)
181 goto out;
182
183 if (tunnel->fd >= 0) {
184 /* Socket is owned by userspace, who might be in the process
185 * of closing it. Look the socket up using the fd to ensure
186 * consistency.
187 */
188 sock = sockfd_lookup(tunnel->fd, &err);
189 if (sock)
190 sk = sock->sk;
191 } else {
192 /* Socket is owned by kernelspace */
193 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000194 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000195 }
196
197out:
198 return sk;
199}
200EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
201
202/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
203void l2tp_tunnel_sock_put(struct sock *sk)
204{
205 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
206 if (tunnel) {
207 if (tunnel->fd >= 0) {
208 /* Socket is owned by userspace */
209 sockfd_put(sk->sk_socket);
210 }
211 sock_put(sk);
212 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000213 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000214}
215EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
216
James Chapmanf7faffa2010-04-02 06:18:49 +0000217/* Lookup a session by id in the global session list
218 */
219static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
220{
221 struct l2tp_net *pn = l2tp_pernet(net);
222 struct hlist_head *session_list =
223 l2tp_session_id_hash_2(pn, session_id);
224 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000225
James Chapmane02d4942010-04-02 06:19:16 +0000226 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800227 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000228 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000229 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000230 return session;
231 }
232 }
James Chapmane02d4942010-04-02 06:19:16 +0000233 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000234
235 return NULL;
236}
237
James Chapmanfd558d12010-04-02 06:18:33 +0000238/* Session hash list.
239 * The session_id SHOULD be random according to RFC2661, but several
240 * L2TP implementations (Cisco and Microsoft) use incrementing
241 * session_ids. So we do a real hash on the session_id, rather than a
242 * simple bitmask.
243 */
244static inline struct hlist_head *
245l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
246{
247 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
248}
249
250/* Lookup a session by id
251 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000252struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000253{
James Chapmanf7faffa2010-04-02 06:18:49 +0000254 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000255 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000256
James Chapmanf7faffa2010-04-02 06:18:49 +0000257 /* In L2TPv3, session_ids are unique over all tunnels and we
258 * sometimes need to look them up before we know the
259 * tunnel.
260 */
261 if (tunnel == NULL)
262 return l2tp_session_find_2(net, session_id);
263
264 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000265 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800266 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000267 if (session->session_id == session_id) {
268 read_unlock_bh(&tunnel->hlist_lock);
269 return session;
270 }
271 }
272 read_unlock_bh(&tunnel->hlist_lock);
273
274 return NULL;
275}
276EXPORT_SYMBOL_GPL(l2tp_session_find);
277
278struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
279{
280 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000281 struct l2tp_session *session;
282 int count = 0;
283
284 read_lock_bh(&tunnel->hlist_lock);
285 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800286 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000287 if (++count > nth) {
288 read_unlock_bh(&tunnel->hlist_lock);
289 return session;
290 }
291 }
292 }
293
294 read_unlock_bh(&tunnel->hlist_lock);
295
296 return NULL;
297}
298EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
299
James Chapman309795f2010-04-02 06:19:10 +0000300/* Lookup a session by interface name.
301 * This is very inefficient but is only used by management interfaces.
302 */
303struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
304{
305 struct l2tp_net *pn = l2tp_pernet(net);
306 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000307 struct l2tp_session *session;
308
James Chapmane02d4942010-04-02 06:19:16 +0000309 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000310 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800311 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000312 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000313 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000314 return session;
315 }
316 }
317 }
318
James Chapmane02d4942010-04-02 06:19:16 +0000319 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000320
321 return NULL;
322}
323EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
324
James Chapmanfd558d12010-04-02 06:18:33 +0000325/* Lookup a tunnel by id
326 */
327struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
328{
329 struct l2tp_tunnel *tunnel;
330 struct l2tp_net *pn = l2tp_pernet(net);
331
James Chapmane02d4942010-04-02 06:19:16 +0000332 rcu_read_lock_bh();
333 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000334 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000335 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000336 return tunnel;
337 }
338 }
James Chapmane02d4942010-04-02 06:19:16 +0000339 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000340
341 return NULL;
342}
343EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
344
345struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
346{
347 struct l2tp_net *pn = l2tp_pernet(net);
348 struct l2tp_tunnel *tunnel;
349 int count = 0;
350
James Chapmane02d4942010-04-02 06:19:16 +0000351 rcu_read_lock_bh();
352 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000353 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000354 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000355 return tunnel;
356 }
357 }
358
James Chapmane02d4942010-04-02 06:19:16 +0000359 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000360
361 return NULL;
362}
363EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
364
365/*****************************************************************************
366 * Receive data handling
367 *****************************************************************************/
368
369/* Queue a skb in order. We come here only if the skb has an L2TP sequence
370 * number.
371 */
372static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
373{
374 struct sk_buff *skbp;
375 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000376 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000377
378 spin_lock_bh(&session->reorder_q.lock);
379 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
380 if (L2TP_SKB_CB(skbp)->ns > ns) {
381 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000382 l2tp_dbg(session, L2TP_MSG_SEQ,
383 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
384 session->name, ns, L2TP_SKB_CB(skbp)->ns,
385 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000386 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000387 goto out;
388 }
389 }
390
391 __skb_queue_tail(&session->reorder_q, skb);
392
393out:
394 spin_unlock_bh(&session->reorder_q.lock);
395}
396
397/* Dequeue a single skb.
398 */
399static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
400{
401 struct l2tp_tunnel *tunnel = session->tunnel;
402 int length = L2TP_SKB_CB(skb)->length;
403
404 /* We're about to requeue the skb, so return resources
405 * to its current owner (a socket receive buffer).
406 */
407 skb_orphan(skb);
408
Tom Parkin7b7c0712013-03-19 06:11:22 +0000409 atomic_long_inc(&tunnel->stats.rx_packets);
410 atomic_long_add(length, &tunnel->stats.rx_bytes);
411 atomic_long_inc(&session->stats.rx_packets);
412 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000413
414 if (L2TP_SKB_CB(skb)->has_seq) {
415 /* Bump our Nr */
416 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000417 if (tunnel->version == L2TP_HDR_VER_2)
418 session->nr &= 0xffff;
419 else
420 session->nr &= 0xffffff;
421
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)
510 if (sk->sk_family == PF_INET6) {
511 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 Chapmanb6dc01a2013-07-02 20:28:58 +0100545/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
546 * acceptable, else non-zero.
547 */
548static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
549{
550 if (session->reorder_timeout != 0) {
551 /* Packet reordering enabled. Add skb to session's
552 * reorder queue, in order of ns.
553 */
554 l2tp_recv_queue_skb(session, skb);
555 } else {
556 /* Packet reordering disabled. Discard out-of-sequence
557 * packets
558 */
559 if (L2TP_SKB_CB(skb)->ns != session->nr) {
560 atomic_long_inc(&session->stats.rx_seq_discards);
561 l2tp_dbg(session, L2TP_MSG_SEQ,
562 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
563 session->name, L2TP_SKB_CB(skb)->ns,
564 L2TP_SKB_CB(skb)->length, session->nr,
565 skb_queue_len(&session->reorder_q));
566 goto discard;
567 }
568 skb_queue_tail(&session->reorder_q, skb);
569 }
570
571 return 0;
572
573discard:
574 return 1;
575}
576
James Chapmanf7faffa2010-04-02 06:18:49 +0000577/* Do receive processing of L2TP data frames. We handle both L2TPv2
578 * and L2TPv3 data frames here.
579 *
580 * L2TPv2 Data Message Header
581 *
582 * 0 1 2 3
583 * 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
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | Tunnel ID | Session ID |
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * | Ns (opt) | Nr (opt) |
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 * | Offset Size (opt) | Offset pad... (opt)
592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
593 *
594 * Data frames are marked by T=0. All other fields are the same as
595 * those in L2TP control frames.
596 *
597 * L2TPv3 Data Message Header
598 *
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * | L2TP Session Header |
601 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 * | L2-Specific Sublayer |
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 * | Tunnel Payload ...
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 *
607 * L2TPv3 Session Header Over IP
608 *
609 * 0 1 2 3
610 * 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
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * | Session ID |
613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 * | Cookie (optional, maximum 64 bits)...
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * |
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
618 *
619 * L2TPv3 L2-Specific Sublayer Format
620 *
621 * 0 1 2 3
622 * 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
623 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
624 * |x|S|x|x|x|x|x|x| Sequence Number |
625 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
626 *
627 * Cookie value, sublayer format and offset (pad) are negotiated with
628 * the peer when the session is set up. Unlike L2TPv2, we do not need
629 * to parse the packet header to determine if optional fields are
630 * present.
631 *
632 * Caller must already have parsed the frame and determined that it is
633 * a data (not control) frame before coming here. Fields up to the
634 * session-id have already been parsed and ptr points to the data
635 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000636 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000637void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
638 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
639 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000640{
James Chapmanf7faffa2010-04-02 06:18:49 +0000641 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000642 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000643 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000644
645 /* The ref count is increased since we now hold a pointer to
646 * the session. Take care to decrement the refcnt when exiting
647 * this function from now on...
648 */
649 l2tp_session_inc_refcount(session);
650 if (session->ref)
651 (*session->ref)(session);
652
James Chapmanf7faffa2010-04-02 06:18:49 +0000653 /* Parse and check optional cookie */
654 if (session->peer_cookie_len > 0) {
655 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000656 l2tp_info(tunnel, L2TP_MSG_DATA,
657 "%s: cookie mismatch (%u/%u). Discarding.\n",
658 tunnel->name, tunnel->tunnel_id,
659 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000660 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000661 goto discard;
662 }
663 ptr += session->peer_cookie_len;
664 }
665
James Chapmanfd558d12010-04-02 06:18:33 +0000666 /* Handle the optional sequence numbers. Sequence numbers are
667 * in different places for L2TPv2 and L2TPv3.
668 *
669 * If we are the LAC, enable/disable sequence numbers under
670 * the control of the LNS. If no sequence numbers present but
671 * we were expecting them, discard frame.
672 */
673 ns = nr = 0;
674 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000675 if (tunnel->version == L2TP_HDR_VER_2) {
676 if (hdrflags & L2TP_HDRFLAG_S) {
677 ns = ntohs(*(__be16 *) ptr);
678 ptr += 2;
679 nr = ntohs(*(__be16 *) ptr);
680 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000681
James Chapmanf7faffa2010-04-02 06:18:49 +0000682 /* Store L2TP info in the skb */
683 L2TP_SKB_CB(skb)->ns = ns;
684 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000685
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000686 l2tp_dbg(session, L2TP_MSG_SEQ,
687 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
688 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000689 }
690 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
691 u32 l2h = ntohl(*(__be32 *) ptr);
692
693 if (l2h & 0x40000000) {
694 ns = l2h & 0x00ffffff;
695
696 /* Store L2TP info in the skb */
697 L2TP_SKB_CB(skb)->ns = ns;
698 L2TP_SKB_CB(skb)->has_seq = 1;
699
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000700 l2tp_dbg(session, L2TP_MSG_SEQ,
701 "%s: recv data ns=%u, session nr=%u\n",
702 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000703 }
James Chapmanfd558d12010-04-02 06:18:33 +0000704 }
705
James Chapmanf7faffa2010-04-02 06:18:49 +0000706 /* Advance past L2-specific header, if present */
707 ptr += session->l2specific_len;
708
James Chapmanfd558d12010-04-02 06:18:33 +0000709 if (L2TP_SKB_CB(skb)->has_seq) {
710 /* Received a packet with sequence numbers. If we're the LNS,
711 * check if we sre sending sequence numbers and if not,
712 * configure it so.
713 */
714 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000715 l2tp_info(session, L2TP_MSG_SEQ,
716 "%s: requested to enable seq numbers by LNS\n",
717 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000718 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000719 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000720 }
721 } else {
722 /* No sequence numbers.
723 * If user has configured mandatory sequence numbers, discard.
724 */
725 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000726 l2tp_warn(session, L2TP_MSG_SEQ,
727 "%s: recv data has no seq numbers when required. Discarding.\n",
728 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000729 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000730 goto discard;
731 }
732
733 /* If we're the LAC and we're sending sequence numbers, the
734 * LNS has requested that we no longer send sequence numbers.
735 * If we're the LNS and we're sending sequence numbers, the
736 * LAC is broken. Discard the frame.
737 */
738 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000739 l2tp_info(session, L2TP_MSG_SEQ,
740 "%s: requested to disable seq numbers by LNS\n",
741 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000742 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000743 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000744 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000745 l2tp_warn(session, L2TP_MSG_SEQ,
746 "%s: recv data has no seq numbers when required. Discarding.\n",
747 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000748 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000749 goto discard;
750 }
751 }
752
James Chapmanf7faffa2010-04-02 06:18:49 +0000753 /* Session data offset is handled differently for L2TPv2 and
754 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
755 * the header. For L2TPv3, the offset is negotiated using AVPs
756 * in the session setup control protocol.
757 */
758 if (tunnel->version == L2TP_HDR_VER_2) {
759 /* If offset bit set, skip it. */
760 if (hdrflags & L2TP_HDRFLAG_O) {
761 offset = ntohs(*(__be16 *)ptr);
762 ptr += 2 + offset;
763 }
764 } else
765 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000766
767 offset = ptr - optr;
768 if (!pskb_may_pull(skb, offset))
769 goto discard;
770
771 __skb_pull(skb, offset);
772
773 /* If caller wants to process the payload before we queue the
774 * packet, do so now.
775 */
776 if (payload_hook)
777 if ((*payload_hook)(skb))
778 goto discard;
779
780 /* Prepare skb for adding to the session's reorder_q. Hold
781 * packets for max reorder_timeout or 1 second if not
782 * reordering.
783 */
784 L2TP_SKB_CB(skb)->length = length;
785 L2TP_SKB_CB(skb)->expires = jiffies +
786 (session->reorder_timeout ? session->reorder_timeout : HZ);
787
788 /* Add packet to the session's receive queue. Reordering is done here, if
789 * enabled. Saved L2TP protocol info is stored in skb->sb[].
790 */
791 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100792 if (l2tp_recv_data_seq(session, skb))
793 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000794 } else {
795 /* No sequence numbers. Add the skb to the tail of the
796 * reorder queue. This ensures that it will be
797 * delivered after all previous sequenced skbs.
798 */
799 skb_queue_tail(&session->reorder_q, skb);
800 }
801
802 /* Try to dequeue as many skbs from reorder_q as we can. */
803 l2tp_recv_dequeue(session);
804
805 l2tp_session_dec_refcount(session);
806
James Chapmanf7faffa2010-04-02 06:18:49 +0000807 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000808
809discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000810 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000811 kfree_skb(skb);
812
813 if (session->deref)
814 (*session->deref)(session);
815
816 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000817}
818EXPORT_SYMBOL(l2tp_recv_common);
819
Tom Parkin48f72f92013-03-19 06:11:19 +0000820/* Drop skbs from the session's reorder_q
821 */
822int l2tp_session_queue_purge(struct l2tp_session *session)
823{
824 struct sk_buff *skb = NULL;
825 BUG_ON(!session);
826 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
827 while ((skb = skb_dequeue(&session->reorder_q))) {
828 atomic_long_inc(&session->stats.rx_errors);
829 kfree_skb(skb);
830 if (session->deref)
831 (*session->deref)(session);
832 }
833 return 0;
834}
835EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
836
James Chapmanf7faffa2010-04-02 06:18:49 +0000837/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
838 * here. The skb is not on a list when we get here.
839 * Returns 0 if the packet was a data packet and was successfully passed on.
840 * Returns 1 if the packet was not a good data packet and could not be
841 * forwarded. All such packets are passed up to userspace to deal with.
842 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000843static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
844 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000845{
846 struct l2tp_session *session = NULL;
847 unsigned char *ptr, *optr;
848 u16 hdrflags;
849 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000850 u16 version;
851 int length;
852
853 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
854 goto discard_bad_csum;
855
856 /* UDP always verifies the packet length. */
857 __skb_pull(skb, sizeof(struct udphdr));
858
859 /* Short packet? */
860 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000861 l2tp_info(tunnel, L2TP_MSG_DATA,
862 "%s: recv short packet (len=%d)\n",
863 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000864 goto error;
865 }
866
James Chapmanf7faffa2010-04-02 06:18:49 +0000867 /* Trace packet contents, if enabled */
868 if (tunnel->debug & L2TP_MSG_DATA) {
869 length = min(32u, skb->len);
870 if (!pskb_may_pull(skb, length))
871 goto error;
872
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000873 pr_debug("%s: recv\n", tunnel->name);
874 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000875 }
876
Eric Dumazete50e7052011-11-08 13:59:44 -0500877 /* Point to L2TP header */
878 optr = ptr = skb->data;
879
James Chapmanf7faffa2010-04-02 06:18:49 +0000880 /* Get L2TP header flags */
881 hdrflags = ntohs(*(__be16 *) ptr);
882
883 /* Check protocol version */
884 version = hdrflags & L2TP_HDR_VER_MASK;
885 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000886 l2tp_info(tunnel, L2TP_MSG_DATA,
887 "%s: recv protocol version mismatch: got %d expected %d\n",
888 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000889 goto error;
890 }
891
892 /* Get length of L2TP packet */
893 length = skb->len;
894
895 /* If type is control packet, it is handled by userspace. */
896 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000897 l2tp_dbg(tunnel, L2TP_MSG_DATA,
898 "%s: recv control packet, len=%d\n",
899 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000900 goto error;
901 }
902
903 /* Skip flags */
904 ptr += 2;
905
906 if (tunnel->version == L2TP_HDR_VER_2) {
907 /* If length is present, skip it */
908 if (hdrflags & L2TP_HDRFLAG_L)
909 ptr += 2;
910
911 /* Extract tunnel and session ID */
912 tunnel_id = ntohs(*(__be16 *) ptr);
913 ptr += 2;
914 session_id = ntohs(*(__be16 *) ptr);
915 ptr += 2;
916 } else {
917 ptr += 2; /* skip reserved bits */
918 tunnel_id = tunnel->tunnel_id;
919 session_id = ntohl(*(__be32 *) ptr);
920 ptr += 4;
921 }
922
923 /* Find the session context */
924 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000925 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000926 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000927 l2tp_info(tunnel, L2TP_MSG_DATA,
928 "%s: no session found (%u/%u). Passing up.\n",
929 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000930 goto error;
931 }
932
933 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000934
935 return 0;
936
937discard_bad_csum:
938 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
939 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000940 atomic_long_inc(&tunnel->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000941 kfree_skb(skb);
942
943 return 0;
944
945error:
946 /* Put UDP header back */
947 __skb_push(skb, sizeof(struct udphdr));
948
949 return 1;
950}
James Chapmanfd558d12010-04-02 06:18:33 +0000951
952/* UDP encapsulation receive handler. See net/ipv4/udp.c.
953 * Return codes:
954 * 0 : success.
955 * <0: error
956 * >0: skb should be passed up to userspace as UDP.
957 */
958int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
959{
960 struct l2tp_tunnel *tunnel;
961
962 tunnel = l2tp_sock_to_tunnel(sk);
963 if (tunnel == NULL)
964 goto pass_up;
965
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000966 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
967 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000968
969 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
970 goto pass_up_put;
971
972 sock_put(sk);
973 return 0;
974
975pass_up_put:
976 sock_put(sk);
977pass_up:
978 return 1;
979}
980EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
981
982/************************************************************************
983 * Transmit handling
984 ***********************************************************************/
985
986/* Build an L2TP header for the session into the buffer provided.
987 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000988static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000989{
James Chapmanf7faffa2010-04-02 06:18:49 +0000990 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000991 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000992 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000993 u16 flags = L2TP_HDR_VER_2;
994 u32 tunnel_id = tunnel->peer_tunnel_id;
995 u32 session_id = session->peer_session_id;
996
997 if (session->send_seq)
998 flags |= L2TP_HDRFLAG_S;
999
1000 /* Setup L2TP header. */
1001 *bufp++ = htons(flags);
1002 *bufp++ = htons(tunnel_id);
1003 *bufp++ = htons(session_id);
1004 if (session->send_seq) {
1005 *bufp++ = htons(session->ns);
1006 *bufp++ = 0;
1007 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001008 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001009 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1010 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001011 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001012
1013 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001014}
1015
James Chapmanf7faffa2010-04-02 06:18:49 +00001016static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001017{
James Chapman0d767512010-04-02 06:19:00 +00001018 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001019 char *bufp = buf;
1020 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001021
James Chapman0d767512010-04-02 06:19:00 +00001022 /* Setup L2TP header. The header differs slightly for UDP and
1023 * IP encapsulations. For UDP, there is 4 bytes of flags.
1024 */
1025 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1026 u16 flags = L2TP_HDR_VER_3;
1027 *((__be16 *) bufp) = htons(flags);
1028 bufp += 2;
1029 *((__be16 *) bufp) = 0;
1030 bufp += 2;
1031 }
1032
James Chapmanf7faffa2010-04-02 06:18:49 +00001033 *((__be32 *) bufp) = htonl(session->peer_session_id);
1034 bufp += 4;
1035 if (session->cookie_len) {
1036 memcpy(bufp, &session->cookie[0], session->cookie_len);
1037 bufp += session->cookie_len;
1038 }
1039 if (session->l2specific_len) {
1040 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1041 u32 l2h = 0;
1042 if (session->send_seq) {
1043 l2h = 0x40000000 | session->ns;
1044 session->ns++;
1045 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001046 l2tp_dbg(session, L2TP_MSG_SEQ,
1047 "%s: updated ns to %u\n",
1048 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001049 }
1050
1051 *((__be32 *) bufp) = htonl(l2h);
1052 }
1053 bufp += session->l2specific_len;
1054 }
1055 if (session->offset)
1056 bufp += session->offset;
1057
1058 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001059}
James Chapmanfd558d12010-04-02 06:18:33 +00001060
stephen hemmingerfc130842010-10-21 07:50:46 +00001061static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001062 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001063{
1064 struct l2tp_tunnel *tunnel = session->tunnel;
1065 unsigned int len = skb->len;
1066 int error;
1067
1068 /* Debug */
1069 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001070 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1071 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001072 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001073 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1074 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001075
1076 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001077 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1078 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001079
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001080 pr_debug("%s: xmit\n", session->name);
1081 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1082 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001083 }
1084
1085 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001086 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001087#if IS_ENABLED(CONFIG_IPV6)
1088 if (skb->sk->sk_family == PF_INET6)
1089 error = inet6_csk_xmit(skb, NULL);
1090 else
1091#endif
1092 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001093
1094 /* Update stats */
1095 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001096 atomic_long_inc(&tunnel->stats.tx_packets);
1097 atomic_long_add(len, &tunnel->stats.tx_bytes);
1098 atomic_long_inc(&session->stats.tx_packets);
1099 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001100 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001101 atomic_long_inc(&tunnel->stats.tx_errors);
1102 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001103 }
1104
1105 return 0;
1106}
James Chapmanfd558d12010-04-02 06:18:33 +00001107
1108/* Automatically called when the skb is freed.
1109 */
1110static void l2tp_sock_wfree(struct sk_buff *skb)
1111{
1112 sock_put(skb->sk);
1113}
1114
1115/* For data skbs that we transmit, we associate with the tunnel socket
1116 * but don't do accounting.
1117 */
1118static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1119{
1120 sock_hold(sk);
1121 skb->sk = sk;
1122 skb->destructor = l2tp_sock_wfree;
1123}
1124
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001125#if IS_ENABLED(CONFIG_IPV6)
1126static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1127 int udp_len)
1128{
1129 struct ipv6_pinfo *np = inet6_sk(sk);
1130 struct udphdr *uh = udp_hdr(skb);
1131
1132 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1133 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1134 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1135 skb->ip_summed = CHECKSUM_UNNECESSARY;
1136 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1137 IPPROTO_UDP, csum);
1138 if (uh->check == 0)
1139 uh->check = CSUM_MANGLED_0;
1140 } else {
1141 skb->ip_summed = CHECKSUM_PARTIAL;
1142 skb->csum_start = skb_transport_header(skb) - skb->head;
1143 skb->csum_offset = offsetof(struct udphdr, check);
1144 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1145 udp_len, IPPROTO_UDP, 0);
1146 }
1147}
1148#endif
1149
James Chapmanfd558d12010-04-02 06:18:33 +00001150/* If caller requires the skb to have a ppp header, the header must be
1151 * inserted in the skb data before calling this function.
1152 */
1153int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1154{
1155 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001156 struct l2tp_tunnel *tunnel = session->tunnel;
1157 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001158 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001159 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001160 struct inet_sock *inet;
1161 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001162 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001163 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1164 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001165 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001166
1167 /* Check that there's enough headroom in the skb to insert IP,
1168 * UDP and L2TP headers. If not enough, expand it to
1169 * make room. Adjust truesize.
1170 */
1171 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001172 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001173 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001174 kfree_skb(skb);
1175 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001176 }
James Chapmanfd558d12010-04-02 06:18:33 +00001177
James Chapmanfd558d12010-04-02 06:18:33 +00001178 skb_orphan(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001179 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001180 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001181
James Chapman0d767512010-04-02 06:19:00 +00001182 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001183 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1184 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1185 IPSKB_REROUTED);
1186 nf_reset(skb);
1187
David S. Miller6af88da2011-05-08 13:45:20 -07001188 bh_lock_sock(sk);
1189 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001190 kfree_skb(skb);
1191 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001192 goto out_unlock;
1193 }
1194
James Chapmanfd558d12010-04-02 06:18:33 +00001195 /* Get routing info from the tunnel socket */
1196 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001197 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001198
David S. Millerd9d8da82011-05-06 22:23:20 -07001199 inet = inet_sk(sk);
1200 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001201 switch (tunnel->encap) {
1202 case L2TP_ENCAPTYPE_UDP:
1203 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001204 __skb_push(skb, sizeof(*uh));
1205 skb_reset_transport_header(skb);
1206 uh = udp_hdr(skb);
1207 uh->source = inet->inet_sport;
1208 uh->dest = inet->inet_dport;
1209 udp_len = uhlen + hdr_len + data_len;
1210 uh->len = htons(udp_len);
1211 uh->check = 0;
1212
1213 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001214#if IS_ENABLED(CONFIG_IPV6)
1215 if (sk->sk_family == PF_INET6)
1216 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1217 else
1218#endif
James Chapman0d767512010-04-02 06:19:00 +00001219 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1220 skb->ip_summed = CHECKSUM_NONE;
1221 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1222 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1223 skb->ip_summed = CHECKSUM_COMPLETE;
1224 csum = skb_checksum(skb, 0, udp_len, 0);
1225 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1226 inet->inet_daddr,
1227 udp_len, IPPROTO_UDP, csum);
1228 if (uh->check == 0)
1229 uh->check = CSUM_MANGLED_0;
1230 } else {
1231 skb->ip_summed = CHECKSUM_PARTIAL;
1232 skb->csum_start = skb_transport_header(skb) - skb->head;
1233 skb->csum_offset = offsetof(struct udphdr, check);
1234 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1235 inet->inet_daddr,
1236 udp_len, IPPROTO_UDP, 0);
1237 }
1238 break;
1239
1240 case L2TP_ENCAPTYPE_IP:
1241 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001242 }
1243
James Chapman0d767512010-04-02 06:19:00 +00001244 l2tp_skb_set_owner_w(skb, sk);
1245
David S. Millerd9d8da82011-05-06 22:23:20 -07001246 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001247out_unlock:
1248 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001249
Eric Dumazetb8c84302012-06-28 20:15:13 +00001250 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001251}
1252EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1253
1254/*****************************************************************************
1255 * Tinnel and session create/destroy.
1256 *****************************************************************************/
1257
1258/* Tunnel socket destruct hook.
1259 * The tunnel context is deleted only when all session sockets have been
1260 * closed.
1261 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001262static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001263{
1264 struct l2tp_tunnel *tunnel;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001265 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001266
1267 tunnel = sk->sk_user_data;
1268 if (tunnel == NULL)
1269 goto end;
1270
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001271 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001272
James Chapmanfd558d12010-04-02 06:18:33 +00001273
Tom Parkinf8ccac02013-01-31 23:43:00 +00001274 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001275 switch (tunnel->encap) {
1276 case L2TP_ENCAPTYPE_UDP:
1277 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1278 (udp_sk(sk))->encap_type = 0;
1279 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001280 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001281 break;
1282 case L2TP_ENCAPTYPE_IP:
1283 break;
1284 }
James Chapmanfd558d12010-04-02 06:18:33 +00001285
1286 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001287 sk->sk_destruct = tunnel->old_sk_destruct;
1288 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001289 tunnel->sock = NULL;
1290
1291 /* Remove the tunnel struct from the tunnel list */
1292 pn = l2tp_pernet(tunnel->l2tp_net);
1293 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1294 list_del_rcu(&tunnel->list);
1295 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1296 atomic_dec(&l2tp_tunnel_count);
1297
1298 l2tp_tunnel_closeall(tunnel);
1299 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001300
1301 /* Call the original destructor */
1302 if (sk->sk_destruct)
1303 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001304end:
1305 return;
1306}
James Chapmanfd558d12010-04-02 06:18:33 +00001307
1308/* When the tunnel is closed, all the attached sessions need to go too.
1309 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001310void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001311{
1312 int hash;
1313 struct hlist_node *walk;
1314 struct hlist_node *tmp;
1315 struct l2tp_session *session;
1316
1317 BUG_ON(tunnel == NULL);
1318
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001319 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1320 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001321
1322 write_lock_bh(&tunnel->hlist_lock);
1323 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1324again:
1325 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1326 session = hlist_entry(walk, struct l2tp_session, hlist);
1327
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001328 l2tp_info(session, L2TP_MSG_CONTROL,
1329 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001330
1331 hlist_del_init(&session->hlist);
1332
James Chapmanfd558d12010-04-02 06:18:33 +00001333 if (session->ref != NULL)
1334 (*session->ref)(session);
1335
1336 write_unlock_bh(&tunnel->hlist_lock);
1337
Tom Parkinf6e16b22013-03-19 06:11:23 +00001338 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001339 l2tp_session_queue_purge(session);
1340
James Chapmanfd558d12010-04-02 06:18:33 +00001341 if (session->session_close != NULL)
1342 (*session->session_close)(session);
1343
1344 if (session->deref != NULL)
1345 (*session->deref)(session);
1346
Tom Parkin9980d002013-03-19 06:11:13 +00001347 l2tp_session_dec_refcount(session);
1348
James Chapmanfd558d12010-04-02 06:18:33 +00001349 write_lock_bh(&tunnel->hlist_lock);
1350
1351 /* Now restart from the beginning of this hash
1352 * chain. We always remove a session from the
1353 * list so we are guaranteed to make forward
1354 * progress.
1355 */
1356 goto again;
1357 }
1358 }
1359 write_unlock_bh(&tunnel->hlist_lock);
1360}
Tom Parkine34f4c72013-03-19 06:11:14 +00001361EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001362
Tom Parkin9980d002013-03-19 06:11:13 +00001363/* Tunnel socket destroy hook for UDP encapsulation */
1364static void l2tp_udp_encap_destroy(struct sock *sk)
1365{
1366 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1367 if (tunnel) {
1368 l2tp_tunnel_closeall(tunnel);
1369 sock_put(sk);
1370 }
1371}
1372
James Chapmanfd558d12010-04-02 06:18:33 +00001373/* Really kill the tunnel.
1374 * Come here only when all sessions have been cleared from the tunnel.
1375 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001376static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001377{
James Chapmanfd558d12010-04-02 06:18:33 +00001378 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1379 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001380 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001381 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001382}
James Chapmanfd558d12010-04-02 06:18:33 +00001383
Tom Parkinf8ccac02013-01-31 23:43:00 +00001384/* Workqueue tunnel deletion function */
1385static void l2tp_tunnel_del_work(struct work_struct *work)
1386{
1387 struct l2tp_tunnel *tunnel = NULL;
1388 struct socket *sock = NULL;
1389 struct sock *sk = NULL;
1390
1391 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1392 sk = l2tp_tunnel_sock_lookup(tunnel);
1393 if (!sk)
1394 return;
1395
1396 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001397
Tom Parkin02d13ed2013-03-19 06:11:18 +00001398 /* If the tunnel socket was created by userspace, then go through the
1399 * inet layer to shut the socket down, and let userspace close it.
1400 * Otherwise, if we created the socket directly within the kernel, use
1401 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001402 * In either case the tunnel resources are freed in the socket
1403 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001404 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001405 if (tunnel->fd >= 0) {
1406 if (sock)
1407 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001408 } else {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001409 if (sock)
1410 kernel_sock_shutdown(sock, SHUT_RDWR);
1411 sk_release_kernel(sk);
Tom Parkin167eb172013-01-31 23:43:03 +00001412 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001413
1414 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001415}
James Chapmanfd558d12010-04-02 06:18:33 +00001416
James Chapman789a4a22010-04-02 06:19:40 +00001417/* Create a socket for the tunnel, if one isn't set up by
1418 * userspace. This is used for static tunnels where there is no
1419 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001420 *
1421 * Since we don't want these sockets to keep a namespace alive by
1422 * themselves, we drop the socket's namespace refcount after creation.
1423 * These sockets are freed when the namespace exits using the pernet
1424 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001425 */
Tom Parkin167eb172013-01-31 23:43:03 +00001426static int l2tp_tunnel_sock_create(struct net *net,
1427 u32 tunnel_id,
1428 u32 peer_tunnel_id,
1429 struct l2tp_tunnel_cfg *cfg,
1430 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001431{
1432 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001433 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001434 struct sockaddr_in udp_addr = {0};
1435 struct sockaddr_l2tpip ip_addr = {0};
1436#if IS_ENABLED(CONFIG_IPV6)
1437 struct sockaddr_in6 udp6_addr = {0};
1438 struct sockaddr_l2tpip6 ip6_addr = {0};
1439#endif
James Chapman789a4a22010-04-02 06:19:40 +00001440
1441 switch (cfg->encap) {
1442 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001443#if IS_ENABLED(CONFIG_IPV6)
1444 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001445 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001446 if (err < 0)
1447 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001448
Tom Parkin167eb172013-01-31 23:43:03 +00001449 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001450
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001451 udp6_addr.sin6_family = AF_INET6;
1452 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1453 sizeof(udp6_addr.sin6_addr));
1454 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1455 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1456 sizeof(udp6_addr));
1457 if (err < 0)
1458 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001459
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001460 udp6_addr.sin6_family = AF_INET6;
1461 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1462 sizeof(udp6_addr.sin6_addr));
1463 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1464 err = kernel_connect(sock,
1465 (struct sockaddr *) &udp6_addr,
1466 sizeof(udp6_addr), 0);
1467 if (err < 0)
1468 goto out;
1469 } else
1470#endif
1471 {
Tom Parkin167eb172013-01-31 23:43:03 +00001472 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001473 if (err < 0)
1474 goto out;
1475
Tom Parkin167eb172013-01-31 23:43:03 +00001476 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001477
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001478 udp_addr.sin_family = AF_INET;
1479 udp_addr.sin_addr = cfg->local_ip;
1480 udp_addr.sin_port = htons(cfg->local_udp_port);
1481 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1482 sizeof(udp_addr));
1483 if (err < 0)
1484 goto out;
1485
1486 udp_addr.sin_family = AF_INET;
1487 udp_addr.sin_addr = cfg->peer_ip;
1488 udp_addr.sin_port = htons(cfg->peer_udp_port);
1489 err = kernel_connect(sock,
1490 (struct sockaddr *) &udp_addr,
1491 sizeof(udp_addr), 0);
1492 if (err < 0)
1493 goto out;
1494 }
James Chapman789a4a22010-04-02 06:19:40 +00001495
1496 if (!cfg->use_udp_checksums)
1497 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1498
1499 break;
1500
1501 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001502#if IS_ENABLED(CONFIG_IPV6)
1503 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001504 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1505 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001506 if (err < 0)
1507 goto out;
1508
Tom Parkin167eb172013-01-31 23:43:03 +00001509 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001510
James Chapman5dac94e2012-04-29 21:48:55 +00001511 ip6_addr.l2tp_family = AF_INET6;
1512 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1513 sizeof(ip6_addr.l2tp_addr));
1514 ip6_addr.l2tp_conn_id = tunnel_id;
1515 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1516 sizeof(ip6_addr));
1517 if (err < 0)
1518 goto out;
1519
1520 ip6_addr.l2tp_family = AF_INET6;
1521 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1522 sizeof(ip6_addr.l2tp_addr));
1523 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1524 err = kernel_connect(sock,
1525 (struct sockaddr *) &ip6_addr,
1526 sizeof(ip6_addr), 0);
1527 if (err < 0)
1528 goto out;
1529 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001530#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001531 {
Tom Parkin167eb172013-01-31 23:43:03 +00001532 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1533 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001534 if (err < 0)
1535 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001536
Tom Parkin167eb172013-01-31 23:43:03 +00001537 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001538
James Chapman5dac94e2012-04-29 21:48:55 +00001539 ip_addr.l2tp_family = AF_INET;
1540 ip_addr.l2tp_addr = cfg->local_ip;
1541 ip_addr.l2tp_conn_id = tunnel_id;
1542 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1543 sizeof(ip_addr));
1544 if (err < 0)
1545 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001546
James Chapman5dac94e2012-04-29 21:48:55 +00001547 ip_addr.l2tp_family = AF_INET;
1548 ip_addr.l2tp_addr = cfg->peer_ip;
1549 ip_addr.l2tp_conn_id = peer_tunnel_id;
1550 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1551 sizeof(ip_addr), 0);
1552 if (err < 0)
1553 goto out;
1554 }
James Chapman789a4a22010-04-02 06:19:40 +00001555 break;
1556
1557 default:
1558 goto out;
1559 }
1560
1561out:
Tom Parkin167eb172013-01-31 23:43:03 +00001562 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001563 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001564 kernel_sock_shutdown(sock, SHUT_RDWR);
1565 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001566 *sockp = NULL;
1567 }
1568
1569 return err;
1570}
1571
Eric Dumazet37159ef2012-09-04 07:18:57 +00001572static struct lock_class_key l2tp_socket_class;
1573
James Chapmanfd558d12010-04-02 06:18:33 +00001574int 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)
1575{
1576 struct l2tp_tunnel *tunnel = NULL;
1577 int err;
1578 struct socket *sock = NULL;
1579 struct sock *sk = NULL;
1580 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001581 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001582
1583 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001584 * the userspace L2TP daemon. If not specified, create a
1585 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001586 */
James Chapman789a4a22010-04-02 06:19:40 +00001587 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001588 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1589 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001590 if (err < 0)
1591 goto err;
1592 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001593 sock = sockfd_lookup(fd, &err);
1594 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001595 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001596 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001597 err = -EBADF;
1598 goto err;
1599 }
1600
1601 /* Reject namespace mismatches */
1602 if (!net_eq(sock_net(sock->sk), net)) {
1603 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1604 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001605 goto err;
1606 }
James Chapmanfd558d12010-04-02 06:18:33 +00001607 }
1608
1609 sk = sock->sk;
1610
James Chapman0d767512010-04-02 06:19:00 +00001611 if (cfg != NULL)
1612 encap = cfg->encap;
1613
James Chapmanfd558d12010-04-02 06:18:33 +00001614 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001615 switch (encap) {
1616 case L2TP_ENCAPTYPE_UDP:
1617 err = -EPROTONOSUPPORT;
1618 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001619 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001620 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1621 goto err;
1622 }
1623 break;
1624 case L2TP_ENCAPTYPE_IP:
1625 err = -EPROTONOSUPPORT;
1626 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001627 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001628 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1629 goto err;
1630 }
1631 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001632 }
1633
1634 /* Check if this socket has already been prepped */
1635 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1636 if (tunnel != NULL) {
1637 /* This socket has already been prepped */
1638 err = -EBUSY;
1639 goto err;
1640 }
1641
James Chapmanfd558d12010-04-02 06:18:33 +00001642 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1643 if (tunnel == NULL) {
1644 err = -ENOMEM;
1645 goto err;
1646 }
1647
1648 tunnel->version = version;
1649 tunnel->tunnel_id = tunnel_id;
1650 tunnel->peer_tunnel_id = peer_tunnel_id;
1651 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1652
1653 tunnel->magic = L2TP_TUNNEL_MAGIC;
1654 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1655 rwlock_init(&tunnel->hlist_lock);
1656
1657 /* The net we belong to */
1658 tunnel->l2tp_net = net;
1659 pn = l2tp_pernet(net);
1660
James Chapman0d767512010-04-02 06:19:00 +00001661 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001662 tunnel->debug = cfg->debug;
1663
1664 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001665 tunnel->encap = encap;
1666 if (encap == L2TP_ENCAPTYPE_UDP) {
1667 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1668 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1669 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001670 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001671#if IS_ENABLED(CONFIG_IPV6)
1672 if (sk->sk_family == PF_INET6)
1673 udpv6_encap_enable();
1674 else
1675#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001676 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001677 }
James Chapmanfd558d12010-04-02 06:18:33 +00001678
1679 sk->sk_user_data = tunnel;
1680
1681 /* Hook on the tunnel socket destructor so that we can cleanup
1682 * if the tunnel socket goes away.
1683 */
1684 tunnel->old_sk_destruct = sk->sk_destruct;
1685 sk->sk_destruct = &l2tp_tunnel_destruct;
1686 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001687 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001688 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1689
James Chapmanfd558d12010-04-02 06:18:33 +00001690 sk->sk_allocation = GFP_ATOMIC;
1691
Tom Parkinf8ccac02013-01-31 23:43:00 +00001692 /* Init delete workqueue struct */
1693 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1694
James Chapmanfd558d12010-04-02 06:18:33 +00001695 /* Add tunnel to our list */
1696 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001697 atomic_inc(&l2tp_tunnel_count);
1698
1699 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001700 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001701 */
1702 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001703 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1704 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1705 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001706
1707 err = 0;
1708err:
1709 if (tunnelp)
1710 *tunnelp = tunnel;
1711
James Chapman789a4a22010-04-02 06:19:40 +00001712 /* If tunnel's socket was created by the kernel, it doesn't
1713 * have a file.
1714 */
1715 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001716 sockfd_put(sock);
1717
1718 return err;
1719}
1720EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1721
James Chapman309795f2010-04-02 06:19:10 +00001722/* This function is used by the netlink TUNNEL_DELETE command.
1723 */
1724int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1725{
Tom Parkin2b551c62013-03-19 06:11:16 +00001726 l2tp_tunnel_closeall(tunnel);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001727 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001728}
1729EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1730
James Chapmanfd558d12010-04-02 06:18:33 +00001731/* Really kill the session.
1732 */
1733void l2tp_session_free(struct l2tp_session *session)
1734{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001735 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001736
1737 BUG_ON(atomic_read(&session->ref_count) != 0);
1738
Tom Parkinf6e16b22013-03-19 06:11:23 +00001739 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001740 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001741 if (session->session_id != 0)
1742 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001743 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001744 session->tunnel = NULL;
1745 l2tp_tunnel_dec_refcount(tunnel);
1746 }
1747
1748 kfree(session);
1749
1750 return;
1751}
1752EXPORT_SYMBOL_GPL(l2tp_session_free);
1753
Tom Parkinf6e16b22013-03-19 06:11:23 +00001754/* Remove an l2tp session from l2tp_core's hash lists.
1755 * Provides a tidyup interface for pseudowire code which can't just route all
1756 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1757 * callback.
1758 */
1759void __l2tp_session_unhash(struct l2tp_session *session)
1760{
1761 struct l2tp_tunnel *tunnel = session->tunnel;
1762
1763 /* Remove the session from core hashes */
1764 if (tunnel) {
1765 /* Remove from the per-tunnel hash */
1766 write_lock_bh(&tunnel->hlist_lock);
1767 hlist_del_init(&session->hlist);
1768 write_unlock_bh(&tunnel->hlist_lock);
1769
1770 /* For L2TPv3 we have a per-net hash: remove from there, too */
1771 if (tunnel->version != L2TP_HDR_VER_2) {
1772 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1773 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1774 hlist_del_init_rcu(&session->global_hlist);
1775 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1776 synchronize_rcu();
1777 }
1778 }
1779}
1780EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1781
James Chapman309795f2010-04-02 06:19:10 +00001782/* This function is used by the netlink SESSION_DELETE command and by
1783 pseudowire modules.
1784 */
1785int l2tp_session_delete(struct l2tp_session *session)
1786{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001787 if (session->ref)
1788 (*session->ref)(session);
1789 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001790 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001791 if (session->session_close != NULL)
1792 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001793 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001794 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001795 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001796 return 0;
1797}
1798EXPORT_SYMBOL_GPL(l2tp_session_delete);
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