blob: e91d55924d94bb0e79336bdbf698f91eaacce2b2 [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
21#include <linux/module.h>
22#include <linux/string.h>
23#include <linux/list.h>
James Chapmane02d4942010-04-02 06:19:16 +000024#include <linux/rculist.h>
James Chapmanfd558d12010-04-02 06:18:33 +000025#include <linux/uaccess.h>
26
27#include <linux/kernel.h>
28#include <linux/spinlock.h>
29#include <linux/kthread.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/errno.h>
33#include <linux/jiffies.h>
34
35#include <linux/netdevice.h>
36#include <linux/net.h>
37#include <linux/inetdevice.h>
38#include <linux/skbuff.h>
39#include <linux/init.h>
James Chapman0d767512010-04-02 06:19:00 +000040#include <linux/in.h>
James Chapmanfd558d12010-04-02 06:18:33 +000041#include <linux/ip.h>
42#include <linux/udp.h>
James Chapman0d767512010-04-02 06:19:00 +000043#include <linux/l2tp.h>
James Chapmanfd558d12010-04-02 06:18:33 +000044#include <linux/hash.h>
45#include <linux/sort.h>
46#include <linux/file.h>
47#include <linux/nsproxy.h>
48#include <net/net_namespace.h>
49#include <net/netns/generic.h>
50#include <net/dst.h>
51#include <net/ip.h>
52#include <net/udp.h>
James Chapman309795f2010-04-02 06:19:10 +000053#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000054#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000055#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000056#include <net/inet6_connection_sock.h>
57#include <net/inet_ecn.h>
58#include <net/ip6_route.h>
James Chapmanfd558d12010-04-02 06:18:33 +000059
60#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000062
63#include "l2tp_core.h"
64
65#define L2TP_DRV_VERSION "V2.0"
66
67/* L2TP header constants */
68#define L2TP_HDRFLAG_T 0x8000
69#define L2TP_HDRFLAG_L 0x4000
70#define L2TP_HDRFLAG_S 0x0800
71#define L2TP_HDRFLAG_O 0x0200
72#define L2TP_HDRFLAG_P 0x0100
73
74#define L2TP_HDR_VER_MASK 0x000F
75#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000076#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000077
78/* L2TPv3 default L2-specific sublayer */
79#define L2TP_SLFLAG_S 0x40000000
80#define L2TP_SL_SEQ_MASK 0x00ffffff
81
82#define L2TP_HDR_SIZE_SEQ 10
83#define L2TP_HDR_SIZE_NOSEQ 6
84
85/* Default trace flags */
86#define L2TP_DEFAULT_DEBUG_FLAGS 0
87
88#define PRINTK(_mask, _type, _lvl, _fmt, args...) \
89 do { \
90 if ((_mask) & (_type)) \
91 printk(_lvl "L2TP: " _fmt, ##args); \
92 } while (0)
93
94/* Private data stored for received packets in the skb.
95 */
96struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000097 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000098 u16 has_seq;
99 u16 length;
100 unsigned long expires;
101};
102
103#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
104
105static atomic_t l2tp_tunnel_count;
106static atomic_t l2tp_session_count;
107
108/* per-net private data for this module */
109static unsigned int l2tp_net_id;
110struct l2tp_net {
111 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000112 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000113 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000114 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000115};
116
stephen hemmingerfc130842010-10-21 07:50:46 +0000117static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
118static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
119static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
120
James Chapmanfd558d12010-04-02 06:18:33 +0000121static inline struct l2tp_net *l2tp_pernet(struct net *net)
122{
123 BUG_ON(!net);
124
125 return net_generic(net, l2tp_net_id);
126}
127
stephen hemmingerfc130842010-10-21 07:50:46 +0000128
129/* Tunnel reference counts. Incremented per session that is added to
130 * the tunnel.
131 */
132static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
133{
134 atomic_inc(&tunnel->ref_count);
135}
136
137static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
138{
139 if (atomic_dec_and_test(&tunnel->ref_count))
140 l2tp_tunnel_free(tunnel);
141}
142#ifdef L2TP_REFCNT_DEBUG
143#define l2tp_tunnel_inc_refcount(_t) do { \
144 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
145 l2tp_tunnel_inc_refcount_1(_t); \
146 } while (0)
147#define l2tp_tunnel_dec_refcount(_t) do { \
148 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
149 l2tp_tunnel_dec_refcount_1(_t); \
150 } while (0)
151#else
152#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
153#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
154#endif
155
James Chapmanf7faffa2010-04-02 06:18:49 +0000156/* Session hash global list for L2TPv3.
157 * The session_id SHOULD be random according to RFC3931, but several
158 * L2TP implementations use incrementing session_ids. So we do a real
159 * hash on the session_id, rather than a simple bitmask.
160 */
161static inline struct hlist_head *
162l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
163{
164 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
165
166}
167
168/* Lookup a session by id in the global session list
169 */
170static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
171{
172 struct l2tp_net *pn = l2tp_pernet(net);
173 struct hlist_head *session_list =
174 l2tp_session_id_hash_2(pn, session_id);
175 struct l2tp_session *session;
176 struct hlist_node *walk;
177
James Chapmane02d4942010-04-02 06:19:16 +0000178 rcu_read_lock_bh();
179 hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000180 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000181 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000182 return session;
183 }
184 }
James Chapmane02d4942010-04-02 06:19:16 +0000185 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000186
187 return NULL;
188}
189
James Chapmanfd558d12010-04-02 06:18:33 +0000190/* Session hash list.
191 * The session_id SHOULD be random according to RFC2661, but several
192 * L2TP implementations (Cisco and Microsoft) use incrementing
193 * session_ids. So we do a real hash on the session_id, rather than a
194 * simple bitmask.
195 */
196static inline struct hlist_head *
197l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
198{
199 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
200}
201
202/* Lookup a session by id
203 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000204struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000205{
James Chapmanf7faffa2010-04-02 06:18:49 +0000206 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000207 struct l2tp_session *session;
208 struct hlist_node *walk;
209
James Chapmanf7faffa2010-04-02 06:18:49 +0000210 /* In L2TPv3, session_ids are unique over all tunnels and we
211 * sometimes need to look them up before we know the
212 * tunnel.
213 */
214 if (tunnel == NULL)
215 return l2tp_session_find_2(net, session_id);
216
217 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000218 read_lock_bh(&tunnel->hlist_lock);
219 hlist_for_each_entry(session, walk, session_list, hlist) {
220 if (session->session_id == session_id) {
221 read_unlock_bh(&tunnel->hlist_lock);
222 return session;
223 }
224 }
225 read_unlock_bh(&tunnel->hlist_lock);
226
227 return NULL;
228}
229EXPORT_SYMBOL_GPL(l2tp_session_find);
230
231struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
232{
233 int hash;
234 struct hlist_node *walk;
235 struct l2tp_session *session;
236 int count = 0;
237
238 read_lock_bh(&tunnel->hlist_lock);
239 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
240 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
241 if (++count > nth) {
242 read_unlock_bh(&tunnel->hlist_lock);
243 return session;
244 }
245 }
246 }
247
248 read_unlock_bh(&tunnel->hlist_lock);
249
250 return NULL;
251}
252EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
253
James Chapman309795f2010-04-02 06:19:10 +0000254/* Lookup a session by interface name.
255 * This is very inefficient but is only used by management interfaces.
256 */
257struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
258{
259 struct l2tp_net *pn = l2tp_pernet(net);
260 int hash;
261 struct hlist_node *walk;
262 struct l2tp_session *session;
263
James Chapmane02d4942010-04-02 06:19:16 +0000264 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000265 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
James Chapmane02d4942010-04-02 06:19:16 +0000266 hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000267 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000268 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000269 return session;
270 }
271 }
272 }
273
James Chapmane02d4942010-04-02 06:19:16 +0000274 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000275
276 return NULL;
277}
278EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
279
James Chapmanfd558d12010-04-02 06:18:33 +0000280/* Lookup a tunnel by id
281 */
282struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
283{
284 struct l2tp_tunnel *tunnel;
285 struct l2tp_net *pn = l2tp_pernet(net);
286
James Chapmane02d4942010-04-02 06:19:16 +0000287 rcu_read_lock_bh();
288 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000289 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000290 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000291 return tunnel;
292 }
293 }
James Chapmane02d4942010-04-02 06:19:16 +0000294 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000295
296 return NULL;
297}
298EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
299
300struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
301{
302 struct l2tp_net *pn = l2tp_pernet(net);
303 struct l2tp_tunnel *tunnel;
304 int count = 0;
305
James Chapmane02d4942010-04-02 06:19:16 +0000306 rcu_read_lock_bh();
307 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000308 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000309 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000310 return tunnel;
311 }
312 }
313
James Chapmane02d4942010-04-02 06:19:16 +0000314 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000315
316 return NULL;
317}
318EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
319
320/*****************************************************************************
321 * Receive data handling
322 *****************************************************************************/
323
324/* Queue a skb in order. We come here only if the skb has an L2TP sequence
325 * number.
326 */
327static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
328{
329 struct sk_buff *skbp;
330 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000331 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000332
333 spin_lock_bh(&session->reorder_q.lock);
334 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
335 if (L2TP_SKB_CB(skbp)->ns > ns) {
336 __skb_queue_before(&session->reorder_q, skbp, skb);
337 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
338 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
339 session->name, ns, L2TP_SKB_CB(skbp)->ns,
340 skb_queue_len(&session->reorder_q));
341 session->stats.rx_oos_packets++;
342 goto out;
343 }
344 }
345
346 __skb_queue_tail(&session->reorder_q, skb);
347
348out:
349 spin_unlock_bh(&session->reorder_q.lock);
350}
351
352/* Dequeue a single skb.
353 */
354static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
355{
356 struct l2tp_tunnel *tunnel = session->tunnel;
357 int length = L2TP_SKB_CB(skb)->length;
358
359 /* We're about to requeue the skb, so return resources
360 * to its current owner (a socket receive buffer).
361 */
362 skb_orphan(skb);
363
364 tunnel->stats.rx_packets++;
365 tunnel->stats.rx_bytes += length;
366 session->stats.rx_packets++;
367 session->stats.rx_bytes += length;
368
369 if (L2TP_SKB_CB(skb)->has_seq) {
370 /* Bump our Nr */
371 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000372 if (tunnel->version == L2TP_HDR_VER_2)
373 session->nr &= 0xffff;
374 else
375 session->nr &= 0xffffff;
376
James Chapmanfd558d12010-04-02 06:18:33 +0000377 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
378 "%s: updated nr to %hu\n", session->name, session->nr);
379 }
380
381 /* call private receive handler */
382 if (session->recv_skb != NULL)
383 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
384 else
385 kfree_skb(skb);
386
387 if (session->deref)
388 (*session->deref)(session);
389}
390
391/* Dequeue skbs from the session's reorder_q, subject to packet order.
392 * Skbs that have been in the queue for too long are simply discarded.
393 */
394static void l2tp_recv_dequeue(struct l2tp_session *session)
395{
396 struct sk_buff *skb;
397 struct sk_buff *tmp;
398
399 /* If the pkt at the head of the queue has the nr that we
400 * expect to send up next, dequeue it and any other
401 * in-sequence packets behind it.
402 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000403start:
James Chapmanfd558d12010-04-02 06:18:33 +0000404 spin_lock_bh(&session->reorder_q.lock);
405 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
406 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
407 session->stats.rx_seq_discards++;
408 session->stats.rx_errors++;
409 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000410 "%s: oos pkt %u len %d discarded (too old), "
411 "waiting for %u, reorder_q_len=%d\n",
James Chapmanfd558d12010-04-02 06:18:33 +0000412 session->name, L2TP_SKB_CB(skb)->ns,
413 L2TP_SKB_CB(skb)->length, session->nr,
414 skb_queue_len(&session->reorder_q));
415 __skb_unlink(skb, &session->reorder_q);
416 kfree_skb(skb);
417 if (session->deref)
418 (*session->deref)(session);
419 continue;
420 }
421
422 if (L2TP_SKB_CB(skb)->has_seq) {
423 if (L2TP_SKB_CB(skb)->ns != session->nr) {
424 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000425 "%s: holding oos pkt %u len %d, "
426 "waiting for %u, reorder_q_len=%d\n",
James Chapmanfd558d12010-04-02 06:18:33 +0000427 session->name, L2TP_SKB_CB(skb)->ns,
428 L2TP_SKB_CB(skb)->length, session->nr,
429 skb_queue_len(&session->reorder_q));
430 goto out;
431 }
432 }
433 __skb_unlink(skb, &session->reorder_q);
434
435 /* Process the skb. We release the queue lock while we
436 * do so to let other contexts process the queue.
437 */
438 spin_unlock_bh(&session->reorder_q.lock);
439 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000440 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000441 }
442
443out:
444 spin_unlock_bh(&session->reorder_q.lock);
445}
446
447static inline int l2tp_verify_udp_checksum(struct sock *sk,
448 struct sk_buff *skb)
449{
450 struct udphdr *uh = udp_hdr(skb);
451 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000452 __wsum psum;
453
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000454 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000455 return 0;
456
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000457#if IS_ENABLED(CONFIG_IPV6)
458 if (sk->sk_family == PF_INET6) {
459 if (!uh->check) {
460 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
461 return 1;
462 }
463 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
464 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
465 &ipv6_hdr(skb)->daddr, ulen,
466 IPPROTO_UDP, skb->csum)) {
467 skb->ip_summed = CHECKSUM_UNNECESSARY;
468 return 0;
469 }
470 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
471 &ipv6_hdr(skb)->daddr,
472 skb->len, IPPROTO_UDP,
473 0));
474 } else
475#endif
476 {
477 struct inet_sock *inet;
478 if (!uh->check)
479 return 0;
480 inet = inet_sk(sk);
481 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
482 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000483
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000484 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
485 !csum_fold(csum_add(psum, skb->csum)))
486 return 0;
487 skb->csum = psum;
488 }
James Chapmanfd558d12010-04-02 06:18:33 +0000489
490 return __skb_checksum_complete(skb);
491}
492
James Chapmanf7faffa2010-04-02 06:18:49 +0000493/* Do receive processing of L2TP data frames. We handle both L2TPv2
494 * and L2TPv3 data frames here.
495 *
496 * L2TPv2 Data Message Header
497 *
498 * 0 1 2 3
499 * 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
500 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503 * | Tunnel ID | Session ID |
504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505 * | Ns (opt) | Nr (opt) |
506 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507 * | Offset Size (opt) | Offset pad... (opt)
508 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
509 *
510 * Data frames are marked by T=0. All other fields are the same as
511 * those in L2TP control frames.
512 *
513 * L2TPv3 Data Message Header
514 *
515 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
516 * | L2TP Session Header |
517 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
518 * | L2-Specific Sublayer |
519 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520 * | Tunnel Payload ...
521 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522 *
523 * L2TPv3 Session Header Over IP
524 *
525 * 0 1 2 3
526 * 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
527 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
528 * | Session ID |
529 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 * | Cookie (optional, maximum 64 bits)...
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * |
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 *
535 * L2TPv3 L2-Specific Sublayer Format
536 *
537 * 0 1 2 3
538 * 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
539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540 * |x|S|x|x|x|x|x|x| Sequence Number |
541 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542 *
543 * Cookie value, sublayer format and offset (pad) are negotiated with
544 * the peer when the session is set up. Unlike L2TPv2, we do not need
545 * to parse the packet header to determine if optional fields are
546 * present.
547 *
548 * Caller must already have parsed the frame and determined that it is
549 * a data (not control) frame before coming here. Fields up to the
550 * session-id have already been parsed and ptr points to the data
551 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000552 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000553void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
554 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
555 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000556{
James Chapmanf7faffa2010-04-02 06:18:49 +0000557 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000558 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000559 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000560
561 /* The ref count is increased since we now hold a pointer to
562 * the session. Take care to decrement the refcnt when exiting
563 * this function from now on...
564 */
565 l2tp_session_inc_refcount(session);
566 if (session->ref)
567 (*session->ref)(session);
568
James Chapmanf7faffa2010-04-02 06:18:49 +0000569 /* Parse and check optional cookie */
570 if (session->peer_cookie_len > 0) {
571 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
572 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
573 "%s: cookie mismatch (%u/%u). Discarding.\n",
574 tunnel->name, tunnel->tunnel_id, session->session_id);
575 session->stats.rx_cookie_discards++;
576 goto discard;
577 }
578 ptr += session->peer_cookie_len;
579 }
580
James Chapmanfd558d12010-04-02 06:18:33 +0000581 /* Handle the optional sequence numbers. Sequence numbers are
582 * in different places for L2TPv2 and L2TPv3.
583 *
584 * If we are the LAC, enable/disable sequence numbers under
585 * the control of the LNS. If no sequence numbers present but
586 * we were expecting them, discard frame.
587 */
588 ns = nr = 0;
589 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000590 if (tunnel->version == L2TP_HDR_VER_2) {
591 if (hdrflags & L2TP_HDRFLAG_S) {
592 ns = ntohs(*(__be16 *) ptr);
593 ptr += 2;
594 nr = ntohs(*(__be16 *) ptr);
595 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000596
James Chapmanf7faffa2010-04-02 06:18:49 +0000597 /* Store L2TP info in the skb */
598 L2TP_SKB_CB(skb)->ns = ns;
599 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000600
James Chapmanf7faffa2010-04-02 06:18:49 +0000601 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
602 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
603 session->name, ns, nr, session->nr);
604 }
605 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
606 u32 l2h = ntohl(*(__be32 *) ptr);
607
608 if (l2h & 0x40000000) {
609 ns = l2h & 0x00ffffff;
610
611 /* Store L2TP info in the skb */
612 L2TP_SKB_CB(skb)->ns = ns;
613 L2TP_SKB_CB(skb)->has_seq = 1;
614
615 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
616 "%s: recv data ns=%u, session nr=%u\n",
617 session->name, ns, session->nr);
618 }
James Chapmanfd558d12010-04-02 06:18:33 +0000619 }
620
James Chapmanf7faffa2010-04-02 06:18:49 +0000621 /* Advance past L2-specific header, if present */
622 ptr += session->l2specific_len;
623
James Chapmanfd558d12010-04-02 06:18:33 +0000624 if (L2TP_SKB_CB(skb)->has_seq) {
625 /* Received a packet with sequence numbers. If we're the LNS,
626 * check if we sre sending sequence numbers and if not,
627 * configure it so.
628 */
629 if ((!session->lns_mode) && (!session->send_seq)) {
630 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
631 "%s: requested to enable seq numbers by LNS\n",
632 session->name);
633 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000634 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000635 }
636 } else {
637 /* No sequence numbers.
638 * If user has configured mandatory sequence numbers, discard.
639 */
640 if (session->recv_seq) {
641 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
642 "%s: recv data has no seq numbers when required. "
643 "Discarding\n", session->name);
644 session->stats.rx_seq_discards++;
645 goto discard;
646 }
647
648 /* If we're the LAC and we're sending sequence numbers, the
649 * LNS has requested that we no longer send sequence numbers.
650 * If we're the LNS and we're sending sequence numbers, the
651 * LAC is broken. Discard the frame.
652 */
653 if ((!session->lns_mode) && (session->send_seq)) {
654 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
655 "%s: requested to disable seq numbers by LNS\n",
656 session->name);
657 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000658 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000659 } else if (session->send_seq) {
660 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
661 "%s: recv data has no seq numbers when required. "
662 "Discarding\n", session->name);
663 session->stats.rx_seq_discards++;
664 goto discard;
665 }
666 }
667
James Chapmanf7faffa2010-04-02 06:18:49 +0000668 /* Session data offset is handled differently for L2TPv2 and
669 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
670 * the header. For L2TPv3, the offset is negotiated using AVPs
671 * in the session setup control protocol.
672 */
673 if (tunnel->version == L2TP_HDR_VER_2) {
674 /* If offset bit set, skip it. */
675 if (hdrflags & L2TP_HDRFLAG_O) {
676 offset = ntohs(*(__be16 *)ptr);
677 ptr += 2 + offset;
678 }
679 } else
680 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000681
682 offset = ptr - optr;
683 if (!pskb_may_pull(skb, offset))
684 goto discard;
685
686 __skb_pull(skb, offset);
687
688 /* If caller wants to process the payload before we queue the
689 * packet, do so now.
690 */
691 if (payload_hook)
692 if ((*payload_hook)(skb))
693 goto discard;
694
695 /* Prepare skb for adding to the session's reorder_q. Hold
696 * packets for max reorder_timeout or 1 second if not
697 * reordering.
698 */
699 L2TP_SKB_CB(skb)->length = length;
700 L2TP_SKB_CB(skb)->expires = jiffies +
701 (session->reorder_timeout ? session->reorder_timeout : HZ);
702
703 /* Add packet to the session's receive queue. Reordering is done here, if
704 * enabled. Saved L2TP protocol info is stored in skb->sb[].
705 */
706 if (L2TP_SKB_CB(skb)->has_seq) {
707 if (session->reorder_timeout != 0) {
708 /* Packet reordering enabled. Add skb to session's
709 * reorder queue, in order of ns.
710 */
711 l2tp_recv_queue_skb(session, skb);
712 } else {
713 /* Packet reordering disabled. Discard out-of-sequence
714 * packets
715 */
716 if (L2TP_SKB_CB(skb)->ns != session->nr) {
717 session->stats.rx_seq_discards++;
718 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000719 "%s: oos pkt %u len %d discarded, "
720 "waiting for %u, reorder_q_len=%d\n",
James Chapmanfd558d12010-04-02 06:18:33 +0000721 session->name, L2TP_SKB_CB(skb)->ns,
722 L2TP_SKB_CB(skb)->length, session->nr,
723 skb_queue_len(&session->reorder_q));
724 goto discard;
725 }
726 skb_queue_tail(&session->reorder_q, skb);
727 }
728 } else {
729 /* No sequence numbers. Add the skb to the tail of the
730 * reorder queue. This ensures that it will be
731 * delivered after all previous sequenced skbs.
732 */
733 skb_queue_tail(&session->reorder_q, skb);
734 }
735
736 /* Try to dequeue as many skbs from reorder_q as we can. */
737 l2tp_recv_dequeue(session);
738
739 l2tp_session_dec_refcount(session);
740
James Chapmanf7faffa2010-04-02 06:18:49 +0000741 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000742
743discard:
744 session->stats.rx_errors++;
745 kfree_skb(skb);
746
747 if (session->deref)
748 (*session->deref)(session);
749
750 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000751}
752EXPORT_SYMBOL(l2tp_recv_common);
753
754/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
755 * here. The skb is not on a list when we get here.
756 * Returns 0 if the packet was a data packet and was successfully passed on.
757 * Returns 1 if the packet was not a good data packet and could not be
758 * forwarded. All such packets are passed up to userspace to deal with.
759 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000760static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
761 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000762{
763 struct l2tp_session *session = NULL;
764 unsigned char *ptr, *optr;
765 u16 hdrflags;
766 u32 tunnel_id, session_id;
767 int offset;
768 u16 version;
769 int length;
770
771 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
772 goto discard_bad_csum;
773
774 /* UDP always verifies the packet length. */
775 __skb_pull(skb, sizeof(struct udphdr));
776
777 /* Short packet? */
778 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
779 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
780 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
781 goto error;
782 }
783
James Chapmanf7faffa2010-04-02 06:18:49 +0000784 /* Trace packet contents, if enabled */
785 if (tunnel->debug & L2TP_MSG_DATA) {
786 length = min(32u, skb->len);
787 if (!pskb_may_pull(skb, length))
788 goto error;
789
790 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
791
792 offset = 0;
793 do {
Eric Dumazete50e7052011-11-08 13:59:44 -0500794 printk(" %02X", skb->data[offset]);
James Chapmanf7faffa2010-04-02 06:18:49 +0000795 } while (++offset < length);
796
797 printk("\n");
798 }
799
Eric Dumazete50e7052011-11-08 13:59:44 -0500800 /* Point to L2TP header */
801 optr = ptr = skb->data;
802
James Chapmanf7faffa2010-04-02 06:18:49 +0000803 /* Get L2TP header flags */
804 hdrflags = ntohs(*(__be16 *) ptr);
805
806 /* Check protocol version */
807 version = hdrflags & L2TP_HDR_VER_MASK;
808 if (version != tunnel->version) {
809 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
810 "%s: recv protocol version mismatch: got %d expected %d\n",
811 tunnel->name, version, tunnel->version);
812 goto error;
813 }
814
815 /* Get length of L2TP packet */
816 length = skb->len;
817
818 /* If type is control packet, it is handled by userspace. */
819 if (hdrflags & L2TP_HDRFLAG_T) {
820 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
821 "%s: recv control packet, len=%d\n", tunnel->name, length);
822 goto error;
823 }
824
825 /* Skip flags */
826 ptr += 2;
827
828 if (tunnel->version == L2TP_HDR_VER_2) {
829 /* If length is present, skip it */
830 if (hdrflags & L2TP_HDRFLAG_L)
831 ptr += 2;
832
833 /* Extract tunnel and session ID */
834 tunnel_id = ntohs(*(__be16 *) ptr);
835 ptr += 2;
836 session_id = ntohs(*(__be16 *) ptr);
837 ptr += 2;
838 } else {
839 ptr += 2; /* skip reserved bits */
840 tunnel_id = tunnel->tunnel_id;
841 session_id = ntohl(*(__be32 *) ptr);
842 ptr += 4;
843 }
844
845 /* Find the session context */
846 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000847 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000848 /* Not found? Pass to userspace to deal with */
849 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
850 "%s: no session found (%u/%u). Passing up.\n",
851 tunnel->name, tunnel_id, session_id);
852 goto error;
853 }
854
855 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000856
857 return 0;
858
859discard_bad_csum:
860 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
861 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
862 tunnel->stats.rx_errors++;
863 kfree_skb(skb);
864
865 return 0;
866
867error:
868 /* Put UDP header back */
869 __skb_push(skb, sizeof(struct udphdr));
870
871 return 1;
872}
James Chapmanfd558d12010-04-02 06:18:33 +0000873
874/* UDP encapsulation receive handler. See net/ipv4/udp.c.
875 * Return codes:
876 * 0 : success.
877 * <0: error
878 * >0: skb should be passed up to userspace as UDP.
879 */
880int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
881{
882 struct l2tp_tunnel *tunnel;
883
884 tunnel = l2tp_sock_to_tunnel(sk);
885 if (tunnel == NULL)
886 goto pass_up;
887
888 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
889 "%s: received %d bytes\n", tunnel->name, skb->len);
890
891 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
892 goto pass_up_put;
893
894 sock_put(sk);
895 return 0;
896
897pass_up_put:
898 sock_put(sk);
899pass_up:
900 return 1;
901}
902EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
903
904/************************************************************************
905 * Transmit handling
906 ***********************************************************************/
907
908/* Build an L2TP header for the session into the buffer provided.
909 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000910static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000911{
James Chapmanf7faffa2010-04-02 06:18:49 +0000912 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000913 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000914 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000915 u16 flags = L2TP_HDR_VER_2;
916 u32 tunnel_id = tunnel->peer_tunnel_id;
917 u32 session_id = session->peer_session_id;
918
919 if (session->send_seq)
920 flags |= L2TP_HDRFLAG_S;
921
922 /* Setup L2TP header. */
923 *bufp++ = htons(flags);
924 *bufp++ = htons(tunnel_id);
925 *bufp++ = htons(session_id);
926 if (session->send_seq) {
927 *bufp++ = htons(session->ns);
928 *bufp++ = 0;
929 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000930 session->ns &= 0xffff;
James Chapmanfd558d12010-04-02 06:18:33 +0000931 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000932 "%s: updated ns to %u\n", session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000933 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000934
935 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000936}
937
James Chapmanf7faffa2010-04-02 06:18:49 +0000938static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000939{
James Chapman0d767512010-04-02 06:19:00 +0000940 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000941 char *bufp = buf;
942 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000943
James Chapman0d767512010-04-02 06:19:00 +0000944 /* Setup L2TP header. The header differs slightly for UDP and
945 * IP encapsulations. For UDP, there is 4 bytes of flags.
946 */
947 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
948 u16 flags = L2TP_HDR_VER_3;
949 *((__be16 *) bufp) = htons(flags);
950 bufp += 2;
951 *((__be16 *) bufp) = 0;
952 bufp += 2;
953 }
954
James Chapmanf7faffa2010-04-02 06:18:49 +0000955 *((__be32 *) bufp) = htonl(session->peer_session_id);
956 bufp += 4;
957 if (session->cookie_len) {
958 memcpy(bufp, &session->cookie[0], session->cookie_len);
959 bufp += session->cookie_len;
960 }
961 if (session->l2specific_len) {
962 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
963 u32 l2h = 0;
964 if (session->send_seq) {
965 l2h = 0x40000000 | session->ns;
966 session->ns++;
967 session->ns &= 0xffffff;
968 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
969 "%s: updated ns to %u\n", session->name, session->ns);
970 }
971
972 *((__be32 *) bufp) = htonl(l2h);
973 }
974 bufp += session->l2specific_len;
975 }
976 if (session->offset)
977 bufp += session->offset;
978
979 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000980}
James Chapmanfd558d12010-04-02 06:18:33 +0000981
stephen hemmingerfc130842010-10-21 07:50:46 +0000982static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -0700983 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +0000984{
985 struct l2tp_tunnel *tunnel = session->tunnel;
986 unsigned int len = skb->len;
987 int error;
988
989 /* Debug */
990 if (session->send_seq)
991 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000992 "%s: send %Zd bytes, ns=%u\n", session->name,
James Chapmanfd558d12010-04-02 06:18:33 +0000993 data_len, session->ns - 1);
994 else
995 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
996 "%s: send %Zd bytes\n", session->name, data_len);
997
998 if (session->debug & L2TP_MSG_DATA) {
999 int i;
James Chapman0d767512010-04-02 06:19:00 +00001000 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1001 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001002
1003 printk(KERN_DEBUG "%s: xmit:", session->name);
James Chapman0d767512010-04-02 06:19:00 +00001004 for (i = 0; i < (len - uhlen); i++) {
James Chapmanfd558d12010-04-02 06:18:33 +00001005 printk(" %02X", *datap++);
1006 if (i == 31) {
1007 printk(" ...");
1008 break;
1009 }
1010 }
1011 printk("\n");
1012 }
1013
1014 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001015 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001016#if IS_ENABLED(CONFIG_IPV6)
1017 if (skb->sk->sk_family == PF_INET6)
1018 error = inet6_csk_xmit(skb, NULL);
1019 else
1020#endif
1021 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001022
1023 /* Update stats */
1024 if (error >= 0) {
1025 tunnel->stats.tx_packets++;
1026 tunnel->stats.tx_bytes += len;
1027 session->stats.tx_packets++;
1028 session->stats.tx_bytes += len;
1029 } else {
1030 tunnel->stats.tx_errors++;
1031 session->stats.tx_errors++;
1032 }
1033
1034 return 0;
1035}
James Chapmanfd558d12010-04-02 06:18:33 +00001036
1037/* Automatically called when the skb is freed.
1038 */
1039static void l2tp_sock_wfree(struct sk_buff *skb)
1040{
1041 sock_put(skb->sk);
1042}
1043
1044/* For data skbs that we transmit, we associate with the tunnel socket
1045 * but don't do accounting.
1046 */
1047static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1048{
1049 sock_hold(sk);
1050 skb->sk = sk;
1051 skb->destructor = l2tp_sock_wfree;
1052}
1053
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001054#if IS_ENABLED(CONFIG_IPV6)
1055static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1056 int udp_len)
1057{
1058 struct ipv6_pinfo *np = inet6_sk(sk);
1059 struct udphdr *uh = udp_hdr(skb);
1060
1061 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1062 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1063 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1064 skb->ip_summed = CHECKSUM_UNNECESSARY;
1065 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1066 IPPROTO_UDP, csum);
1067 if (uh->check == 0)
1068 uh->check = CSUM_MANGLED_0;
1069 } else {
1070 skb->ip_summed = CHECKSUM_PARTIAL;
1071 skb->csum_start = skb_transport_header(skb) - skb->head;
1072 skb->csum_offset = offsetof(struct udphdr, check);
1073 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1074 udp_len, IPPROTO_UDP, 0);
1075 }
1076}
1077#endif
1078
James Chapmanfd558d12010-04-02 06:18:33 +00001079/* If caller requires the skb to have a ppp header, the header must be
1080 * inserted in the skb data before calling this function.
1081 */
1082int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1083{
1084 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001085 struct l2tp_tunnel *tunnel = session->tunnel;
1086 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001087 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001088 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001089 struct inet_sock *inet;
1090 __wsum csum;
1091 int old_headroom;
1092 int new_headroom;
1093 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001094 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1095 int udp_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001096
1097 /* Check that there's enough headroom in the skb to insert IP,
1098 * UDP and L2TP headers. If not enough, expand it to
1099 * make room. Adjust truesize.
1100 */
1101 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001102 uhlen + hdr_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001103 old_headroom = skb_headroom(skb);
Eric Dumazet835acf52011-10-07 05:35:46 +00001104 if (skb_cow_head(skb, headroom)) {
1105 dev_kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001106 goto abort;
Eric Dumazet835acf52011-10-07 05:35:46 +00001107 }
James Chapmanfd558d12010-04-02 06:18:33 +00001108
1109 new_headroom = skb_headroom(skb);
1110 skb_orphan(skb);
1111 skb->truesize += new_headroom - old_headroom;
1112
1113 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001114 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001115
James Chapman0d767512010-04-02 06:19:00 +00001116 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001117 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1118 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1119 IPSKB_REROUTED);
1120 nf_reset(skb);
1121
David S. Miller6af88da2011-05-08 13:45:20 -07001122 bh_lock_sock(sk);
1123 if (sock_owned_by_user(sk)) {
1124 dev_kfree_skb(skb);
1125 goto out_unlock;
1126 }
1127
James Chapmanfd558d12010-04-02 06:18:33 +00001128 /* Get routing info from the tunnel socket */
1129 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001130 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001131
David S. Millerd9d8da82011-05-06 22:23:20 -07001132 inet = inet_sk(sk);
1133 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001134 switch (tunnel->encap) {
1135 case L2TP_ENCAPTYPE_UDP:
1136 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001137 __skb_push(skb, sizeof(*uh));
1138 skb_reset_transport_header(skb);
1139 uh = udp_hdr(skb);
1140 uh->source = inet->inet_sport;
1141 uh->dest = inet->inet_dport;
1142 udp_len = uhlen + hdr_len + data_len;
1143 uh->len = htons(udp_len);
1144 uh->check = 0;
1145
1146 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001147#if IS_ENABLED(CONFIG_IPV6)
1148 if (sk->sk_family == PF_INET6)
1149 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1150 else
1151#endif
James Chapman0d767512010-04-02 06:19:00 +00001152 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1153 skb->ip_summed = CHECKSUM_NONE;
1154 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1155 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1156 skb->ip_summed = CHECKSUM_COMPLETE;
1157 csum = skb_checksum(skb, 0, udp_len, 0);
1158 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1159 inet->inet_daddr,
1160 udp_len, IPPROTO_UDP, csum);
1161 if (uh->check == 0)
1162 uh->check = CSUM_MANGLED_0;
1163 } else {
1164 skb->ip_summed = CHECKSUM_PARTIAL;
1165 skb->csum_start = skb_transport_header(skb) - skb->head;
1166 skb->csum_offset = offsetof(struct udphdr, check);
1167 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1168 inet->inet_daddr,
1169 udp_len, IPPROTO_UDP, 0);
1170 }
1171 break;
1172
1173 case L2TP_ENCAPTYPE_IP:
1174 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001175 }
1176
James Chapman0d767512010-04-02 06:19:00 +00001177 l2tp_skb_set_owner_w(skb, sk);
1178
David S. Millerd9d8da82011-05-06 22:23:20 -07001179 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001180out_unlock:
1181 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001182
1183abort:
1184 return 0;
1185}
1186EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1187
1188/*****************************************************************************
1189 * Tinnel and session create/destroy.
1190 *****************************************************************************/
1191
1192/* Tunnel socket destruct hook.
1193 * The tunnel context is deleted only when all session sockets have been
1194 * closed.
1195 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001196static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001197{
1198 struct l2tp_tunnel *tunnel;
1199
1200 tunnel = sk->sk_user_data;
1201 if (tunnel == NULL)
1202 goto end;
1203
1204 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1205 "%s: closing...\n", tunnel->name);
1206
1207 /* Close all sessions */
1208 l2tp_tunnel_closeall(tunnel);
1209
James Chapman0d767512010-04-02 06:19:00 +00001210 switch (tunnel->encap) {
1211 case L2TP_ENCAPTYPE_UDP:
1212 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1213 (udp_sk(sk))->encap_type = 0;
1214 (udp_sk(sk))->encap_rcv = NULL;
1215 break;
1216 case L2TP_ENCAPTYPE_IP:
1217 break;
1218 }
James Chapmanfd558d12010-04-02 06:18:33 +00001219
1220 /* Remove hooks into tunnel socket */
1221 tunnel->sock = NULL;
1222 sk->sk_destruct = tunnel->old_sk_destruct;
1223 sk->sk_user_data = NULL;
1224
1225 /* Call the original destructor */
1226 if (sk->sk_destruct)
1227 (*sk->sk_destruct)(sk);
1228
1229 /* We're finished with the socket */
1230 l2tp_tunnel_dec_refcount(tunnel);
1231
1232end:
1233 return;
1234}
James Chapmanfd558d12010-04-02 06:18:33 +00001235
1236/* When the tunnel is closed, all the attached sessions need to go too.
1237 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001238static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001239{
1240 int hash;
1241 struct hlist_node *walk;
1242 struct hlist_node *tmp;
1243 struct l2tp_session *session;
1244
1245 BUG_ON(tunnel == NULL);
1246
1247 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1248 "%s: closing all sessions...\n", tunnel->name);
1249
1250 write_lock_bh(&tunnel->hlist_lock);
1251 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1252again:
1253 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1254 session = hlist_entry(walk, struct l2tp_session, hlist);
1255
1256 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1257 "%s: closing session\n", session->name);
1258
1259 hlist_del_init(&session->hlist);
1260
1261 /* Since we should hold the sock lock while
1262 * doing any unbinding, we need to release the
1263 * lock we're holding before taking that lock.
1264 * Hold a reference to the sock so it doesn't
1265 * disappear as we're jumping between locks.
1266 */
1267 if (session->ref != NULL)
1268 (*session->ref)(session);
1269
1270 write_unlock_bh(&tunnel->hlist_lock);
1271
James Chapmanf7faffa2010-04-02 06:18:49 +00001272 if (tunnel->version != L2TP_HDR_VER_2) {
1273 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1274
James Chapmane02d4942010-04-02 06:19:16 +00001275 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1276 hlist_del_init_rcu(&session->global_hlist);
1277 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1278 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001279 }
1280
James Chapmanfd558d12010-04-02 06:18:33 +00001281 if (session->session_close != NULL)
1282 (*session->session_close)(session);
1283
1284 if (session->deref != NULL)
1285 (*session->deref)(session);
1286
1287 write_lock_bh(&tunnel->hlist_lock);
1288
1289 /* Now restart from the beginning of this hash
1290 * chain. We always remove a session from the
1291 * list so we are guaranteed to make forward
1292 * progress.
1293 */
1294 goto again;
1295 }
1296 }
1297 write_unlock_bh(&tunnel->hlist_lock);
1298}
James Chapmanfd558d12010-04-02 06:18:33 +00001299
1300/* Really kill the tunnel.
1301 * Come here only when all sessions have been cleared from the tunnel.
1302 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001303static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001304{
1305 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1306
1307 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1308 BUG_ON(tunnel->sock != NULL);
1309
1310 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1311 "%s: free...\n", tunnel->name);
1312
1313 /* Remove from tunnel list */
James Chapmane02d4942010-04-02 06:19:16 +00001314 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1315 list_del_rcu(&tunnel->list);
1316 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1317 synchronize_rcu();
James Chapmanfd558d12010-04-02 06:18:33 +00001318
1319 atomic_dec(&l2tp_tunnel_count);
1320 kfree(tunnel);
1321}
James Chapmanfd558d12010-04-02 06:18:33 +00001322
James Chapman789a4a22010-04-02 06:19:40 +00001323/* Create a socket for the tunnel, if one isn't set up by
1324 * userspace. This is used for static tunnels where there is no
1325 * managing L2TP daemon.
1326 */
1327static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1328{
1329 int err = -EINVAL;
1330 struct sockaddr_in udp_addr;
1331 struct sockaddr_l2tpip ip_addr;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001332 struct socket *sock = NULL;
James Chapman789a4a22010-04-02 06:19:40 +00001333
1334 switch (cfg->encap) {
1335 case L2TP_ENCAPTYPE_UDP:
1336 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1337 if (err < 0)
1338 goto out;
1339
1340 sock = *sockp;
1341
1342 memset(&udp_addr, 0, sizeof(udp_addr));
1343 udp_addr.sin_family = AF_INET;
1344 udp_addr.sin_addr = cfg->local_ip;
1345 udp_addr.sin_port = htons(cfg->local_udp_port);
1346 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1347 if (err < 0)
1348 goto out;
1349
1350 udp_addr.sin_family = AF_INET;
1351 udp_addr.sin_addr = cfg->peer_ip;
1352 udp_addr.sin_port = htons(cfg->peer_udp_port);
1353 err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1354 if (err < 0)
1355 goto out;
1356
1357 if (!cfg->use_udp_checksums)
1358 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1359
1360 break;
1361
1362 case L2TP_ENCAPTYPE_IP:
1363 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1364 if (err < 0)
1365 goto out;
1366
1367 sock = *sockp;
1368
1369 memset(&ip_addr, 0, sizeof(ip_addr));
1370 ip_addr.l2tp_family = AF_INET;
1371 ip_addr.l2tp_addr = cfg->local_ip;
1372 ip_addr.l2tp_conn_id = tunnel_id;
1373 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1374 if (err < 0)
1375 goto out;
1376
1377 ip_addr.l2tp_family = AF_INET;
1378 ip_addr.l2tp_addr = cfg->peer_ip;
1379 ip_addr.l2tp_conn_id = peer_tunnel_id;
1380 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1381 if (err < 0)
1382 goto out;
1383
1384 break;
1385
1386 default:
1387 goto out;
1388 }
1389
1390out:
1391 if ((err < 0) && sock) {
1392 sock_release(sock);
1393 *sockp = NULL;
1394 }
1395
1396 return err;
1397}
1398
James Chapmanfd558d12010-04-02 06:18:33 +00001399int 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)
1400{
1401 struct l2tp_tunnel *tunnel = NULL;
1402 int err;
1403 struct socket *sock = NULL;
1404 struct sock *sk = NULL;
1405 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001406 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001407
1408 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001409 * the userspace L2TP daemon. If not specified, create a
1410 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001411 */
James Chapman789a4a22010-04-02 06:19:40 +00001412 if (fd < 0) {
1413 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1414 if (err < 0)
1415 goto err;
1416 } else {
1417 err = -EBADF;
1418 sock = sockfd_lookup(fd, &err);
1419 if (!sock) {
1420 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1421 tunnel_id, fd, err);
1422 goto err;
1423 }
James Chapmanfd558d12010-04-02 06:18:33 +00001424 }
1425
1426 sk = sock->sk;
1427
James Chapman0d767512010-04-02 06:19:00 +00001428 if (cfg != NULL)
1429 encap = cfg->encap;
1430
James Chapmanfd558d12010-04-02 06:18:33 +00001431 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001432 switch (encap) {
1433 case L2TP_ENCAPTYPE_UDP:
1434 err = -EPROTONOSUPPORT;
1435 if (sk->sk_protocol != IPPROTO_UDP) {
1436 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1437 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1438 goto err;
1439 }
1440 break;
1441 case L2TP_ENCAPTYPE_IP:
1442 err = -EPROTONOSUPPORT;
1443 if (sk->sk_protocol != IPPROTO_L2TP) {
1444 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1445 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1446 goto err;
1447 }
1448 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001449 }
1450
1451 /* Check if this socket has already been prepped */
1452 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1453 if (tunnel != NULL) {
1454 /* This socket has already been prepped */
1455 err = -EBUSY;
1456 goto err;
1457 }
1458
James Chapmanfd558d12010-04-02 06:18:33 +00001459 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1460 if (tunnel == NULL) {
1461 err = -ENOMEM;
1462 goto err;
1463 }
1464
1465 tunnel->version = version;
1466 tunnel->tunnel_id = tunnel_id;
1467 tunnel->peer_tunnel_id = peer_tunnel_id;
1468 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1469
1470 tunnel->magic = L2TP_TUNNEL_MAGIC;
1471 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1472 rwlock_init(&tunnel->hlist_lock);
1473
1474 /* The net we belong to */
1475 tunnel->l2tp_net = net;
1476 pn = l2tp_pernet(net);
1477
James Chapman0d767512010-04-02 06:19:00 +00001478 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001479 tunnel->debug = cfg->debug;
1480
1481 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001482 tunnel->encap = encap;
1483 if (encap == L2TP_ENCAPTYPE_UDP) {
1484 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1485 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1486 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001487#if IS_ENABLED(CONFIG_IPV6)
1488 if (sk->sk_family == PF_INET6)
1489 udpv6_encap_enable();
1490 else
1491#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001492 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001493 }
James Chapmanfd558d12010-04-02 06:18:33 +00001494
1495 sk->sk_user_data = tunnel;
1496
1497 /* Hook on the tunnel socket destructor so that we can cleanup
1498 * if the tunnel socket goes away.
1499 */
1500 tunnel->old_sk_destruct = sk->sk_destruct;
1501 sk->sk_destruct = &l2tp_tunnel_destruct;
1502 tunnel->sock = sk;
1503 sk->sk_allocation = GFP_ATOMIC;
1504
1505 /* Add tunnel to our list */
1506 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001507 atomic_inc(&l2tp_tunnel_count);
1508
1509 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001510 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001511 */
1512 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001513 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1514 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1515 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001516
1517 err = 0;
1518err:
1519 if (tunnelp)
1520 *tunnelp = tunnel;
1521
James Chapman789a4a22010-04-02 06:19:40 +00001522 /* If tunnel's socket was created by the kernel, it doesn't
1523 * have a file.
1524 */
1525 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001526 sockfd_put(sock);
1527
1528 return err;
1529}
1530EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1531
James Chapman309795f2010-04-02 06:19:10 +00001532/* This function is used by the netlink TUNNEL_DELETE command.
1533 */
1534int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1535{
1536 int err = 0;
James Chapman789a4a22010-04-02 06:19:40 +00001537 struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
James Chapman309795f2010-04-02 06:19:10 +00001538
1539 /* Force the tunnel socket to close. This will eventually
1540 * cause the tunnel to be deleted via the normal socket close
1541 * mechanisms when userspace closes the tunnel socket.
1542 */
James Chapman789a4a22010-04-02 06:19:40 +00001543 if (sock != NULL) {
1544 err = inet_shutdown(sock, 2);
1545
1546 /* If the tunnel's socket was created by the kernel,
1547 * close the socket here since the socket was not
1548 * created by userspace.
1549 */
1550 if (sock->file == NULL)
1551 err = inet_release(sock);
1552 }
James Chapman309795f2010-04-02 06:19:10 +00001553
1554 return err;
1555}
1556EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1557
James Chapmanfd558d12010-04-02 06:18:33 +00001558/* Really kill the session.
1559 */
1560void l2tp_session_free(struct l2tp_session *session)
1561{
1562 struct l2tp_tunnel *tunnel;
1563
1564 BUG_ON(atomic_read(&session->ref_count) != 0);
1565
1566 tunnel = session->tunnel;
1567 if (tunnel != NULL) {
1568 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1569
1570 /* Delete the session from the hash */
1571 write_lock_bh(&tunnel->hlist_lock);
1572 hlist_del_init(&session->hlist);
1573 write_unlock_bh(&tunnel->hlist_lock);
1574
James Chapmanf7faffa2010-04-02 06:18:49 +00001575 /* Unlink from the global hash if not L2TPv2 */
1576 if (tunnel->version != L2TP_HDR_VER_2) {
1577 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1578
James Chapmane02d4942010-04-02 06:19:16 +00001579 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1580 hlist_del_init_rcu(&session->global_hlist);
1581 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1582 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001583 }
1584
James Chapmanfd558d12010-04-02 06:18:33 +00001585 if (session->session_id != 0)
1586 atomic_dec(&l2tp_session_count);
1587
1588 sock_put(tunnel->sock);
1589
1590 /* This will delete the tunnel context if this
1591 * is the last session on the tunnel.
1592 */
1593 session->tunnel = NULL;
1594 l2tp_tunnel_dec_refcount(tunnel);
1595 }
1596
1597 kfree(session);
1598
1599 return;
1600}
1601EXPORT_SYMBOL_GPL(l2tp_session_free);
1602
James Chapman309795f2010-04-02 06:19:10 +00001603/* This function is used by the netlink SESSION_DELETE command and by
1604 pseudowire modules.
1605 */
1606int l2tp_session_delete(struct l2tp_session *session)
1607{
1608 if (session->session_close != NULL)
1609 (*session->session_close)(session);
1610
1611 l2tp_session_dec_refcount(session);
1612
1613 return 0;
1614}
1615EXPORT_SYMBOL_GPL(l2tp_session_delete);
1616
1617
James Chapmanf7faffa2010-04-02 06:18:49 +00001618/* We come here whenever a session's send_seq, cookie_len or
1619 * l2specific_len parameters are set.
1620 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001621static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001622{
1623 if (version == L2TP_HDR_VER_2) {
1624 session->hdr_len = 6;
1625 if (session->send_seq)
1626 session->hdr_len += 4;
1627 } else {
James Chapman0d767512010-04-02 06:19:00 +00001628 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1629 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1630 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001631 }
1632
1633}
James Chapmanf7faffa2010-04-02 06:18:49 +00001634
James Chapmanfd558d12010-04-02 06:18:33 +00001635struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1636{
1637 struct l2tp_session *session;
1638
1639 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1640 if (session != NULL) {
1641 session->magic = L2TP_SESSION_MAGIC;
1642 session->tunnel = tunnel;
1643
1644 session->session_id = session_id;
1645 session->peer_session_id = peer_session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +00001646 session->nr = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001647
1648 sprintf(&session->name[0], "sess %u/%u",
1649 tunnel->tunnel_id, session->session_id);
1650
1651 skb_queue_head_init(&session->reorder_q);
1652
1653 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001654 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001655
1656 /* Inherit debug options from tunnel */
1657 session->debug = tunnel->debug;
1658
1659 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001660 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001661 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001662 session->mtu = cfg->mtu;
1663 session->mru = cfg->mru;
1664 session->send_seq = cfg->send_seq;
1665 session->recv_seq = cfg->recv_seq;
1666 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001667 session->reorder_timeout = cfg->reorder_timeout;
1668 session->offset = cfg->offset;
1669 session->l2specific_type = cfg->l2specific_type;
1670 session->l2specific_len = cfg->l2specific_len;
1671 session->cookie_len = cfg->cookie_len;
1672 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1673 session->peer_cookie_len = cfg->peer_cookie_len;
1674 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001675 }
1676
James Chapmanf7faffa2010-04-02 06:18:49 +00001677 if (tunnel->version == L2TP_HDR_VER_2)
1678 session->build_header = l2tp_build_l2tpv2_header;
1679 else
1680 session->build_header = l2tp_build_l2tpv3_header;
1681
1682 l2tp_session_set_header_len(session, tunnel->version);
1683
James Chapmanfd558d12010-04-02 06:18:33 +00001684 /* Bump the reference count. The session context is deleted
1685 * only when this drops to zero.
1686 */
1687 l2tp_session_inc_refcount(session);
1688 l2tp_tunnel_inc_refcount(tunnel);
1689
1690 /* Ensure tunnel socket isn't deleted */
1691 sock_hold(tunnel->sock);
1692
1693 /* Add session to the tunnel's hash list */
1694 write_lock_bh(&tunnel->hlist_lock);
1695 hlist_add_head(&session->hlist,
1696 l2tp_session_id_hash(tunnel, session_id));
1697 write_unlock_bh(&tunnel->hlist_lock);
1698
James Chapmanf7faffa2010-04-02 06:18:49 +00001699 /* And to the global session list if L2TPv3 */
1700 if (tunnel->version != L2TP_HDR_VER_2) {
1701 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1702
James Chapmane02d4942010-04-02 06:19:16 +00001703 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1704 hlist_add_head_rcu(&session->global_hlist,
1705 l2tp_session_id_hash_2(pn, session_id));
1706 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001707 }
1708
James Chapmanfd558d12010-04-02 06:18:33 +00001709 /* Ignore management session in session count value */
1710 if (session->session_id != 0)
1711 atomic_inc(&l2tp_session_count);
1712 }
1713
1714 return session;
1715}
1716EXPORT_SYMBOL_GPL(l2tp_session_create);
1717
1718/*****************************************************************************
1719 * Init and cleanup
1720 *****************************************************************************/
1721
1722static __net_init int l2tp_init_net(struct net *net)
1723{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001724 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001725 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001726
James Chapmanfd558d12010-04-02 06:18:33 +00001727 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001728 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001729
James Chapmanf7faffa2010-04-02 06:18:49 +00001730 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1731 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1732
James Chapmane02d4942010-04-02 06:19:16 +00001733 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001734
James Chapmanfd558d12010-04-02 06:18:33 +00001735 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001736}
1737
1738static struct pernet_operations l2tp_net_ops = {
1739 .init = l2tp_init_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001740 .id = &l2tp_net_id,
1741 .size = sizeof(struct l2tp_net),
1742};
1743
1744static int __init l2tp_init(void)
1745{
1746 int rc = 0;
1747
1748 rc = register_pernet_device(&l2tp_net_ops);
1749 if (rc)
1750 goto out;
1751
1752 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1753
1754out:
1755 return rc;
1756}
1757
1758static void __exit l2tp_exit(void)
1759{
1760 unregister_pernet_device(&l2tp_net_ops);
1761}
1762
1763module_init(l2tp_init);
1764module_exit(l2tp_exit);
1765
1766MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1767MODULE_DESCRIPTION("L2TP core");
1768MODULE_LICENSE("GPL");
1769MODULE_VERSION(L2TP_DRV_VERSION);
1770