blob: ee726a7522925e58c7ae4bed2bbb20a031f30e5c [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;
James Chapmanf7faffa2010-04-02 06:18:49 +0000224
James Chapmane02d4942010-04-02 06:19:16 +0000225 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800226 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000227 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000228 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000229 return session;
230 }
231 }
James Chapmane02d4942010-04-02 06:19:16 +0000232 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000233
234 return NULL;
235}
236
James Chapmanfd558d12010-04-02 06:18:33 +0000237/* Session hash list.
238 * The session_id SHOULD be random according to RFC2661, but several
239 * L2TP implementations (Cisco and Microsoft) use incrementing
240 * session_ids. So we do a real hash on the session_id, rather than a
241 * simple bitmask.
242 */
243static inline struct hlist_head *
244l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
245{
246 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
247}
248
249/* Lookup a session by id
250 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000251struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000252{
James Chapmanf7faffa2010-04-02 06:18:49 +0000253 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000254 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000255
James Chapmanf7faffa2010-04-02 06:18:49 +0000256 /* In L2TPv3, session_ids are unique over all tunnels and we
257 * sometimes need to look them up before we know the
258 * tunnel.
259 */
260 if (tunnel == NULL)
261 return l2tp_session_find_2(net, session_id);
262
263 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000264 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800265 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000266 if (session->session_id == session_id) {
267 read_unlock_bh(&tunnel->hlist_lock);
268 return session;
269 }
270 }
271 read_unlock_bh(&tunnel->hlist_lock);
272
273 return NULL;
274}
275EXPORT_SYMBOL_GPL(l2tp_session_find);
276
277struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
278{
279 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000280 struct l2tp_session *session;
281 int count = 0;
282
283 read_lock_bh(&tunnel->hlist_lock);
284 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800285 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000286 if (++count > nth) {
287 read_unlock_bh(&tunnel->hlist_lock);
288 return session;
289 }
290 }
291 }
292
293 read_unlock_bh(&tunnel->hlist_lock);
294
295 return NULL;
296}
297EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
298
James Chapman309795f2010-04-02 06:19:10 +0000299/* Lookup a session by interface name.
300 * This is very inefficient but is only used by management interfaces.
301 */
302struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
303{
304 struct l2tp_net *pn = l2tp_pernet(net);
305 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000306 struct l2tp_session *session;
307
James Chapmane02d4942010-04-02 06:19:16 +0000308 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000309 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800310 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000311 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000312 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000313 return session;
314 }
315 }
316 }
317
James Chapmane02d4942010-04-02 06:19:16 +0000318 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000319
320 return NULL;
321}
322EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
323
James Chapmanfd558d12010-04-02 06:18:33 +0000324/* Lookup a tunnel by id
325 */
326struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
327{
328 struct l2tp_tunnel *tunnel;
329 struct l2tp_net *pn = l2tp_pernet(net);
330
James Chapmane02d4942010-04-02 06:19:16 +0000331 rcu_read_lock_bh();
332 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000333 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000334 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000335 return tunnel;
336 }
337 }
James Chapmane02d4942010-04-02 06:19:16 +0000338 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000339
340 return NULL;
341}
342EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
343
344struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
345{
346 struct l2tp_net *pn = l2tp_pernet(net);
347 struct l2tp_tunnel *tunnel;
348 int count = 0;
349
James Chapmane02d4942010-04-02 06:19:16 +0000350 rcu_read_lock_bh();
351 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000352 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000353 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000354 return tunnel;
355 }
356 }
357
James Chapmane02d4942010-04-02 06:19:16 +0000358 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000359
360 return NULL;
361}
362EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
363
364/*****************************************************************************
365 * Receive data handling
366 *****************************************************************************/
367
368/* Queue a skb in order. We come here only if the skb has an L2TP sequence
369 * number.
370 */
371static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
372{
373 struct sk_buff *skbp;
374 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000375 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapman5de7aee2012-04-29 21:48:46 +0000376 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000377
378 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000379 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000380 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
381 if (L2TP_SKB_CB(skbp)->ns > ns) {
382 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000383 l2tp_dbg(session, L2TP_MSG_SEQ,
384 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
385 session->name, ns, L2TP_SKB_CB(skbp)->ns,
386 skb_queue_len(&session->reorder_q));
James Chapman5de7aee2012-04-29 21:48:46 +0000387 u64_stats_update_begin(&sstats->syncp);
388 sstats->rx_oos_packets++;
389 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000390 goto out;
391 }
392 }
393
394 __skb_queue_tail(&session->reorder_q, skb);
395
396out:
397 spin_unlock_bh(&session->reorder_q.lock);
398}
399
400/* Dequeue a single skb.
401 */
402static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
403{
404 struct l2tp_tunnel *tunnel = session->tunnel;
405 int length = L2TP_SKB_CB(skb)->length;
James Chapman5de7aee2012-04-29 21:48:46 +0000406 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000407
408 /* We're about to requeue the skb, so return resources
409 * to its current owner (a socket receive buffer).
410 */
411 skb_orphan(skb);
412
James Chapman5de7aee2012-04-29 21:48:46 +0000413 tstats = &tunnel->stats;
414 u64_stats_update_begin(&tstats->syncp);
415 sstats = &session->stats;
416 u64_stats_update_begin(&sstats->syncp);
417 tstats->rx_packets++;
418 tstats->rx_bytes += length;
419 sstats->rx_packets++;
420 sstats->rx_bytes += length;
421 u64_stats_update_end(&tstats->syncp);
422 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000423
424 if (L2TP_SKB_CB(skb)->has_seq) {
425 /* Bump our Nr */
426 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000427 if (tunnel->version == L2TP_HDR_VER_2)
428 session->nr &= 0xffff;
429 else
430 session->nr &= 0xffffff;
431
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000432 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
433 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000434 }
435
436 /* call private receive handler */
437 if (session->recv_skb != NULL)
438 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
439 else
440 kfree_skb(skb);
441
442 if (session->deref)
443 (*session->deref)(session);
444}
445
446/* Dequeue skbs from the session's reorder_q, subject to packet order.
447 * Skbs that have been in the queue for too long are simply discarded.
448 */
449static void l2tp_recv_dequeue(struct l2tp_session *session)
450{
451 struct sk_buff *skb;
452 struct sk_buff *tmp;
James Chapman5de7aee2012-04-29 21:48:46 +0000453 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000454
455 /* If the pkt at the head of the queue has the nr that we
456 * expect to send up next, dequeue it and any other
457 * in-sequence packets behind it.
458 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000459start:
James Chapmanfd558d12010-04-02 06:18:33 +0000460 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000461 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000462 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
463 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
James Chapman5de7aee2012-04-29 21:48:46 +0000464 u64_stats_update_begin(&sstats->syncp);
465 sstats->rx_seq_discards++;
466 sstats->rx_errors++;
467 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000468 l2tp_dbg(session, L2TP_MSG_SEQ,
469 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
470 session->name, L2TP_SKB_CB(skb)->ns,
471 L2TP_SKB_CB(skb)->length, session->nr,
472 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000473 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000474 __skb_unlink(skb, &session->reorder_q);
475 kfree_skb(skb);
476 if (session->deref)
477 (*session->deref)(session);
478 continue;
479 }
480
481 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000482 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000483 l2tp_dbg(session, L2TP_MSG_SEQ,
484 "%s: advancing nr to next pkt: %u -> %u",
485 session->name, session->nr,
486 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000487 session->reorder_skip = 0;
488 session->nr = L2TP_SKB_CB(skb)->ns;
489 }
James Chapmanfd558d12010-04-02 06:18:33 +0000490 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000491 l2tp_dbg(session, L2TP_MSG_SEQ,
492 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
493 session->name, L2TP_SKB_CB(skb)->ns,
494 L2TP_SKB_CB(skb)->length, session->nr,
495 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000496 goto out;
497 }
498 }
499 __skb_unlink(skb, &session->reorder_q);
500
501 /* Process the skb. We release the queue lock while we
502 * do so to let other contexts process the queue.
503 */
504 spin_unlock_bh(&session->reorder_q.lock);
505 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000506 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000507 }
508
509out:
510 spin_unlock_bh(&session->reorder_q.lock);
511}
512
513static inline int l2tp_verify_udp_checksum(struct sock *sk,
514 struct sk_buff *skb)
515{
516 struct udphdr *uh = udp_hdr(skb);
517 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000518 __wsum psum;
519
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000520 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000521 return 0;
522
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000523#if IS_ENABLED(CONFIG_IPV6)
524 if (sk->sk_family == PF_INET6) {
525 if (!uh->check) {
526 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
527 return 1;
528 }
529 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
530 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
531 &ipv6_hdr(skb)->daddr, ulen,
532 IPPROTO_UDP, skb->csum)) {
533 skb->ip_summed = CHECKSUM_UNNECESSARY;
534 return 0;
535 }
536 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
537 &ipv6_hdr(skb)->daddr,
538 skb->len, IPPROTO_UDP,
539 0));
540 } else
541#endif
542 {
543 struct inet_sock *inet;
544 if (!uh->check)
545 return 0;
546 inet = inet_sk(sk);
547 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
548 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000549
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000550 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
551 !csum_fold(csum_add(psum, skb->csum)))
552 return 0;
553 skb->csum = psum;
554 }
James Chapmanfd558d12010-04-02 06:18:33 +0000555
556 return __skb_checksum_complete(skb);
557}
558
James Chapmanf7faffa2010-04-02 06:18:49 +0000559/* Do receive processing of L2TP data frames. We handle both L2TPv2
560 * and L2TPv3 data frames here.
561 *
562 * L2TPv2 Data Message Header
563 *
564 * 0 1 2 3
565 * 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
566 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
568 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
569 * | Tunnel ID | Session ID |
570 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
571 * | Ns (opt) | Nr (opt) |
572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
573 * | Offset Size (opt) | Offset pad... (opt)
574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
575 *
576 * Data frames are marked by T=0. All other fields are the same as
577 * those in L2TP control frames.
578 *
579 * L2TPv3 Data Message Header
580 *
581 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
582 * | L2TP Session Header |
583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * | L2-Specific Sublayer |
585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * | Tunnel Payload ...
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 *
589 * L2TPv3 Session Header Over IP
590 *
591 * 0 1 2 3
592 * 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
593 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594 * | Session ID |
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | Cookie (optional, maximum 64 bits)...
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * |
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 *
601 * L2TPv3 L2-Specific Sublayer Format
602 *
603 * 0 1 2 3
604 * 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
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * |x|S|x|x|x|x|x|x| Sequence Number |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 *
609 * Cookie value, sublayer format and offset (pad) are negotiated with
610 * the peer when the session is set up. Unlike L2TPv2, we do not need
611 * to parse the packet header to determine if optional fields are
612 * present.
613 *
614 * Caller must already have parsed the frame and determined that it is
615 * a data (not control) frame before coming here. Fields up to the
616 * session-id have already been parsed and ptr points to the data
617 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000618 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000619void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
620 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
621 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000622{
James Chapmanf7faffa2010-04-02 06:18:49 +0000623 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000624 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000625 u32 ns, nr;
James Chapman5de7aee2012-04-29 21:48:46 +0000626 struct l2tp_stats *sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000627
628 /* The ref count is increased since we now hold a pointer to
629 * the session. Take care to decrement the refcnt when exiting
630 * this function from now on...
631 */
632 l2tp_session_inc_refcount(session);
633 if (session->ref)
634 (*session->ref)(session);
635
James Chapmanf7faffa2010-04-02 06:18:49 +0000636 /* Parse and check optional cookie */
637 if (session->peer_cookie_len > 0) {
638 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000639 l2tp_info(tunnel, L2TP_MSG_DATA,
640 "%s: cookie mismatch (%u/%u). Discarding.\n",
641 tunnel->name, tunnel->tunnel_id,
642 session->session_id);
James Chapman5de7aee2012-04-29 21:48:46 +0000643 u64_stats_update_begin(&sstats->syncp);
644 sstats->rx_cookie_discards++;
645 u64_stats_update_end(&sstats->syncp);
James Chapmanf7faffa2010-04-02 06:18:49 +0000646 goto discard;
647 }
648 ptr += session->peer_cookie_len;
649 }
650
James Chapmanfd558d12010-04-02 06:18:33 +0000651 /* Handle the optional sequence numbers. Sequence numbers are
652 * in different places for L2TPv2 and L2TPv3.
653 *
654 * If we are the LAC, enable/disable sequence numbers under
655 * the control of the LNS. If no sequence numbers present but
656 * we were expecting them, discard frame.
657 */
658 ns = nr = 0;
659 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000660 if (tunnel->version == L2TP_HDR_VER_2) {
661 if (hdrflags & L2TP_HDRFLAG_S) {
662 ns = ntohs(*(__be16 *) ptr);
663 ptr += 2;
664 nr = ntohs(*(__be16 *) ptr);
665 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000666
James Chapmanf7faffa2010-04-02 06:18:49 +0000667 /* Store L2TP info in the skb */
668 L2TP_SKB_CB(skb)->ns = ns;
669 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000670
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000671 l2tp_dbg(session, L2TP_MSG_SEQ,
672 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
673 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000674 }
675 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
676 u32 l2h = ntohl(*(__be32 *) ptr);
677
678 if (l2h & 0x40000000) {
679 ns = l2h & 0x00ffffff;
680
681 /* Store L2TP info in the skb */
682 L2TP_SKB_CB(skb)->ns = ns;
683 L2TP_SKB_CB(skb)->has_seq = 1;
684
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000685 l2tp_dbg(session, L2TP_MSG_SEQ,
686 "%s: recv data ns=%u, session nr=%u\n",
687 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000688 }
James Chapmanfd558d12010-04-02 06:18:33 +0000689 }
690
James Chapmanf7faffa2010-04-02 06:18:49 +0000691 /* Advance past L2-specific header, if present */
692 ptr += session->l2specific_len;
693
James Chapmanfd558d12010-04-02 06:18:33 +0000694 if (L2TP_SKB_CB(skb)->has_seq) {
695 /* Received a packet with sequence numbers. If we're the LNS,
696 * check if we sre sending sequence numbers and if not,
697 * configure it so.
698 */
699 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000700 l2tp_info(session, L2TP_MSG_SEQ,
701 "%s: requested to enable seq numbers by LNS\n",
702 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000703 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000704 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000705 }
706 } else {
707 /* No sequence numbers.
708 * If user has configured mandatory sequence numbers, discard.
709 */
710 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000711 l2tp_warn(session, L2TP_MSG_SEQ,
712 "%s: recv data has no seq numbers when required. Discarding.\n",
713 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000714 u64_stats_update_begin(&sstats->syncp);
715 sstats->rx_seq_discards++;
716 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000717 goto discard;
718 }
719
720 /* If we're the LAC and we're sending sequence numbers, the
721 * LNS has requested that we no longer send sequence numbers.
722 * If we're the LNS and we're sending sequence numbers, the
723 * LAC is broken. Discard the frame.
724 */
725 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000726 l2tp_info(session, L2TP_MSG_SEQ,
727 "%s: requested to disable seq numbers by LNS\n",
728 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000729 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000730 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000731 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000732 l2tp_warn(session, L2TP_MSG_SEQ,
733 "%s: recv data has no seq numbers when required. Discarding.\n",
734 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000735 u64_stats_update_begin(&sstats->syncp);
736 sstats->rx_seq_discards++;
737 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000738 goto discard;
739 }
740 }
741
James Chapmanf7faffa2010-04-02 06:18:49 +0000742 /* Session data offset is handled differently for L2TPv2 and
743 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
744 * the header. For L2TPv3, the offset is negotiated using AVPs
745 * in the session setup control protocol.
746 */
747 if (tunnel->version == L2TP_HDR_VER_2) {
748 /* If offset bit set, skip it. */
749 if (hdrflags & L2TP_HDRFLAG_O) {
750 offset = ntohs(*(__be16 *)ptr);
751 ptr += 2 + offset;
752 }
753 } else
754 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000755
756 offset = ptr - optr;
757 if (!pskb_may_pull(skb, offset))
758 goto discard;
759
760 __skb_pull(skb, offset);
761
762 /* If caller wants to process the payload before we queue the
763 * packet, do so now.
764 */
765 if (payload_hook)
766 if ((*payload_hook)(skb))
767 goto discard;
768
769 /* Prepare skb for adding to the session's reorder_q. Hold
770 * packets for max reorder_timeout or 1 second if not
771 * reordering.
772 */
773 L2TP_SKB_CB(skb)->length = length;
774 L2TP_SKB_CB(skb)->expires = jiffies +
775 (session->reorder_timeout ? session->reorder_timeout : HZ);
776
777 /* Add packet to the session's receive queue. Reordering is done here, if
778 * enabled. Saved L2TP protocol info is stored in skb->sb[].
779 */
780 if (L2TP_SKB_CB(skb)->has_seq) {
781 if (session->reorder_timeout != 0) {
782 /* Packet reordering enabled. Add skb to session's
783 * reorder queue, in order of ns.
784 */
785 l2tp_recv_queue_skb(session, skb);
786 } else {
787 /* Packet reordering disabled. Discard out-of-sequence
788 * packets
789 */
790 if (L2TP_SKB_CB(skb)->ns != session->nr) {
James Chapman5de7aee2012-04-29 21:48:46 +0000791 u64_stats_update_begin(&sstats->syncp);
792 sstats->rx_seq_discards++;
793 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000794 l2tp_dbg(session, L2TP_MSG_SEQ,
795 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
796 session->name, L2TP_SKB_CB(skb)->ns,
797 L2TP_SKB_CB(skb)->length, session->nr,
798 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000799 goto discard;
800 }
801 skb_queue_tail(&session->reorder_q, skb);
802 }
803 } else {
804 /* No sequence numbers. Add the skb to the tail of the
805 * reorder queue. This ensures that it will be
806 * delivered after all previous sequenced skbs.
807 */
808 skb_queue_tail(&session->reorder_q, skb);
809 }
810
811 /* Try to dequeue as many skbs from reorder_q as we can. */
812 l2tp_recv_dequeue(session);
813
814 l2tp_session_dec_refcount(session);
815
James Chapmanf7faffa2010-04-02 06:18:49 +0000816 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000817
818discard:
James Chapman5de7aee2012-04-29 21:48:46 +0000819 u64_stats_update_begin(&sstats->syncp);
820 sstats->rx_errors++;
821 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000822 kfree_skb(skb);
823
824 if (session->deref)
825 (*session->deref)(session);
826
827 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000828}
829EXPORT_SYMBOL(l2tp_recv_common);
830
831/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
832 * here. The skb is not on a list when we get here.
833 * Returns 0 if the packet was a data packet and was successfully passed on.
834 * Returns 1 if the packet was not a good data packet and could not be
835 * forwarded. All such packets are passed up to userspace to deal with.
836 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000837static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
838 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000839{
840 struct l2tp_session *session = NULL;
841 unsigned char *ptr, *optr;
842 u16 hdrflags;
843 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000844 u16 version;
845 int length;
James Chapman5de7aee2012-04-29 21:48:46 +0000846 struct l2tp_stats *tstats;
James Chapmanf7faffa2010-04-02 06:18:49 +0000847
848 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
849 goto discard_bad_csum;
850
851 /* UDP always verifies the packet length. */
852 __skb_pull(skb, sizeof(struct udphdr));
853
854 /* Short packet? */
855 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000856 l2tp_info(tunnel, L2TP_MSG_DATA,
857 "%s: recv short packet (len=%d)\n",
858 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000859 goto error;
860 }
861
James Chapmanf7faffa2010-04-02 06:18:49 +0000862 /* Trace packet contents, if enabled */
863 if (tunnel->debug & L2TP_MSG_DATA) {
864 length = min(32u, skb->len);
865 if (!pskb_may_pull(skb, length))
866 goto error;
867
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000868 pr_debug("%s: recv\n", tunnel->name);
869 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000870 }
871
Eric Dumazete50e7052011-11-08 13:59:44 -0500872 /* Point to L2TP header */
873 optr = ptr = skb->data;
874
James Chapmanf7faffa2010-04-02 06:18:49 +0000875 /* Get L2TP header flags */
876 hdrflags = ntohs(*(__be16 *) ptr);
877
878 /* Check protocol version */
879 version = hdrflags & L2TP_HDR_VER_MASK;
880 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000881 l2tp_info(tunnel, L2TP_MSG_DATA,
882 "%s: recv protocol version mismatch: got %d expected %d\n",
883 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000884 goto error;
885 }
886
887 /* Get length of L2TP packet */
888 length = skb->len;
889
890 /* If type is control packet, it is handled by userspace. */
891 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000892 l2tp_dbg(tunnel, L2TP_MSG_DATA,
893 "%s: recv control packet, len=%d\n",
894 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000895 goto error;
896 }
897
898 /* Skip flags */
899 ptr += 2;
900
901 if (tunnel->version == L2TP_HDR_VER_2) {
902 /* If length is present, skip it */
903 if (hdrflags & L2TP_HDRFLAG_L)
904 ptr += 2;
905
906 /* Extract tunnel and session ID */
907 tunnel_id = ntohs(*(__be16 *) ptr);
908 ptr += 2;
909 session_id = ntohs(*(__be16 *) ptr);
910 ptr += 2;
911 } else {
912 ptr += 2; /* skip reserved bits */
913 tunnel_id = tunnel->tunnel_id;
914 session_id = ntohl(*(__be32 *) ptr);
915 ptr += 4;
916 }
917
918 /* Find the session context */
919 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000920 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000921 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000922 l2tp_info(tunnel, L2TP_MSG_DATA,
923 "%s: no session found (%u/%u). Passing up.\n",
924 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000925 goto error;
926 }
927
928 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000929
930 return 0;
931
932discard_bad_csum:
933 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
934 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
James Chapman5de7aee2012-04-29 21:48:46 +0000935 tstats = &tunnel->stats;
936 u64_stats_update_begin(&tstats->syncp);
937 tstats->rx_errors++;
938 u64_stats_update_end(&tstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000939 kfree_skb(skb);
940
941 return 0;
942
943error:
944 /* Put UDP header back */
945 __skb_push(skb, sizeof(struct udphdr));
946
947 return 1;
948}
James Chapmanfd558d12010-04-02 06:18:33 +0000949
950/* UDP encapsulation receive handler. See net/ipv4/udp.c.
951 * Return codes:
952 * 0 : success.
953 * <0: error
954 * >0: skb should be passed up to userspace as UDP.
955 */
956int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
957{
958 struct l2tp_tunnel *tunnel;
959
960 tunnel = l2tp_sock_to_tunnel(sk);
961 if (tunnel == NULL)
962 goto pass_up;
963
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000964 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
965 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000966
967 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
968 goto pass_up_put;
969
970 sock_put(sk);
971 return 0;
972
973pass_up_put:
974 sock_put(sk);
975pass_up:
976 return 1;
977}
978EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
979
980/************************************************************************
981 * Transmit handling
982 ***********************************************************************/
983
984/* Build an L2TP header for the session into the buffer provided.
985 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000986static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000987{
James Chapmanf7faffa2010-04-02 06:18:49 +0000988 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000989 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000990 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000991 u16 flags = L2TP_HDR_VER_2;
992 u32 tunnel_id = tunnel->peer_tunnel_id;
993 u32 session_id = session->peer_session_id;
994
995 if (session->send_seq)
996 flags |= L2TP_HDRFLAG_S;
997
998 /* Setup L2TP header. */
999 *bufp++ = htons(flags);
1000 *bufp++ = htons(tunnel_id);
1001 *bufp++ = htons(session_id);
1002 if (session->send_seq) {
1003 *bufp++ = htons(session->ns);
1004 *bufp++ = 0;
1005 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001006 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001007 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1008 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001009 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001010
1011 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001012}
1013
James Chapmanf7faffa2010-04-02 06:18:49 +00001014static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001015{
James Chapman0d767512010-04-02 06:19:00 +00001016 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001017 char *bufp = buf;
1018 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001019
James Chapman0d767512010-04-02 06:19:00 +00001020 /* Setup L2TP header. The header differs slightly for UDP and
1021 * IP encapsulations. For UDP, there is 4 bytes of flags.
1022 */
1023 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1024 u16 flags = L2TP_HDR_VER_3;
1025 *((__be16 *) bufp) = htons(flags);
1026 bufp += 2;
1027 *((__be16 *) bufp) = 0;
1028 bufp += 2;
1029 }
1030
James Chapmanf7faffa2010-04-02 06:18:49 +00001031 *((__be32 *) bufp) = htonl(session->peer_session_id);
1032 bufp += 4;
1033 if (session->cookie_len) {
1034 memcpy(bufp, &session->cookie[0], session->cookie_len);
1035 bufp += session->cookie_len;
1036 }
1037 if (session->l2specific_len) {
1038 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1039 u32 l2h = 0;
1040 if (session->send_seq) {
1041 l2h = 0x40000000 | session->ns;
1042 session->ns++;
1043 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001044 l2tp_dbg(session, L2TP_MSG_SEQ,
1045 "%s: updated ns to %u\n",
1046 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001047 }
1048
1049 *((__be32 *) bufp) = htonl(l2h);
1050 }
1051 bufp += session->l2specific_len;
1052 }
1053 if (session->offset)
1054 bufp += session->offset;
1055
1056 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001057}
James Chapmanfd558d12010-04-02 06:18:33 +00001058
stephen hemmingerfc130842010-10-21 07:50:46 +00001059static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001060 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001061{
1062 struct l2tp_tunnel *tunnel = session->tunnel;
1063 unsigned int len = skb->len;
1064 int error;
James Chapman5de7aee2012-04-29 21:48:46 +00001065 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +00001066
1067 /* Debug */
1068 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001069 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1070 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001071 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001072 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1073 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001074
1075 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001076 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1077 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001078
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001079 pr_debug("%s: xmit\n", session->name);
1080 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1081 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001082 }
1083
1084 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001085 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001086#if IS_ENABLED(CONFIG_IPV6)
1087 if (skb->sk->sk_family == PF_INET6)
1088 error = inet6_csk_xmit(skb, NULL);
1089 else
1090#endif
1091 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001092
1093 /* Update stats */
James Chapman5de7aee2012-04-29 21:48:46 +00001094 tstats = &tunnel->stats;
1095 u64_stats_update_begin(&tstats->syncp);
1096 sstats = &session->stats;
1097 u64_stats_update_begin(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001098 if (error >= 0) {
James Chapman5de7aee2012-04-29 21:48:46 +00001099 tstats->tx_packets++;
1100 tstats->tx_bytes += len;
1101 sstats->tx_packets++;
1102 sstats->tx_bytes += len;
James Chapmanfd558d12010-04-02 06:18:33 +00001103 } else {
James Chapman5de7aee2012-04-29 21:48:46 +00001104 tstats->tx_errors++;
1105 sstats->tx_errors++;
James Chapmanfd558d12010-04-02 06:18:33 +00001106 }
James Chapman5de7aee2012-04-29 21:48:46 +00001107 u64_stats_update_end(&tstats->syncp);
1108 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001109
1110 return 0;
1111}
James Chapmanfd558d12010-04-02 06:18:33 +00001112
1113/* Automatically called when the skb is freed.
1114 */
1115static void l2tp_sock_wfree(struct sk_buff *skb)
1116{
1117 sock_put(skb->sk);
1118}
1119
1120/* For data skbs that we transmit, we associate with the tunnel socket
1121 * but don't do accounting.
1122 */
1123static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1124{
1125 sock_hold(sk);
1126 skb->sk = sk;
1127 skb->destructor = l2tp_sock_wfree;
1128}
1129
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001130#if IS_ENABLED(CONFIG_IPV6)
1131static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1132 int udp_len)
1133{
1134 struct ipv6_pinfo *np = inet6_sk(sk);
1135 struct udphdr *uh = udp_hdr(skb);
1136
1137 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1138 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1139 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1140 skb->ip_summed = CHECKSUM_UNNECESSARY;
1141 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1142 IPPROTO_UDP, csum);
1143 if (uh->check == 0)
1144 uh->check = CSUM_MANGLED_0;
1145 } else {
1146 skb->ip_summed = CHECKSUM_PARTIAL;
1147 skb->csum_start = skb_transport_header(skb) - skb->head;
1148 skb->csum_offset = offsetof(struct udphdr, check);
1149 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1150 udp_len, IPPROTO_UDP, 0);
1151 }
1152}
1153#endif
1154
James Chapmanfd558d12010-04-02 06:18:33 +00001155/* If caller requires the skb to have a ppp header, the header must be
1156 * inserted in the skb data before calling this function.
1157 */
1158int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1159{
1160 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001161 struct l2tp_tunnel *tunnel = session->tunnel;
1162 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001163 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001164 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001165 struct inet_sock *inet;
1166 __wsum csum;
James Chapmanfd558d12010-04-02 06:18:33 +00001167 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001168 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1169 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001170 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001171
1172 /* Check that there's enough headroom in the skb to insert IP,
1173 * UDP and L2TP headers. If not enough, expand it to
1174 * make room. Adjust truesize.
1175 */
1176 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001177 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001178 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001179 kfree_skb(skb);
1180 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001181 }
James Chapmanfd558d12010-04-02 06:18:33 +00001182
James Chapmanfd558d12010-04-02 06:18:33 +00001183 skb_orphan(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001184 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001185 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001186
James Chapman0d767512010-04-02 06:19:00 +00001187 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001188 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1189 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1190 IPSKB_REROUTED);
1191 nf_reset(skb);
1192
David S. Miller6af88da2011-05-08 13:45:20 -07001193 bh_lock_sock(sk);
1194 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001195 kfree_skb(skb);
1196 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001197 goto out_unlock;
1198 }
1199
James Chapmanfd558d12010-04-02 06:18:33 +00001200 /* Get routing info from the tunnel socket */
1201 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001202 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001203
David S. Millerd9d8da82011-05-06 22:23:20 -07001204 inet = inet_sk(sk);
1205 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001206 switch (tunnel->encap) {
1207 case L2TP_ENCAPTYPE_UDP:
1208 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001209 __skb_push(skb, sizeof(*uh));
1210 skb_reset_transport_header(skb);
1211 uh = udp_hdr(skb);
1212 uh->source = inet->inet_sport;
1213 uh->dest = inet->inet_dport;
1214 udp_len = uhlen + hdr_len + data_len;
1215 uh->len = htons(udp_len);
1216 uh->check = 0;
1217
1218 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001219#if IS_ENABLED(CONFIG_IPV6)
1220 if (sk->sk_family == PF_INET6)
1221 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1222 else
1223#endif
James Chapman0d767512010-04-02 06:19:00 +00001224 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1225 skb->ip_summed = CHECKSUM_NONE;
1226 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1227 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1228 skb->ip_summed = CHECKSUM_COMPLETE;
1229 csum = skb_checksum(skb, 0, udp_len, 0);
1230 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1231 inet->inet_daddr,
1232 udp_len, IPPROTO_UDP, csum);
1233 if (uh->check == 0)
1234 uh->check = CSUM_MANGLED_0;
1235 } else {
1236 skb->ip_summed = CHECKSUM_PARTIAL;
1237 skb->csum_start = skb_transport_header(skb) - skb->head;
1238 skb->csum_offset = offsetof(struct udphdr, check);
1239 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1240 inet->inet_daddr,
1241 udp_len, IPPROTO_UDP, 0);
1242 }
1243 break;
1244
1245 case L2TP_ENCAPTYPE_IP:
1246 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001247 }
1248
James Chapman0d767512010-04-02 06:19:00 +00001249 l2tp_skb_set_owner_w(skb, sk);
1250
David S. Millerd9d8da82011-05-06 22:23:20 -07001251 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001252out_unlock:
1253 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001254
Eric Dumazetb8c84302012-06-28 20:15:13 +00001255 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001256}
1257EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1258
1259/*****************************************************************************
1260 * Tinnel and session create/destroy.
1261 *****************************************************************************/
1262
1263/* Tunnel socket destruct hook.
1264 * The tunnel context is deleted only when all session sockets have been
1265 * closed.
1266 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001267static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001268{
1269 struct l2tp_tunnel *tunnel;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001270 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001271
1272 tunnel = sk->sk_user_data;
1273 if (tunnel == NULL)
1274 goto end;
1275
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001276 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001277
James Chapmanfd558d12010-04-02 06:18:33 +00001278
Tom Parkinf8ccac02013-01-31 23:43:00 +00001279 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001280 switch (tunnel->encap) {
1281 case L2TP_ENCAPTYPE_UDP:
1282 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1283 (udp_sk(sk))->encap_type = 0;
1284 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001285 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001286 break;
1287 case L2TP_ENCAPTYPE_IP:
1288 break;
1289 }
James Chapmanfd558d12010-04-02 06:18:33 +00001290
1291 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001292 sk->sk_destruct = tunnel->old_sk_destruct;
1293 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001294 tunnel->sock = NULL;
1295
1296 /* Remove the tunnel struct from the tunnel list */
1297 pn = l2tp_pernet(tunnel->l2tp_net);
1298 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1299 list_del_rcu(&tunnel->list);
1300 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1301 atomic_dec(&l2tp_tunnel_count);
1302
1303 l2tp_tunnel_closeall(tunnel);
1304 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001305
1306 /* Call the original destructor */
1307 if (sk->sk_destruct)
1308 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001309end:
1310 return;
1311}
James Chapmanfd558d12010-04-02 06:18:33 +00001312
1313/* When the tunnel is closed, all the attached sessions need to go too.
1314 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001315static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001316{
1317 int hash;
1318 struct hlist_node *walk;
1319 struct hlist_node *tmp;
1320 struct l2tp_session *session;
1321
1322 BUG_ON(tunnel == NULL);
1323
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001324 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1325 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001326
1327 write_lock_bh(&tunnel->hlist_lock);
1328 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1329again:
1330 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1331 session = hlist_entry(walk, struct l2tp_session, hlist);
1332
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001333 l2tp_info(session, L2TP_MSG_CONTROL,
1334 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001335
1336 hlist_del_init(&session->hlist);
1337
1338 /* Since we should hold the sock lock while
1339 * doing any unbinding, we need to release the
1340 * lock we're holding before taking that lock.
1341 * Hold a reference to the sock so it doesn't
1342 * disappear as we're jumping between locks.
1343 */
1344 if (session->ref != NULL)
1345 (*session->ref)(session);
1346
1347 write_unlock_bh(&tunnel->hlist_lock);
1348
James Chapmanf7faffa2010-04-02 06:18:49 +00001349 if (tunnel->version != L2TP_HDR_VER_2) {
1350 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1351
James Chapmane02d4942010-04-02 06:19:16 +00001352 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1353 hlist_del_init_rcu(&session->global_hlist);
1354 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1355 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001356 }
1357
James Chapmanfd558d12010-04-02 06:18:33 +00001358 if (session->session_close != NULL)
1359 (*session->session_close)(session);
1360
1361 if (session->deref != NULL)
1362 (*session->deref)(session);
1363
Tom Parkin9980d002013-03-19 06:11:13 +00001364 l2tp_session_dec_refcount(session);
1365
James Chapmanfd558d12010-04-02 06:18:33 +00001366 write_lock_bh(&tunnel->hlist_lock);
1367
1368 /* Now restart from the beginning of this hash
1369 * chain. We always remove a session from the
1370 * list so we are guaranteed to make forward
1371 * progress.
1372 */
1373 goto again;
1374 }
1375 }
1376 write_unlock_bh(&tunnel->hlist_lock);
1377}
James Chapmanfd558d12010-04-02 06:18:33 +00001378
Tom Parkin9980d002013-03-19 06:11:13 +00001379/* Tunnel socket destroy hook for UDP encapsulation */
1380static void l2tp_udp_encap_destroy(struct sock *sk)
1381{
1382 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1383 if (tunnel) {
1384 l2tp_tunnel_closeall(tunnel);
1385 sock_put(sk);
1386 }
1387}
1388
James Chapmanfd558d12010-04-02 06:18:33 +00001389/* Really kill the tunnel.
1390 * Come here only when all sessions have been cleared from the tunnel.
1391 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001392static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001393{
James Chapmanfd558d12010-04-02 06:18:33 +00001394 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1395 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001396 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001397 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001398}
James Chapmanfd558d12010-04-02 06:18:33 +00001399
Tom Parkinf8ccac02013-01-31 23:43:00 +00001400/* Workqueue tunnel deletion function */
1401static void l2tp_tunnel_del_work(struct work_struct *work)
1402{
1403 struct l2tp_tunnel *tunnel = NULL;
1404 struct socket *sock = NULL;
1405 struct sock *sk = NULL;
1406
1407 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1408 sk = l2tp_tunnel_sock_lookup(tunnel);
1409 if (!sk)
1410 return;
1411
1412 sock = sk->sk_socket;
1413 BUG_ON(!sock);
1414
Tom Parkin167eb172013-01-31 23:43:03 +00001415 /* If the tunnel socket was created directly by the kernel, use the
1416 * sk_* API to release the socket now. Otherwise go through the
1417 * inet_* layer to shut the socket down, and let userspace close it.
1418 * In either case the tunnel resources are freed in the socket
1419 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001420 */
Tom Parkin167eb172013-01-31 23:43:03 +00001421 if (sock->file == NULL) {
1422 kernel_sock_shutdown(sock, SHUT_RDWR);
1423 sk_release_kernel(sk);
1424 } else {
1425 inet_shutdown(sock, 2);
1426 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001427
1428 l2tp_tunnel_sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001429}
James Chapmanfd558d12010-04-02 06:18:33 +00001430
James Chapman789a4a22010-04-02 06:19:40 +00001431/* Create a socket for the tunnel, if one isn't set up by
1432 * userspace. This is used for static tunnels where there is no
1433 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001434 *
1435 * Since we don't want these sockets to keep a namespace alive by
1436 * themselves, we drop the socket's namespace refcount after creation.
1437 * These sockets are freed when the namespace exits using the pernet
1438 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001439 */
Tom Parkin167eb172013-01-31 23:43:03 +00001440static int l2tp_tunnel_sock_create(struct net *net,
1441 u32 tunnel_id,
1442 u32 peer_tunnel_id,
1443 struct l2tp_tunnel_cfg *cfg,
1444 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001445{
1446 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001447 struct socket *sock = NULL;
Tom Parkin167eb172013-01-31 23:43:03 +00001448 struct sockaddr_in udp_addr = {0};
1449 struct sockaddr_l2tpip ip_addr = {0};
1450#if IS_ENABLED(CONFIG_IPV6)
1451 struct sockaddr_in6 udp6_addr = {0};
1452 struct sockaddr_l2tpip6 ip6_addr = {0};
1453#endif
James Chapman789a4a22010-04-02 06:19:40 +00001454
1455 switch (cfg->encap) {
1456 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001457#if IS_ENABLED(CONFIG_IPV6)
1458 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001459 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001460 if (err < 0)
1461 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001462
Tom Parkin167eb172013-01-31 23:43:03 +00001463 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001464
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001465 udp6_addr.sin6_family = AF_INET6;
1466 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1467 sizeof(udp6_addr.sin6_addr));
1468 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1469 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1470 sizeof(udp6_addr));
1471 if (err < 0)
1472 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001473
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001474 udp6_addr.sin6_family = AF_INET6;
1475 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1476 sizeof(udp6_addr.sin6_addr));
1477 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1478 err = kernel_connect(sock,
1479 (struct sockaddr *) &udp6_addr,
1480 sizeof(udp6_addr), 0);
1481 if (err < 0)
1482 goto out;
1483 } else
1484#endif
1485 {
Tom Parkin167eb172013-01-31 23:43:03 +00001486 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001487 if (err < 0)
1488 goto out;
1489
Tom Parkin167eb172013-01-31 23:43:03 +00001490 sk_change_net(sock->sk, net);
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001491
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001492 udp_addr.sin_family = AF_INET;
1493 udp_addr.sin_addr = cfg->local_ip;
1494 udp_addr.sin_port = htons(cfg->local_udp_port);
1495 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1496 sizeof(udp_addr));
1497 if (err < 0)
1498 goto out;
1499
1500 udp_addr.sin_family = AF_INET;
1501 udp_addr.sin_addr = cfg->peer_ip;
1502 udp_addr.sin_port = htons(cfg->peer_udp_port);
1503 err = kernel_connect(sock,
1504 (struct sockaddr *) &udp_addr,
1505 sizeof(udp_addr), 0);
1506 if (err < 0)
1507 goto out;
1508 }
James Chapman789a4a22010-04-02 06:19:40 +00001509
1510 if (!cfg->use_udp_checksums)
1511 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1512
1513 break;
1514
1515 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001516#if IS_ENABLED(CONFIG_IPV6)
1517 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Parkin167eb172013-01-31 23:43:03 +00001518 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1519 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001520 if (err < 0)
1521 goto out;
1522
Tom Parkin167eb172013-01-31 23:43:03 +00001523 sk_change_net(sock->sk, net);
James Chapman5dac94e2012-04-29 21:48:55 +00001524
James Chapman5dac94e2012-04-29 21:48:55 +00001525 ip6_addr.l2tp_family = AF_INET6;
1526 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1527 sizeof(ip6_addr.l2tp_addr));
1528 ip6_addr.l2tp_conn_id = tunnel_id;
1529 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1530 sizeof(ip6_addr));
1531 if (err < 0)
1532 goto out;
1533
1534 ip6_addr.l2tp_family = AF_INET6;
1535 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1536 sizeof(ip6_addr.l2tp_addr));
1537 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1538 err = kernel_connect(sock,
1539 (struct sockaddr *) &ip6_addr,
1540 sizeof(ip6_addr), 0);
1541 if (err < 0)
1542 goto out;
1543 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001544#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001545 {
Tom Parkin167eb172013-01-31 23:43:03 +00001546 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1547 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001548 if (err < 0)
1549 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001550
Tom Parkin167eb172013-01-31 23:43:03 +00001551 sk_change_net(sock->sk, net);
James Chapman789a4a22010-04-02 06:19:40 +00001552
James Chapman5dac94e2012-04-29 21:48:55 +00001553 ip_addr.l2tp_family = AF_INET;
1554 ip_addr.l2tp_addr = cfg->local_ip;
1555 ip_addr.l2tp_conn_id = tunnel_id;
1556 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1557 sizeof(ip_addr));
1558 if (err < 0)
1559 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001560
James Chapman5dac94e2012-04-29 21:48:55 +00001561 ip_addr.l2tp_family = AF_INET;
1562 ip_addr.l2tp_addr = cfg->peer_ip;
1563 ip_addr.l2tp_conn_id = peer_tunnel_id;
1564 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1565 sizeof(ip_addr), 0);
1566 if (err < 0)
1567 goto out;
1568 }
James Chapman789a4a22010-04-02 06:19:40 +00001569 break;
1570
1571 default:
1572 goto out;
1573 }
1574
1575out:
Tom Parkin167eb172013-01-31 23:43:03 +00001576 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001577 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001578 kernel_sock_shutdown(sock, SHUT_RDWR);
1579 sk_release_kernel(sock->sk);
James Chapman789a4a22010-04-02 06:19:40 +00001580 *sockp = NULL;
1581 }
1582
1583 return err;
1584}
1585
Eric Dumazet37159ef2012-09-04 07:18:57 +00001586static struct lock_class_key l2tp_socket_class;
1587
James Chapmanfd558d12010-04-02 06:18:33 +00001588int 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)
1589{
1590 struct l2tp_tunnel *tunnel = NULL;
1591 int err;
1592 struct socket *sock = NULL;
1593 struct sock *sk = NULL;
1594 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001595 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001596
1597 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001598 * the userspace L2TP daemon. If not specified, create a
1599 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001600 */
James Chapman789a4a22010-04-02 06:19:40 +00001601 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001602 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1603 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001604 if (err < 0)
1605 goto err;
1606 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001607 sock = sockfd_lookup(fd, &err);
1608 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001609 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001610 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001611 err = -EBADF;
1612 goto err;
1613 }
1614
1615 /* Reject namespace mismatches */
1616 if (!net_eq(sock_net(sock->sk), net)) {
1617 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1618 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001619 goto err;
1620 }
James Chapmanfd558d12010-04-02 06:18:33 +00001621 }
1622
1623 sk = sock->sk;
1624
James Chapman0d767512010-04-02 06:19:00 +00001625 if (cfg != NULL)
1626 encap = cfg->encap;
1627
James Chapmanfd558d12010-04-02 06:18:33 +00001628 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001629 switch (encap) {
1630 case L2TP_ENCAPTYPE_UDP:
1631 err = -EPROTONOSUPPORT;
1632 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001633 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001634 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1635 goto err;
1636 }
1637 break;
1638 case L2TP_ENCAPTYPE_IP:
1639 err = -EPROTONOSUPPORT;
1640 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001641 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001642 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1643 goto err;
1644 }
1645 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001646 }
1647
1648 /* Check if this socket has already been prepped */
1649 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1650 if (tunnel != NULL) {
1651 /* This socket has already been prepped */
1652 err = -EBUSY;
1653 goto err;
1654 }
1655
James Chapmanfd558d12010-04-02 06:18:33 +00001656 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1657 if (tunnel == NULL) {
1658 err = -ENOMEM;
1659 goto err;
1660 }
1661
1662 tunnel->version = version;
1663 tunnel->tunnel_id = tunnel_id;
1664 tunnel->peer_tunnel_id = peer_tunnel_id;
1665 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1666
1667 tunnel->magic = L2TP_TUNNEL_MAGIC;
1668 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1669 rwlock_init(&tunnel->hlist_lock);
1670
1671 /* The net we belong to */
1672 tunnel->l2tp_net = net;
1673 pn = l2tp_pernet(net);
1674
James Chapman0d767512010-04-02 06:19:00 +00001675 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001676 tunnel->debug = cfg->debug;
1677
1678 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001679 tunnel->encap = encap;
1680 if (encap == L2TP_ENCAPTYPE_UDP) {
1681 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1682 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1683 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Tom Parkin9980d002013-03-19 06:11:13 +00001684 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001685#if IS_ENABLED(CONFIG_IPV6)
1686 if (sk->sk_family == PF_INET6)
1687 udpv6_encap_enable();
1688 else
1689#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001690 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001691 }
James Chapmanfd558d12010-04-02 06:18:33 +00001692
1693 sk->sk_user_data = tunnel;
1694
1695 /* Hook on the tunnel socket destructor so that we can cleanup
1696 * if the tunnel socket goes away.
1697 */
1698 tunnel->old_sk_destruct = sk->sk_destruct;
1699 sk->sk_destruct = &l2tp_tunnel_destruct;
1700 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001701 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001702 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1703
James Chapmanfd558d12010-04-02 06:18:33 +00001704 sk->sk_allocation = GFP_ATOMIC;
1705
Tom Parkinf8ccac02013-01-31 23:43:00 +00001706 /* Init delete workqueue struct */
1707 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1708
James Chapmanfd558d12010-04-02 06:18:33 +00001709 /* Add tunnel to our list */
1710 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001711 atomic_inc(&l2tp_tunnel_count);
1712
1713 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001714 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001715 */
1716 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001717 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1718 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1719 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001720
1721 err = 0;
1722err:
1723 if (tunnelp)
1724 *tunnelp = tunnel;
1725
James Chapman789a4a22010-04-02 06:19:40 +00001726 /* If tunnel's socket was created by the kernel, it doesn't
1727 * have a file.
1728 */
1729 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001730 sockfd_put(sock);
1731
1732 return err;
1733}
1734EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1735
James Chapman309795f2010-04-02 06:19:10 +00001736/* This function is used by the netlink TUNNEL_DELETE command.
1737 */
1738int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1739{
Tom Parkinf8ccac02013-01-31 23:43:00 +00001740 return (false == queue_work(l2tp_wq, &tunnel->del_work));
James Chapman309795f2010-04-02 06:19:10 +00001741}
1742EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1743
James Chapmanfd558d12010-04-02 06:18:33 +00001744/* Really kill the session.
1745 */
1746void l2tp_session_free(struct l2tp_session *session)
1747{
1748 struct l2tp_tunnel *tunnel;
1749
1750 BUG_ON(atomic_read(&session->ref_count) != 0);
1751
1752 tunnel = session->tunnel;
1753 if (tunnel != NULL) {
1754 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1755
1756 /* Delete the session from the hash */
1757 write_lock_bh(&tunnel->hlist_lock);
1758 hlist_del_init(&session->hlist);
1759 write_unlock_bh(&tunnel->hlist_lock);
1760
James Chapmanf7faffa2010-04-02 06:18:49 +00001761 /* Unlink from the global hash if not L2TPv2 */
1762 if (tunnel->version != L2TP_HDR_VER_2) {
1763 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1764
James Chapmane02d4942010-04-02 06:19:16 +00001765 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1766 hlist_del_init_rcu(&session->global_hlist);
1767 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1768 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001769 }
1770
James Chapmanfd558d12010-04-02 06:18:33 +00001771 if (session->session_id != 0)
1772 atomic_dec(&l2tp_session_count);
1773
1774 sock_put(tunnel->sock);
1775
1776 /* This will delete the tunnel context if this
1777 * is the last session on the tunnel.
1778 */
1779 session->tunnel = NULL;
1780 l2tp_tunnel_dec_refcount(tunnel);
1781 }
1782
1783 kfree(session);
1784
1785 return;
1786}
1787EXPORT_SYMBOL_GPL(l2tp_session_free);
1788
James Chapman309795f2010-04-02 06:19:10 +00001789/* This function is used by the netlink SESSION_DELETE command and by
1790 pseudowire modules.
1791 */
1792int l2tp_session_delete(struct l2tp_session *session)
1793{
1794 if (session->session_close != NULL)
1795 (*session->session_close)(session);
1796
1797 l2tp_session_dec_refcount(session);
1798
1799 return 0;
1800}
1801EXPORT_SYMBOL_GPL(l2tp_session_delete);
1802
1803
James Chapmanf7faffa2010-04-02 06:18:49 +00001804/* We come here whenever a session's send_seq, cookie_len or
1805 * l2specific_len parameters are set.
1806 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001807static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001808{
1809 if (version == L2TP_HDR_VER_2) {
1810 session->hdr_len = 6;
1811 if (session->send_seq)
1812 session->hdr_len += 4;
1813 } else {
James Chapman0d767512010-04-02 06:19:00 +00001814 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1815 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1816 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001817 }
1818
1819}
James Chapmanf7faffa2010-04-02 06:18:49 +00001820
James Chapmanfd558d12010-04-02 06:18:33 +00001821struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1822{
1823 struct l2tp_session *session;
1824
1825 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1826 if (session != NULL) {
1827 session->magic = L2TP_SESSION_MAGIC;
1828 session->tunnel = tunnel;
1829
1830 session->session_id = session_id;
1831 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001832 session->nr = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001833
1834 sprintf(&session->name[0], "sess %u/%u",
1835 tunnel->tunnel_id, session->session_id);
1836
1837 skb_queue_head_init(&session->reorder_q);
1838
1839 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001840 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001841
1842 /* Inherit debug options from tunnel */
1843 session->debug = tunnel->debug;
1844
1845 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001846 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001847 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001848 session->mtu = cfg->mtu;
1849 session->mru = cfg->mru;
1850 session->send_seq = cfg->send_seq;
1851 session->recv_seq = cfg->recv_seq;
1852 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001853 session->reorder_timeout = cfg->reorder_timeout;
1854 session->offset = cfg->offset;
1855 session->l2specific_type = cfg->l2specific_type;
1856 session->l2specific_len = cfg->l2specific_len;
1857 session->cookie_len = cfg->cookie_len;
1858 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1859 session->peer_cookie_len = cfg->peer_cookie_len;
1860 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001861 }
1862
James Chapmanf7faffa2010-04-02 06:18:49 +00001863 if (tunnel->version == L2TP_HDR_VER_2)
1864 session->build_header = l2tp_build_l2tpv2_header;
1865 else
1866 session->build_header = l2tp_build_l2tpv3_header;
1867
1868 l2tp_session_set_header_len(session, tunnel->version);
1869
James Chapmanfd558d12010-04-02 06:18:33 +00001870 /* Bump the reference count. The session context is deleted
1871 * only when this drops to zero.
1872 */
1873 l2tp_session_inc_refcount(session);
1874 l2tp_tunnel_inc_refcount(tunnel);
1875
1876 /* Ensure tunnel socket isn't deleted */
1877 sock_hold(tunnel->sock);
1878
1879 /* Add session to the tunnel's hash list */
1880 write_lock_bh(&tunnel->hlist_lock);
1881 hlist_add_head(&session->hlist,
1882 l2tp_session_id_hash(tunnel, session_id));
1883 write_unlock_bh(&tunnel->hlist_lock);
1884
James Chapmanf7faffa2010-04-02 06:18:49 +00001885 /* And to the global session list if L2TPv3 */
1886 if (tunnel->version != L2TP_HDR_VER_2) {
1887 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1888
James Chapmane02d4942010-04-02 06:19:16 +00001889 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1890 hlist_add_head_rcu(&session->global_hlist,
1891 l2tp_session_id_hash_2(pn, session_id));
1892 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001893 }
1894
James Chapmanfd558d12010-04-02 06:18:33 +00001895 /* Ignore management session in session count value */
1896 if (session->session_id != 0)
1897 atomic_inc(&l2tp_session_count);
1898 }
1899
1900 return session;
1901}
1902EXPORT_SYMBOL_GPL(l2tp_session_create);
1903
1904/*****************************************************************************
1905 * Init and cleanup
1906 *****************************************************************************/
1907
1908static __net_init int l2tp_init_net(struct net *net)
1909{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001910 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001911 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001912
James Chapmanfd558d12010-04-02 06:18:33 +00001913 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001914 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001915
James Chapmanf7faffa2010-04-02 06:18:49 +00001916 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1917 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1918
James Chapmane02d4942010-04-02 06:19:16 +00001919 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001920
James Chapmanfd558d12010-04-02 06:18:33 +00001921 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001922}
1923
Tom Parkin167eb172013-01-31 23:43:03 +00001924static __net_exit void l2tp_exit_net(struct net *net)
1925{
1926 struct l2tp_net *pn = l2tp_pernet(net);
1927 struct l2tp_tunnel *tunnel = NULL;
1928
1929 rcu_read_lock_bh();
1930 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1931 (void)l2tp_tunnel_delete(tunnel);
1932 }
1933 rcu_read_unlock_bh();
1934}
1935
James Chapmanfd558d12010-04-02 06:18:33 +00001936static struct pernet_operations l2tp_net_ops = {
1937 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001938 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001939 .id = &l2tp_net_id,
1940 .size = sizeof(struct l2tp_net),
1941};
1942
1943static int __init l2tp_init(void)
1944{
1945 int rc = 0;
1946
1947 rc = register_pernet_device(&l2tp_net_ops);
1948 if (rc)
1949 goto out;
1950
Tom Parkinf8ccac02013-01-31 23:43:00 +00001951 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1952 if (!l2tp_wq) {
1953 pr_err("alloc_workqueue failed\n");
1954 rc = -ENOMEM;
1955 goto out;
1956 }
1957
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001958 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001959
1960out:
1961 return rc;
1962}
1963
1964static void __exit l2tp_exit(void)
1965{
1966 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001967 if (l2tp_wq) {
1968 destroy_workqueue(l2tp_wq);
1969 l2tp_wq = NULL;
1970 }
James Chapmanfd558d12010-04-02 06:18:33 +00001971}
1972
1973module_init(l2tp_init);
1974module_exit(l2tp_exit);
1975
1976MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1977MODULE_DESCRIPTION("L2TP core");
1978MODULE_LICENSE("GPL");
1979MODULE_VERSION(L2TP_DRV_VERSION);
1980