blob: 55fc569c8170c22270111b22729dfa1aead909c8 [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>
David S. Millerd499bd22012-04-30 13:21:28 -040059#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000060
61#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000063
64#include "l2tp_core.h"
65
66#define L2TP_DRV_VERSION "V2.0"
67
68/* L2TP header constants */
69#define L2TP_HDRFLAG_T 0x8000
70#define L2TP_HDRFLAG_L 0x4000
71#define L2TP_HDRFLAG_S 0x0800
72#define L2TP_HDRFLAG_O 0x0200
73#define L2TP_HDRFLAG_P 0x0100
74
75#define L2TP_HDR_VER_MASK 0x000F
76#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000077#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000078
79/* L2TPv3 default L2-specific sublayer */
80#define L2TP_SLFLAG_S 0x40000000
81#define L2TP_SL_SEQ_MASK 0x00ffffff
82
83#define L2TP_HDR_SIZE_SEQ 10
84#define L2TP_HDR_SIZE_NOSEQ 6
85
86/* Default trace flags */
87#define L2TP_DEFAULT_DEBUG_FLAGS 0
88
89#define PRINTK(_mask, _type, _lvl, _fmt, args...) \
90 do { \
91 if ((_mask) & (_type)) \
92 printk(_lvl "L2TP: " _fmt, ##args); \
93 } while (0)
94
95/* Private data stored for received packets in the skb.
96 */
97struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000098 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000099 u16 has_seq;
100 u16 length;
101 unsigned long expires;
102};
103
104#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
105
106static atomic_t l2tp_tunnel_count;
107static atomic_t l2tp_session_count;
108
109/* per-net private data for this module */
110static unsigned int l2tp_net_id;
111struct l2tp_net {
112 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000113 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000114 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000115 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000116};
117
stephen hemmingerfc130842010-10-21 07:50:46 +0000118static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
119static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
120static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
121
James Chapmanfd558d12010-04-02 06:18:33 +0000122static inline struct l2tp_net *l2tp_pernet(struct net *net)
123{
124 BUG_ON(!net);
125
126 return net_generic(net, l2tp_net_id);
127}
128
stephen hemmingerfc130842010-10-21 07:50:46 +0000129
130/* Tunnel reference counts. Incremented per session that is added to
131 * the tunnel.
132 */
133static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 atomic_inc(&tunnel->ref_count);
136}
137
138static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139{
140 if (atomic_dec_and_test(&tunnel->ref_count))
141 l2tp_tunnel_free(tunnel);
142}
143#ifdef L2TP_REFCNT_DEBUG
144#define l2tp_tunnel_inc_refcount(_t) do { \
145 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
146 l2tp_tunnel_inc_refcount_1(_t); \
147 } while (0)
148#define l2tp_tunnel_dec_refcount(_t) do { \
149 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
150 l2tp_tunnel_dec_refcount_1(_t); \
151 } while (0)
152#else
153#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
154#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
155#endif
156
James Chapmanf7faffa2010-04-02 06:18:49 +0000157/* Session hash global list for L2TPv3.
158 * The session_id SHOULD be random according to RFC3931, but several
159 * L2TP implementations use incrementing session_ids. So we do a real
160 * hash on the session_id, rather than a simple bitmask.
161 */
162static inline struct hlist_head *
163l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
164{
165 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
166
167}
168
169/* Lookup a session by id in the global session list
170 */
171static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
172{
173 struct l2tp_net *pn = l2tp_pernet(net);
174 struct hlist_head *session_list =
175 l2tp_session_id_hash_2(pn, session_id);
176 struct l2tp_session *session;
177 struct hlist_node *walk;
178
James Chapmane02d4942010-04-02 06:19:16 +0000179 rcu_read_lock_bh();
180 hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000181 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000182 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000183 return session;
184 }
185 }
James Chapmane02d4942010-04-02 06:19:16 +0000186 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000187
188 return NULL;
189}
190
James Chapmanfd558d12010-04-02 06:18:33 +0000191/* Session hash list.
192 * The session_id SHOULD be random according to RFC2661, but several
193 * L2TP implementations (Cisco and Microsoft) use incrementing
194 * session_ids. So we do a real hash on the session_id, rather than a
195 * simple bitmask.
196 */
197static inline struct hlist_head *
198l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
199{
200 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
201}
202
203/* Lookup a session by id
204 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000205struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000206{
James Chapmanf7faffa2010-04-02 06:18:49 +0000207 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000208 struct l2tp_session *session;
209 struct hlist_node *walk;
210
James Chapmanf7faffa2010-04-02 06:18:49 +0000211 /* In L2TPv3, session_ids are unique over all tunnels and we
212 * sometimes need to look them up before we know the
213 * tunnel.
214 */
215 if (tunnel == NULL)
216 return l2tp_session_find_2(net, session_id);
217
218 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000219 read_lock_bh(&tunnel->hlist_lock);
220 hlist_for_each_entry(session, walk, session_list, hlist) {
221 if (session->session_id == session_id) {
222 read_unlock_bh(&tunnel->hlist_lock);
223 return session;
224 }
225 }
226 read_unlock_bh(&tunnel->hlist_lock);
227
228 return NULL;
229}
230EXPORT_SYMBOL_GPL(l2tp_session_find);
231
232struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
233{
234 int hash;
235 struct hlist_node *walk;
236 struct l2tp_session *session;
237 int count = 0;
238
239 read_lock_bh(&tunnel->hlist_lock);
240 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
241 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
242 if (++count > nth) {
243 read_unlock_bh(&tunnel->hlist_lock);
244 return session;
245 }
246 }
247 }
248
249 read_unlock_bh(&tunnel->hlist_lock);
250
251 return NULL;
252}
253EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
254
James Chapman309795f2010-04-02 06:19:10 +0000255/* Lookup a session by interface name.
256 * This is very inefficient but is only used by management interfaces.
257 */
258struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
259{
260 struct l2tp_net *pn = l2tp_pernet(net);
261 int hash;
262 struct hlist_node *walk;
263 struct l2tp_session *session;
264
James Chapmane02d4942010-04-02 06:19:16 +0000265 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000266 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
James Chapmane02d4942010-04-02 06:19:16 +0000267 hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000268 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000269 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000270 return session;
271 }
272 }
273 }
274
James Chapmane02d4942010-04-02 06:19:16 +0000275 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000276
277 return NULL;
278}
279EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
280
James Chapmanfd558d12010-04-02 06:18:33 +0000281/* Lookup a tunnel by id
282 */
283struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
284{
285 struct l2tp_tunnel *tunnel;
286 struct l2tp_net *pn = l2tp_pernet(net);
287
James Chapmane02d4942010-04-02 06:19:16 +0000288 rcu_read_lock_bh();
289 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000290 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000291 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000292 return tunnel;
293 }
294 }
James Chapmane02d4942010-04-02 06:19:16 +0000295 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000296
297 return NULL;
298}
299EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
300
301struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
302{
303 struct l2tp_net *pn = l2tp_pernet(net);
304 struct l2tp_tunnel *tunnel;
305 int count = 0;
306
James Chapmane02d4942010-04-02 06:19:16 +0000307 rcu_read_lock_bh();
308 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000309 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000310 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000311 return tunnel;
312 }
313 }
314
James Chapmane02d4942010-04-02 06:19:16 +0000315 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000316
317 return NULL;
318}
319EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
320
321/*****************************************************************************
322 * Receive data handling
323 *****************************************************************************/
324
325/* Queue a skb in order. We come here only if the skb has an L2TP sequence
326 * number.
327 */
328static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
329{
330 struct sk_buff *skbp;
331 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000332 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapman5de7aee2012-04-29 21:48:46 +0000333 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000334
335 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000336 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000337 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
338 if (L2TP_SKB_CB(skbp)->ns > ns) {
339 __skb_queue_before(&session->reorder_q, skbp, skb);
340 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
341 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
342 session->name, ns, L2TP_SKB_CB(skbp)->ns,
343 skb_queue_len(&session->reorder_q));
James Chapman5de7aee2012-04-29 21:48:46 +0000344 u64_stats_update_begin(&sstats->syncp);
345 sstats->rx_oos_packets++;
346 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000347 goto out;
348 }
349 }
350
351 __skb_queue_tail(&session->reorder_q, skb);
352
353out:
354 spin_unlock_bh(&session->reorder_q.lock);
355}
356
357/* Dequeue a single skb.
358 */
359static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
360{
361 struct l2tp_tunnel *tunnel = session->tunnel;
362 int length = L2TP_SKB_CB(skb)->length;
James Chapman5de7aee2012-04-29 21:48:46 +0000363 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000364
365 /* We're about to requeue the skb, so return resources
366 * to its current owner (a socket receive buffer).
367 */
368 skb_orphan(skb);
369
James Chapman5de7aee2012-04-29 21:48:46 +0000370 tstats = &tunnel->stats;
371 u64_stats_update_begin(&tstats->syncp);
372 sstats = &session->stats;
373 u64_stats_update_begin(&sstats->syncp);
374 tstats->rx_packets++;
375 tstats->rx_bytes += length;
376 sstats->rx_packets++;
377 sstats->rx_bytes += length;
378 u64_stats_update_end(&tstats->syncp);
379 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000380
381 if (L2TP_SKB_CB(skb)->has_seq) {
382 /* Bump our Nr */
383 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000384 if (tunnel->version == L2TP_HDR_VER_2)
385 session->nr &= 0xffff;
386 else
387 session->nr &= 0xffffff;
388
James Chapmanfd558d12010-04-02 06:18:33 +0000389 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
390 "%s: updated nr to %hu\n", session->name, session->nr);
391 }
392
393 /* call private receive handler */
394 if (session->recv_skb != NULL)
395 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
396 else
397 kfree_skb(skb);
398
399 if (session->deref)
400 (*session->deref)(session);
401}
402
403/* Dequeue skbs from the session's reorder_q, subject to packet order.
404 * Skbs that have been in the queue for too long are simply discarded.
405 */
406static void l2tp_recv_dequeue(struct l2tp_session *session)
407{
408 struct sk_buff *skb;
409 struct sk_buff *tmp;
James Chapman5de7aee2012-04-29 21:48:46 +0000410 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000411
412 /* If the pkt at the head of the queue has the nr that we
413 * expect to send up next, dequeue it and any other
414 * in-sequence packets behind it.
415 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000416start:
James Chapmanfd558d12010-04-02 06:18:33 +0000417 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000418 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000419 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
420 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
James Chapman5de7aee2012-04-29 21:48:46 +0000421 u64_stats_update_begin(&sstats->syncp);
422 sstats->rx_seq_discards++;
423 sstats->rx_errors++;
424 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000425 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000426 "%s: oos pkt %u len %d discarded (too old), "
427 "waiting for %u, reorder_q_len=%d\n",
James Chapmanfd558d12010-04-02 06:18:33 +0000428 session->name, L2TP_SKB_CB(skb)->ns,
429 L2TP_SKB_CB(skb)->length, session->nr,
430 skb_queue_len(&session->reorder_q));
431 __skb_unlink(skb, &session->reorder_q);
432 kfree_skb(skb);
433 if (session->deref)
434 (*session->deref)(session);
435 continue;
436 }
437
438 if (L2TP_SKB_CB(skb)->has_seq) {
439 if (L2TP_SKB_CB(skb)->ns != session->nr) {
440 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000441 "%s: holding oos pkt %u len %d, "
442 "waiting for %u, reorder_q_len=%d\n",
James Chapmanfd558d12010-04-02 06:18:33 +0000443 session->name, L2TP_SKB_CB(skb)->ns,
444 L2TP_SKB_CB(skb)->length, session->nr,
445 skb_queue_len(&session->reorder_q));
446 goto out;
447 }
448 }
449 __skb_unlink(skb, &session->reorder_q);
450
451 /* Process the skb. We release the queue lock while we
452 * do so to let other contexts process the queue.
453 */
454 spin_unlock_bh(&session->reorder_q.lock);
455 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000456 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000457 }
458
459out:
460 spin_unlock_bh(&session->reorder_q.lock);
461}
462
463static inline int l2tp_verify_udp_checksum(struct sock *sk,
464 struct sk_buff *skb)
465{
466 struct udphdr *uh = udp_hdr(skb);
467 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000468 __wsum psum;
469
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000470 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000471 return 0;
472
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000473#if IS_ENABLED(CONFIG_IPV6)
474 if (sk->sk_family == PF_INET6) {
475 if (!uh->check) {
476 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
477 return 1;
478 }
479 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
480 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
481 &ipv6_hdr(skb)->daddr, ulen,
482 IPPROTO_UDP, skb->csum)) {
483 skb->ip_summed = CHECKSUM_UNNECESSARY;
484 return 0;
485 }
486 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
487 &ipv6_hdr(skb)->daddr,
488 skb->len, IPPROTO_UDP,
489 0));
490 } else
491#endif
492 {
493 struct inet_sock *inet;
494 if (!uh->check)
495 return 0;
496 inet = inet_sk(sk);
497 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
498 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000499
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000500 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
501 !csum_fold(csum_add(psum, skb->csum)))
502 return 0;
503 skb->csum = psum;
504 }
James Chapmanfd558d12010-04-02 06:18:33 +0000505
506 return __skb_checksum_complete(skb);
507}
508
James Chapmanf7faffa2010-04-02 06:18:49 +0000509/* Do receive processing of L2TP data frames. We handle both L2TPv2
510 * and L2TPv3 data frames here.
511 *
512 * L2TPv2 Data Message Header
513 *
514 * 0 1 2 3
515 * 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
516 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
517 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
518 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519 * | Tunnel ID | Session ID |
520 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521 * | Ns (opt) | Nr (opt) |
522 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
523 * | Offset Size (opt) | Offset pad... (opt)
524 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
525 *
526 * Data frames are marked by T=0. All other fields are the same as
527 * those in L2TP control frames.
528 *
529 * L2TPv3 Data Message Header
530 *
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * | L2TP Session Header |
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 * | L2-Specific Sublayer |
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | Tunnel Payload ...
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538 *
539 * L2TPv3 Session Header Over IP
540 *
541 * 0 1 2 3
542 * 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
543 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544 * | Session ID |
545 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
546 * | Cookie (optional, maximum 64 bits)...
547 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
548 * |
549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
550 *
551 * L2TPv3 L2-Specific Sublayer Format
552 *
553 * 0 1 2 3
554 * 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
555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556 * |x|S|x|x|x|x|x|x| Sequence Number |
557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
558 *
559 * Cookie value, sublayer format and offset (pad) are negotiated with
560 * the peer when the session is set up. Unlike L2TPv2, we do not need
561 * to parse the packet header to determine if optional fields are
562 * present.
563 *
564 * Caller must already have parsed the frame and determined that it is
565 * a data (not control) frame before coming here. Fields up to the
566 * session-id have already been parsed and ptr points to the data
567 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000568 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000569void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
570 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
571 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000572{
James Chapmanf7faffa2010-04-02 06:18:49 +0000573 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000574 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000575 u32 ns, nr;
James Chapman5de7aee2012-04-29 21:48:46 +0000576 struct l2tp_stats *sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000577
578 /* The ref count is increased since we now hold a pointer to
579 * the session. Take care to decrement the refcnt when exiting
580 * this function from now on...
581 */
582 l2tp_session_inc_refcount(session);
583 if (session->ref)
584 (*session->ref)(session);
585
James Chapmanf7faffa2010-04-02 06:18:49 +0000586 /* Parse and check optional cookie */
587 if (session->peer_cookie_len > 0) {
588 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
589 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
590 "%s: cookie mismatch (%u/%u). Discarding.\n",
591 tunnel->name, tunnel->tunnel_id, session->session_id);
James Chapman5de7aee2012-04-29 21:48:46 +0000592 u64_stats_update_begin(&sstats->syncp);
593 sstats->rx_cookie_discards++;
594 u64_stats_update_end(&sstats->syncp);
James Chapmanf7faffa2010-04-02 06:18:49 +0000595 goto discard;
596 }
597 ptr += session->peer_cookie_len;
598 }
599
James Chapmanfd558d12010-04-02 06:18:33 +0000600 /* Handle the optional sequence numbers. Sequence numbers are
601 * in different places for L2TPv2 and L2TPv3.
602 *
603 * If we are the LAC, enable/disable sequence numbers under
604 * the control of the LNS. If no sequence numbers present but
605 * we were expecting them, discard frame.
606 */
607 ns = nr = 0;
608 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000609 if (tunnel->version == L2TP_HDR_VER_2) {
610 if (hdrflags & L2TP_HDRFLAG_S) {
611 ns = ntohs(*(__be16 *) ptr);
612 ptr += 2;
613 nr = ntohs(*(__be16 *) ptr);
614 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000615
James Chapmanf7faffa2010-04-02 06:18:49 +0000616 /* Store L2TP info in the skb */
617 L2TP_SKB_CB(skb)->ns = ns;
618 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000619
James Chapmanf7faffa2010-04-02 06:18:49 +0000620 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
621 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
622 session->name, ns, nr, session->nr);
623 }
624 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
625 u32 l2h = ntohl(*(__be32 *) ptr);
626
627 if (l2h & 0x40000000) {
628 ns = l2h & 0x00ffffff;
629
630 /* Store L2TP info in the skb */
631 L2TP_SKB_CB(skb)->ns = ns;
632 L2TP_SKB_CB(skb)->has_seq = 1;
633
634 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
635 "%s: recv data ns=%u, session nr=%u\n",
636 session->name, ns, session->nr);
637 }
James Chapmanfd558d12010-04-02 06:18:33 +0000638 }
639
James Chapmanf7faffa2010-04-02 06:18:49 +0000640 /* Advance past L2-specific header, if present */
641 ptr += session->l2specific_len;
642
James Chapmanfd558d12010-04-02 06:18:33 +0000643 if (L2TP_SKB_CB(skb)->has_seq) {
644 /* Received a packet with sequence numbers. If we're the LNS,
645 * check if we sre sending sequence numbers and if not,
646 * configure it so.
647 */
648 if ((!session->lns_mode) && (!session->send_seq)) {
649 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
650 "%s: requested to enable seq numbers by LNS\n",
651 session->name);
652 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000653 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000654 }
655 } else {
656 /* No sequence numbers.
657 * If user has configured mandatory sequence numbers, discard.
658 */
659 if (session->recv_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);
James Chapman5de7aee2012-04-29 21:48:46 +0000663 u64_stats_update_begin(&sstats->syncp);
664 sstats->rx_seq_discards++;
665 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000666 goto discard;
667 }
668
669 /* If we're the LAC and we're sending sequence numbers, the
670 * LNS has requested that we no longer send sequence numbers.
671 * If we're the LNS and we're sending sequence numbers, the
672 * LAC is broken. Discard the frame.
673 */
674 if ((!session->lns_mode) && (session->send_seq)) {
675 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
676 "%s: requested to disable seq numbers by LNS\n",
677 session->name);
678 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000679 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000680 } else if (session->send_seq) {
681 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
682 "%s: recv data has no seq numbers when required. "
683 "Discarding\n", session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000684 u64_stats_update_begin(&sstats->syncp);
685 sstats->rx_seq_discards++;
686 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000687 goto discard;
688 }
689 }
690
James Chapmanf7faffa2010-04-02 06:18:49 +0000691 /* Session data offset is handled differently for L2TPv2 and
692 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
693 * the header. For L2TPv3, the offset is negotiated using AVPs
694 * in the session setup control protocol.
695 */
696 if (tunnel->version == L2TP_HDR_VER_2) {
697 /* If offset bit set, skip it. */
698 if (hdrflags & L2TP_HDRFLAG_O) {
699 offset = ntohs(*(__be16 *)ptr);
700 ptr += 2 + offset;
701 }
702 } else
703 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000704
705 offset = ptr - optr;
706 if (!pskb_may_pull(skb, offset))
707 goto discard;
708
709 __skb_pull(skb, offset);
710
711 /* If caller wants to process the payload before we queue the
712 * packet, do so now.
713 */
714 if (payload_hook)
715 if ((*payload_hook)(skb))
716 goto discard;
717
718 /* Prepare skb for adding to the session's reorder_q. Hold
719 * packets for max reorder_timeout or 1 second if not
720 * reordering.
721 */
722 L2TP_SKB_CB(skb)->length = length;
723 L2TP_SKB_CB(skb)->expires = jiffies +
724 (session->reorder_timeout ? session->reorder_timeout : HZ);
725
726 /* Add packet to the session's receive queue. Reordering is done here, if
727 * enabled. Saved L2TP protocol info is stored in skb->sb[].
728 */
729 if (L2TP_SKB_CB(skb)->has_seq) {
730 if (session->reorder_timeout != 0) {
731 /* Packet reordering enabled. Add skb to session's
732 * reorder queue, in order of ns.
733 */
734 l2tp_recv_queue_skb(session, skb);
735 } else {
736 /* Packet reordering disabled. Discard out-of-sequence
737 * packets
738 */
739 if (L2TP_SKB_CB(skb)->ns != session->nr) {
James Chapman5de7aee2012-04-29 21:48:46 +0000740 u64_stats_update_begin(&sstats->syncp);
741 sstats->rx_seq_discards++;
742 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000743 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000744 "%s: oos pkt %u len %d discarded, "
745 "waiting for %u, reorder_q_len=%d\n",
James Chapmanfd558d12010-04-02 06:18:33 +0000746 session->name, L2TP_SKB_CB(skb)->ns,
747 L2TP_SKB_CB(skb)->length, session->nr,
748 skb_queue_len(&session->reorder_q));
749 goto discard;
750 }
751 skb_queue_tail(&session->reorder_q, skb);
752 }
753 } else {
754 /* No sequence numbers. Add the skb to the tail of the
755 * reorder queue. This ensures that it will be
756 * delivered after all previous sequenced skbs.
757 */
758 skb_queue_tail(&session->reorder_q, skb);
759 }
760
761 /* Try to dequeue as many skbs from reorder_q as we can. */
762 l2tp_recv_dequeue(session);
763
764 l2tp_session_dec_refcount(session);
765
James Chapmanf7faffa2010-04-02 06:18:49 +0000766 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000767
768discard:
James Chapman5de7aee2012-04-29 21:48:46 +0000769 u64_stats_update_begin(&sstats->syncp);
770 sstats->rx_errors++;
771 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000772 kfree_skb(skb);
773
774 if (session->deref)
775 (*session->deref)(session);
776
777 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000778}
779EXPORT_SYMBOL(l2tp_recv_common);
780
781/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
782 * here. The skb is not on a list when we get here.
783 * Returns 0 if the packet was a data packet and was successfully passed on.
784 * Returns 1 if the packet was not a good data packet and could not be
785 * forwarded. All such packets are passed up to userspace to deal with.
786 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000787static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
788 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000789{
790 struct l2tp_session *session = NULL;
791 unsigned char *ptr, *optr;
792 u16 hdrflags;
793 u32 tunnel_id, session_id;
794 int offset;
795 u16 version;
796 int length;
James Chapman5de7aee2012-04-29 21:48:46 +0000797 struct l2tp_stats *tstats;
James Chapmanf7faffa2010-04-02 06:18:49 +0000798
799 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
800 goto discard_bad_csum;
801
802 /* UDP always verifies the packet length. */
803 __skb_pull(skb, sizeof(struct udphdr));
804
805 /* Short packet? */
806 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
807 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
808 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
809 goto error;
810 }
811
James Chapmanf7faffa2010-04-02 06:18:49 +0000812 /* Trace packet contents, if enabled */
813 if (tunnel->debug & L2TP_MSG_DATA) {
814 length = min(32u, skb->len);
815 if (!pskb_may_pull(skb, length))
816 goto error;
817
818 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
819
820 offset = 0;
821 do {
Eric Dumazete50e7052011-11-08 13:59:44 -0500822 printk(" %02X", skb->data[offset]);
James Chapmanf7faffa2010-04-02 06:18:49 +0000823 } while (++offset < length);
824
825 printk("\n");
826 }
827
Eric Dumazete50e7052011-11-08 13:59:44 -0500828 /* Point to L2TP header */
829 optr = ptr = skb->data;
830
James Chapmanf7faffa2010-04-02 06:18:49 +0000831 /* Get L2TP header flags */
832 hdrflags = ntohs(*(__be16 *) ptr);
833
834 /* Check protocol version */
835 version = hdrflags & L2TP_HDR_VER_MASK;
836 if (version != tunnel->version) {
837 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
838 "%s: recv protocol version mismatch: got %d expected %d\n",
839 tunnel->name, version, tunnel->version);
840 goto error;
841 }
842
843 /* Get length of L2TP packet */
844 length = skb->len;
845
846 /* If type is control packet, it is handled by userspace. */
847 if (hdrflags & L2TP_HDRFLAG_T) {
848 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
849 "%s: recv control packet, len=%d\n", tunnel->name, length);
850 goto error;
851 }
852
853 /* Skip flags */
854 ptr += 2;
855
856 if (tunnel->version == L2TP_HDR_VER_2) {
857 /* If length is present, skip it */
858 if (hdrflags & L2TP_HDRFLAG_L)
859 ptr += 2;
860
861 /* Extract tunnel and session ID */
862 tunnel_id = ntohs(*(__be16 *) ptr);
863 ptr += 2;
864 session_id = ntohs(*(__be16 *) ptr);
865 ptr += 2;
866 } else {
867 ptr += 2; /* skip reserved bits */
868 tunnel_id = tunnel->tunnel_id;
869 session_id = ntohl(*(__be32 *) ptr);
870 ptr += 4;
871 }
872
873 /* Find the session context */
874 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000875 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000876 /* Not found? Pass to userspace to deal with */
877 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
878 "%s: no session found (%u/%u). Passing up.\n",
879 tunnel->name, tunnel_id, session_id);
880 goto error;
881 }
882
883 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000884
885 return 0;
886
887discard_bad_csum:
888 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
889 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
James Chapman5de7aee2012-04-29 21:48:46 +0000890 tstats = &tunnel->stats;
891 u64_stats_update_begin(&tstats->syncp);
892 tstats->rx_errors++;
893 u64_stats_update_end(&tstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000894 kfree_skb(skb);
895
896 return 0;
897
898error:
899 /* Put UDP header back */
900 __skb_push(skb, sizeof(struct udphdr));
901
902 return 1;
903}
James Chapmanfd558d12010-04-02 06:18:33 +0000904
905/* UDP encapsulation receive handler. See net/ipv4/udp.c.
906 * Return codes:
907 * 0 : success.
908 * <0: error
909 * >0: skb should be passed up to userspace as UDP.
910 */
911int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
912{
913 struct l2tp_tunnel *tunnel;
914
915 tunnel = l2tp_sock_to_tunnel(sk);
916 if (tunnel == NULL)
917 goto pass_up;
918
919 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
920 "%s: received %d bytes\n", tunnel->name, skb->len);
921
922 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
923 goto pass_up_put;
924
925 sock_put(sk);
926 return 0;
927
928pass_up_put:
929 sock_put(sk);
930pass_up:
931 return 1;
932}
933EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
934
935/************************************************************************
936 * Transmit handling
937 ***********************************************************************/
938
939/* Build an L2TP header for the session into the buffer provided.
940 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000941static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000942{
James Chapmanf7faffa2010-04-02 06:18:49 +0000943 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000944 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000945 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000946 u16 flags = L2TP_HDR_VER_2;
947 u32 tunnel_id = tunnel->peer_tunnel_id;
948 u32 session_id = session->peer_session_id;
949
950 if (session->send_seq)
951 flags |= L2TP_HDRFLAG_S;
952
953 /* Setup L2TP header. */
954 *bufp++ = htons(flags);
955 *bufp++ = htons(tunnel_id);
956 *bufp++ = htons(session_id);
957 if (session->send_seq) {
958 *bufp++ = htons(session->ns);
959 *bufp++ = 0;
960 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000961 session->ns &= 0xffff;
James Chapmanfd558d12010-04-02 06:18:33 +0000962 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +0000963 "%s: updated ns to %u\n", session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000964 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000965
966 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000967}
968
James Chapmanf7faffa2010-04-02 06:18:49 +0000969static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000970{
James Chapman0d767512010-04-02 06:19:00 +0000971 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000972 char *bufp = buf;
973 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000974
James Chapman0d767512010-04-02 06:19:00 +0000975 /* Setup L2TP header. The header differs slightly for UDP and
976 * IP encapsulations. For UDP, there is 4 bytes of flags.
977 */
978 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
979 u16 flags = L2TP_HDR_VER_3;
980 *((__be16 *) bufp) = htons(flags);
981 bufp += 2;
982 *((__be16 *) bufp) = 0;
983 bufp += 2;
984 }
985
James Chapmanf7faffa2010-04-02 06:18:49 +0000986 *((__be32 *) bufp) = htonl(session->peer_session_id);
987 bufp += 4;
988 if (session->cookie_len) {
989 memcpy(bufp, &session->cookie[0], session->cookie_len);
990 bufp += session->cookie_len;
991 }
992 if (session->l2specific_len) {
993 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
994 u32 l2h = 0;
995 if (session->send_seq) {
996 l2h = 0x40000000 | session->ns;
997 session->ns++;
998 session->ns &= 0xffffff;
999 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
1000 "%s: updated ns to %u\n", session->name, session->ns);
1001 }
1002
1003 *((__be32 *) bufp) = htonl(l2h);
1004 }
1005 bufp += session->l2specific_len;
1006 }
1007 if (session->offset)
1008 bufp += session->offset;
1009
1010 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001011}
James Chapmanfd558d12010-04-02 06:18:33 +00001012
stephen hemmingerfc130842010-10-21 07:50:46 +00001013static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001014 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001015{
1016 struct l2tp_tunnel *tunnel = session->tunnel;
1017 unsigned int len = skb->len;
1018 int error;
James Chapman5de7aee2012-04-29 21:48:46 +00001019 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +00001020
1021 /* Debug */
1022 if (session->send_seq)
1023 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
James Chapmanf7faffa2010-04-02 06:18:49 +00001024 "%s: send %Zd bytes, ns=%u\n", session->name,
James Chapmanfd558d12010-04-02 06:18:33 +00001025 data_len, session->ns - 1);
1026 else
1027 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
1028 "%s: send %Zd bytes\n", session->name, data_len);
1029
1030 if (session->debug & L2TP_MSG_DATA) {
1031 int i;
James Chapman0d767512010-04-02 06:19:00 +00001032 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1033 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001034
1035 printk(KERN_DEBUG "%s: xmit:", session->name);
James Chapman0d767512010-04-02 06:19:00 +00001036 for (i = 0; i < (len - uhlen); i++) {
James Chapmanfd558d12010-04-02 06:18:33 +00001037 printk(" %02X", *datap++);
1038 if (i == 31) {
1039 printk(" ...");
1040 break;
1041 }
1042 }
1043 printk("\n");
1044 }
1045
1046 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001047 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001048#if IS_ENABLED(CONFIG_IPV6)
1049 if (skb->sk->sk_family == PF_INET6)
1050 error = inet6_csk_xmit(skb, NULL);
1051 else
1052#endif
1053 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001054
1055 /* Update stats */
James Chapman5de7aee2012-04-29 21:48:46 +00001056 tstats = &tunnel->stats;
1057 u64_stats_update_begin(&tstats->syncp);
1058 sstats = &session->stats;
1059 u64_stats_update_begin(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001060 if (error >= 0) {
James Chapman5de7aee2012-04-29 21:48:46 +00001061 tstats->tx_packets++;
1062 tstats->tx_bytes += len;
1063 sstats->tx_packets++;
1064 sstats->tx_bytes += len;
James Chapmanfd558d12010-04-02 06:18:33 +00001065 } else {
James Chapman5de7aee2012-04-29 21:48:46 +00001066 tstats->tx_errors++;
1067 sstats->tx_errors++;
James Chapmanfd558d12010-04-02 06:18:33 +00001068 }
James Chapman5de7aee2012-04-29 21:48:46 +00001069 u64_stats_update_end(&tstats->syncp);
1070 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001071
1072 return 0;
1073}
James Chapmanfd558d12010-04-02 06:18:33 +00001074
1075/* Automatically called when the skb is freed.
1076 */
1077static void l2tp_sock_wfree(struct sk_buff *skb)
1078{
1079 sock_put(skb->sk);
1080}
1081
1082/* For data skbs that we transmit, we associate with the tunnel socket
1083 * but don't do accounting.
1084 */
1085static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1086{
1087 sock_hold(sk);
1088 skb->sk = sk;
1089 skb->destructor = l2tp_sock_wfree;
1090}
1091
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001092#if IS_ENABLED(CONFIG_IPV6)
1093static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1094 int udp_len)
1095{
1096 struct ipv6_pinfo *np = inet6_sk(sk);
1097 struct udphdr *uh = udp_hdr(skb);
1098
1099 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1100 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1101 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1102 skb->ip_summed = CHECKSUM_UNNECESSARY;
1103 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1104 IPPROTO_UDP, csum);
1105 if (uh->check == 0)
1106 uh->check = CSUM_MANGLED_0;
1107 } else {
1108 skb->ip_summed = CHECKSUM_PARTIAL;
1109 skb->csum_start = skb_transport_header(skb) - skb->head;
1110 skb->csum_offset = offsetof(struct udphdr, check);
1111 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1112 udp_len, IPPROTO_UDP, 0);
1113 }
1114}
1115#endif
1116
James Chapmanfd558d12010-04-02 06:18:33 +00001117/* If caller requires the skb to have a ppp header, the header must be
1118 * inserted in the skb data before calling this function.
1119 */
1120int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1121{
1122 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001123 struct l2tp_tunnel *tunnel = session->tunnel;
1124 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001125 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001126 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001127 struct inet_sock *inet;
1128 __wsum csum;
1129 int old_headroom;
1130 int new_headroom;
1131 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001132 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1133 int udp_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001134
1135 /* Check that there's enough headroom in the skb to insert IP,
1136 * UDP and L2TP headers. If not enough, expand it to
1137 * make room. Adjust truesize.
1138 */
1139 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001140 uhlen + hdr_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001141 old_headroom = skb_headroom(skb);
Eric Dumazet835acf52011-10-07 05:35:46 +00001142 if (skb_cow_head(skb, headroom)) {
1143 dev_kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001144 goto abort;
Eric Dumazet835acf52011-10-07 05:35:46 +00001145 }
James Chapmanfd558d12010-04-02 06:18:33 +00001146
1147 new_headroom = skb_headroom(skb);
1148 skb_orphan(skb);
1149 skb->truesize += new_headroom - old_headroom;
1150
1151 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001152 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001153
James Chapman0d767512010-04-02 06:19:00 +00001154 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001155 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1156 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1157 IPSKB_REROUTED);
1158 nf_reset(skb);
1159
David S. Miller6af88da2011-05-08 13:45:20 -07001160 bh_lock_sock(sk);
1161 if (sock_owned_by_user(sk)) {
1162 dev_kfree_skb(skb);
1163 goto out_unlock;
1164 }
1165
James Chapmanfd558d12010-04-02 06:18:33 +00001166 /* Get routing info from the tunnel socket */
1167 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001168 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001169
David S. Millerd9d8da82011-05-06 22:23:20 -07001170 inet = inet_sk(sk);
1171 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001172 switch (tunnel->encap) {
1173 case L2TP_ENCAPTYPE_UDP:
1174 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001175 __skb_push(skb, sizeof(*uh));
1176 skb_reset_transport_header(skb);
1177 uh = udp_hdr(skb);
1178 uh->source = inet->inet_sport;
1179 uh->dest = inet->inet_dport;
1180 udp_len = uhlen + hdr_len + data_len;
1181 uh->len = htons(udp_len);
1182 uh->check = 0;
1183
1184 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001185#if IS_ENABLED(CONFIG_IPV6)
1186 if (sk->sk_family == PF_INET6)
1187 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1188 else
1189#endif
James Chapman0d767512010-04-02 06:19:00 +00001190 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1191 skb->ip_summed = CHECKSUM_NONE;
1192 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1193 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1194 skb->ip_summed = CHECKSUM_COMPLETE;
1195 csum = skb_checksum(skb, 0, udp_len, 0);
1196 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1197 inet->inet_daddr,
1198 udp_len, IPPROTO_UDP, csum);
1199 if (uh->check == 0)
1200 uh->check = CSUM_MANGLED_0;
1201 } else {
1202 skb->ip_summed = CHECKSUM_PARTIAL;
1203 skb->csum_start = skb_transport_header(skb) - skb->head;
1204 skb->csum_offset = offsetof(struct udphdr, check);
1205 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1206 inet->inet_daddr,
1207 udp_len, IPPROTO_UDP, 0);
1208 }
1209 break;
1210
1211 case L2TP_ENCAPTYPE_IP:
1212 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001213 }
1214
James Chapman0d767512010-04-02 06:19:00 +00001215 l2tp_skb_set_owner_w(skb, sk);
1216
David S. Millerd9d8da82011-05-06 22:23:20 -07001217 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001218out_unlock:
1219 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001220
1221abort:
1222 return 0;
1223}
1224EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1225
1226/*****************************************************************************
1227 * Tinnel and session create/destroy.
1228 *****************************************************************************/
1229
1230/* Tunnel socket destruct hook.
1231 * The tunnel context is deleted only when all session sockets have been
1232 * closed.
1233 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001234static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001235{
1236 struct l2tp_tunnel *tunnel;
1237
1238 tunnel = sk->sk_user_data;
1239 if (tunnel == NULL)
1240 goto end;
1241
1242 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1243 "%s: closing...\n", tunnel->name);
1244
1245 /* Close all sessions */
1246 l2tp_tunnel_closeall(tunnel);
1247
James Chapman0d767512010-04-02 06:19:00 +00001248 switch (tunnel->encap) {
1249 case L2TP_ENCAPTYPE_UDP:
1250 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1251 (udp_sk(sk))->encap_type = 0;
1252 (udp_sk(sk))->encap_rcv = NULL;
1253 break;
1254 case L2TP_ENCAPTYPE_IP:
1255 break;
1256 }
James Chapmanfd558d12010-04-02 06:18:33 +00001257
1258 /* Remove hooks into tunnel socket */
1259 tunnel->sock = NULL;
1260 sk->sk_destruct = tunnel->old_sk_destruct;
1261 sk->sk_user_data = NULL;
1262
1263 /* Call the original destructor */
1264 if (sk->sk_destruct)
1265 (*sk->sk_destruct)(sk);
1266
1267 /* We're finished with the socket */
1268 l2tp_tunnel_dec_refcount(tunnel);
1269
1270end:
1271 return;
1272}
James Chapmanfd558d12010-04-02 06:18:33 +00001273
1274/* When the tunnel is closed, all the attached sessions need to go too.
1275 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001276static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001277{
1278 int hash;
1279 struct hlist_node *walk;
1280 struct hlist_node *tmp;
1281 struct l2tp_session *session;
1282
1283 BUG_ON(tunnel == NULL);
1284
1285 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1286 "%s: closing all sessions...\n", tunnel->name);
1287
1288 write_lock_bh(&tunnel->hlist_lock);
1289 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1290again:
1291 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1292 session = hlist_entry(walk, struct l2tp_session, hlist);
1293
1294 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1295 "%s: closing session\n", session->name);
1296
1297 hlist_del_init(&session->hlist);
1298
1299 /* Since we should hold the sock lock while
1300 * doing any unbinding, we need to release the
1301 * lock we're holding before taking that lock.
1302 * Hold a reference to the sock so it doesn't
1303 * disappear as we're jumping between locks.
1304 */
1305 if (session->ref != NULL)
1306 (*session->ref)(session);
1307
1308 write_unlock_bh(&tunnel->hlist_lock);
1309
James Chapmanf7faffa2010-04-02 06:18:49 +00001310 if (tunnel->version != L2TP_HDR_VER_2) {
1311 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1312
James Chapmane02d4942010-04-02 06:19:16 +00001313 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1314 hlist_del_init_rcu(&session->global_hlist);
1315 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1316 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001317 }
1318
James Chapmanfd558d12010-04-02 06:18:33 +00001319 if (session->session_close != NULL)
1320 (*session->session_close)(session);
1321
1322 if (session->deref != NULL)
1323 (*session->deref)(session);
1324
1325 write_lock_bh(&tunnel->hlist_lock);
1326
1327 /* Now restart from the beginning of this hash
1328 * chain. We always remove a session from the
1329 * list so we are guaranteed to make forward
1330 * progress.
1331 */
1332 goto again;
1333 }
1334 }
1335 write_unlock_bh(&tunnel->hlist_lock);
1336}
James Chapmanfd558d12010-04-02 06:18:33 +00001337
1338/* Really kill the tunnel.
1339 * Come here only when all sessions have been cleared from the tunnel.
1340 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001341static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001342{
1343 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1344
1345 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1346 BUG_ON(tunnel->sock != NULL);
1347
1348 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1349 "%s: free...\n", tunnel->name);
1350
1351 /* Remove from tunnel list */
James Chapmane02d4942010-04-02 06:19:16 +00001352 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1353 list_del_rcu(&tunnel->list);
1354 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1355 synchronize_rcu();
James Chapmanfd558d12010-04-02 06:18:33 +00001356
1357 atomic_dec(&l2tp_tunnel_count);
1358 kfree(tunnel);
1359}
James Chapmanfd558d12010-04-02 06:18:33 +00001360
James Chapman789a4a22010-04-02 06:19:40 +00001361/* Create a socket for the tunnel, if one isn't set up by
1362 * userspace. This is used for static tunnels where there is no
1363 * managing L2TP daemon.
1364 */
1365static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1366{
1367 int err = -EINVAL;
1368 struct sockaddr_in udp_addr;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001369#if IS_ENABLED(CONFIG_IPV6)
1370 struct sockaddr_in6 udp6_addr;
1371#endif
James Chapman789a4a22010-04-02 06:19:40 +00001372 struct sockaddr_l2tpip ip_addr;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001373 struct socket *sock = NULL;
James Chapman789a4a22010-04-02 06:19:40 +00001374
1375 switch (cfg->encap) {
1376 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001377#if IS_ENABLED(CONFIG_IPV6)
1378 if (cfg->local_ip6 && cfg->peer_ip6) {
1379 err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp);
1380 if (err < 0)
1381 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001382
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001383 sock = *sockp;
James Chapman789a4a22010-04-02 06:19:40 +00001384
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001385 memset(&udp6_addr, 0, sizeof(udp6_addr));
1386 udp6_addr.sin6_family = AF_INET6;
1387 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1388 sizeof(udp6_addr.sin6_addr));
1389 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1390 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1391 sizeof(udp6_addr));
1392 if (err < 0)
1393 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001394
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001395 udp6_addr.sin6_family = AF_INET6;
1396 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1397 sizeof(udp6_addr.sin6_addr));
1398 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1399 err = kernel_connect(sock,
1400 (struct sockaddr *) &udp6_addr,
1401 sizeof(udp6_addr), 0);
1402 if (err < 0)
1403 goto out;
1404 } else
1405#endif
1406 {
1407 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1408 if (err < 0)
1409 goto out;
1410
1411 sock = *sockp;
1412
1413 memset(&udp_addr, 0, sizeof(udp_addr));
1414 udp_addr.sin_family = AF_INET;
1415 udp_addr.sin_addr = cfg->local_ip;
1416 udp_addr.sin_port = htons(cfg->local_udp_port);
1417 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1418 sizeof(udp_addr));
1419 if (err < 0)
1420 goto out;
1421
1422 udp_addr.sin_family = AF_INET;
1423 udp_addr.sin_addr = cfg->peer_ip;
1424 udp_addr.sin_port = htons(cfg->peer_udp_port);
1425 err = kernel_connect(sock,
1426 (struct sockaddr *) &udp_addr,
1427 sizeof(udp_addr), 0);
1428 if (err < 0)
1429 goto out;
1430 }
James Chapman789a4a22010-04-02 06:19:40 +00001431
1432 if (!cfg->use_udp_checksums)
1433 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1434
1435 break;
1436
1437 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001438#if IS_ENABLED(CONFIG_IPV6)
1439 if (cfg->local_ip6 && cfg->peer_ip6) {
1440 /* IP encap over IPv6 not yet supported */
1441 err = -EPROTONOSUPPORT;
1442 goto out;
1443 }
1444#endif
James Chapman789a4a22010-04-02 06:19:40 +00001445 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1446 if (err < 0)
1447 goto out;
1448
1449 sock = *sockp;
1450
1451 memset(&ip_addr, 0, sizeof(ip_addr));
1452 ip_addr.l2tp_family = AF_INET;
1453 ip_addr.l2tp_addr = cfg->local_ip;
1454 ip_addr.l2tp_conn_id = tunnel_id;
1455 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1456 if (err < 0)
1457 goto out;
1458
1459 ip_addr.l2tp_family = AF_INET;
1460 ip_addr.l2tp_addr = cfg->peer_ip;
1461 ip_addr.l2tp_conn_id = peer_tunnel_id;
1462 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1463 if (err < 0)
1464 goto out;
1465
1466 break;
1467
1468 default:
1469 goto out;
1470 }
1471
1472out:
1473 if ((err < 0) && sock) {
1474 sock_release(sock);
1475 *sockp = NULL;
1476 }
1477
1478 return err;
1479}
1480
James Chapmanfd558d12010-04-02 06:18:33 +00001481int 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)
1482{
1483 struct l2tp_tunnel *tunnel = NULL;
1484 int err;
1485 struct socket *sock = NULL;
1486 struct sock *sk = NULL;
1487 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001488 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001489
1490 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001491 * the userspace L2TP daemon. If not specified, create a
1492 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001493 */
James Chapman789a4a22010-04-02 06:19:40 +00001494 if (fd < 0) {
1495 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1496 if (err < 0)
1497 goto err;
1498 } else {
1499 err = -EBADF;
1500 sock = sockfd_lookup(fd, &err);
1501 if (!sock) {
1502 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1503 tunnel_id, fd, err);
1504 goto err;
1505 }
James Chapmanfd558d12010-04-02 06:18:33 +00001506 }
1507
1508 sk = sock->sk;
1509
James Chapman0d767512010-04-02 06:19:00 +00001510 if (cfg != NULL)
1511 encap = cfg->encap;
1512
James Chapmanfd558d12010-04-02 06:18:33 +00001513 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001514 switch (encap) {
1515 case L2TP_ENCAPTYPE_UDP:
1516 err = -EPROTONOSUPPORT;
1517 if (sk->sk_protocol != IPPROTO_UDP) {
1518 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1519 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1520 goto err;
1521 }
1522 break;
1523 case L2TP_ENCAPTYPE_IP:
1524 err = -EPROTONOSUPPORT;
1525 if (sk->sk_protocol != IPPROTO_L2TP) {
1526 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1527 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1528 goto err;
1529 }
1530 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001531 }
1532
1533 /* Check if this socket has already been prepped */
1534 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1535 if (tunnel != NULL) {
1536 /* This socket has already been prepped */
1537 err = -EBUSY;
1538 goto err;
1539 }
1540
James Chapmanfd558d12010-04-02 06:18:33 +00001541 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1542 if (tunnel == NULL) {
1543 err = -ENOMEM;
1544 goto err;
1545 }
1546
1547 tunnel->version = version;
1548 tunnel->tunnel_id = tunnel_id;
1549 tunnel->peer_tunnel_id = peer_tunnel_id;
1550 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1551
1552 tunnel->magic = L2TP_TUNNEL_MAGIC;
1553 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1554 rwlock_init(&tunnel->hlist_lock);
1555
1556 /* The net we belong to */
1557 tunnel->l2tp_net = net;
1558 pn = l2tp_pernet(net);
1559
James Chapman0d767512010-04-02 06:19:00 +00001560 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001561 tunnel->debug = cfg->debug;
1562
1563 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001564 tunnel->encap = encap;
1565 if (encap == L2TP_ENCAPTYPE_UDP) {
1566 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1567 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1568 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001569#if IS_ENABLED(CONFIG_IPV6)
1570 if (sk->sk_family == PF_INET6)
1571 udpv6_encap_enable();
1572 else
1573#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001574 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001575 }
James Chapmanfd558d12010-04-02 06:18:33 +00001576
1577 sk->sk_user_data = tunnel;
1578
1579 /* Hook on the tunnel socket destructor so that we can cleanup
1580 * if the tunnel socket goes away.
1581 */
1582 tunnel->old_sk_destruct = sk->sk_destruct;
1583 sk->sk_destruct = &l2tp_tunnel_destruct;
1584 tunnel->sock = sk;
1585 sk->sk_allocation = GFP_ATOMIC;
1586
1587 /* Add tunnel to our list */
1588 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001589 atomic_inc(&l2tp_tunnel_count);
1590
1591 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001592 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001593 */
1594 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001595 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1596 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1597 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001598
1599 err = 0;
1600err:
1601 if (tunnelp)
1602 *tunnelp = tunnel;
1603
James Chapman789a4a22010-04-02 06:19:40 +00001604 /* If tunnel's socket was created by the kernel, it doesn't
1605 * have a file.
1606 */
1607 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001608 sockfd_put(sock);
1609
1610 return err;
1611}
1612EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1613
James Chapman309795f2010-04-02 06:19:10 +00001614/* This function is used by the netlink TUNNEL_DELETE command.
1615 */
1616int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1617{
1618 int err = 0;
James Chapman789a4a22010-04-02 06:19:40 +00001619 struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
James Chapman309795f2010-04-02 06:19:10 +00001620
1621 /* Force the tunnel socket to close. This will eventually
1622 * cause the tunnel to be deleted via the normal socket close
1623 * mechanisms when userspace closes the tunnel socket.
1624 */
James Chapman789a4a22010-04-02 06:19:40 +00001625 if (sock != NULL) {
1626 err = inet_shutdown(sock, 2);
1627
1628 /* If the tunnel's socket was created by the kernel,
1629 * close the socket here since the socket was not
1630 * created by userspace.
1631 */
1632 if (sock->file == NULL)
1633 err = inet_release(sock);
1634 }
James Chapman309795f2010-04-02 06:19:10 +00001635
1636 return err;
1637}
1638EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1639
James Chapmanfd558d12010-04-02 06:18:33 +00001640/* Really kill the session.
1641 */
1642void l2tp_session_free(struct l2tp_session *session)
1643{
1644 struct l2tp_tunnel *tunnel;
1645
1646 BUG_ON(atomic_read(&session->ref_count) != 0);
1647
1648 tunnel = session->tunnel;
1649 if (tunnel != NULL) {
1650 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1651
1652 /* Delete the session from the hash */
1653 write_lock_bh(&tunnel->hlist_lock);
1654 hlist_del_init(&session->hlist);
1655 write_unlock_bh(&tunnel->hlist_lock);
1656
James Chapmanf7faffa2010-04-02 06:18:49 +00001657 /* Unlink from the global hash if not L2TPv2 */
1658 if (tunnel->version != L2TP_HDR_VER_2) {
1659 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1660
James Chapmane02d4942010-04-02 06:19:16 +00001661 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1662 hlist_del_init_rcu(&session->global_hlist);
1663 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1664 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001665 }
1666
James Chapmanfd558d12010-04-02 06:18:33 +00001667 if (session->session_id != 0)
1668 atomic_dec(&l2tp_session_count);
1669
1670 sock_put(tunnel->sock);
1671
1672 /* This will delete the tunnel context if this
1673 * is the last session on the tunnel.
1674 */
1675 session->tunnel = NULL;
1676 l2tp_tunnel_dec_refcount(tunnel);
1677 }
1678
1679 kfree(session);
1680
1681 return;
1682}
1683EXPORT_SYMBOL_GPL(l2tp_session_free);
1684
James Chapman309795f2010-04-02 06:19:10 +00001685/* This function is used by the netlink SESSION_DELETE command and by
1686 pseudowire modules.
1687 */
1688int l2tp_session_delete(struct l2tp_session *session)
1689{
1690 if (session->session_close != NULL)
1691 (*session->session_close)(session);
1692
1693 l2tp_session_dec_refcount(session);
1694
1695 return 0;
1696}
1697EXPORT_SYMBOL_GPL(l2tp_session_delete);
1698
1699
James Chapmanf7faffa2010-04-02 06:18:49 +00001700/* We come here whenever a session's send_seq, cookie_len or
1701 * l2specific_len parameters are set.
1702 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001703static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001704{
1705 if (version == L2TP_HDR_VER_2) {
1706 session->hdr_len = 6;
1707 if (session->send_seq)
1708 session->hdr_len += 4;
1709 } else {
James Chapman0d767512010-04-02 06:19:00 +00001710 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1711 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1712 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001713 }
1714
1715}
James Chapmanf7faffa2010-04-02 06:18:49 +00001716
James Chapmanfd558d12010-04-02 06:18:33 +00001717struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1718{
1719 struct l2tp_session *session;
1720
1721 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1722 if (session != NULL) {
1723 session->magic = L2TP_SESSION_MAGIC;
1724 session->tunnel = tunnel;
1725
1726 session->session_id = session_id;
1727 session->peer_session_id = peer_session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +00001728 session->nr = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001729
1730 sprintf(&session->name[0], "sess %u/%u",
1731 tunnel->tunnel_id, session->session_id);
1732
1733 skb_queue_head_init(&session->reorder_q);
1734
1735 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001736 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001737
1738 /* Inherit debug options from tunnel */
1739 session->debug = tunnel->debug;
1740
1741 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001742 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001743 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001744 session->mtu = cfg->mtu;
1745 session->mru = cfg->mru;
1746 session->send_seq = cfg->send_seq;
1747 session->recv_seq = cfg->recv_seq;
1748 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001749 session->reorder_timeout = cfg->reorder_timeout;
1750 session->offset = cfg->offset;
1751 session->l2specific_type = cfg->l2specific_type;
1752 session->l2specific_len = cfg->l2specific_len;
1753 session->cookie_len = cfg->cookie_len;
1754 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1755 session->peer_cookie_len = cfg->peer_cookie_len;
1756 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001757 }
1758
James Chapmanf7faffa2010-04-02 06:18:49 +00001759 if (tunnel->version == L2TP_HDR_VER_2)
1760 session->build_header = l2tp_build_l2tpv2_header;
1761 else
1762 session->build_header = l2tp_build_l2tpv3_header;
1763
1764 l2tp_session_set_header_len(session, tunnel->version);
1765
James Chapmanfd558d12010-04-02 06:18:33 +00001766 /* Bump the reference count. The session context is deleted
1767 * only when this drops to zero.
1768 */
1769 l2tp_session_inc_refcount(session);
1770 l2tp_tunnel_inc_refcount(tunnel);
1771
1772 /* Ensure tunnel socket isn't deleted */
1773 sock_hold(tunnel->sock);
1774
1775 /* Add session to the tunnel's hash list */
1776 write_lock_bh(&tunnel->hlist_lock);
1777 hlist_add_head(&session->hlist,
1778 l2tp_session_id_hash(tunnel, session_id));
1779 write_unlock_bh(&tunnel->hlist_lock);
1780
James Chapmanf7faffa2010-04-02 06:18:49 +00001781 /* And to the global session list if L2TPv3 */
1782 if (tunnel->version != L2TP_HDR_VER_2) {
1783 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1784
James Chapmane02d4942010-04-02 06:19:16 +00001785 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1786 hlist_add_head_rcu(&session->global_hlist,
1787 l2tp_session_id_hash_2(pn, session_id));
1788 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001789 }
1790
James Chapmanfd558d12010-04-02 06:18:33 +00001791 /* Ignore management session in session count value */
1792 if (session->session_id != 0)
1793 atomic_inc(&l2tp_session_count);
1794 }
1795
1796 return session;
1797}
1798EXPORT_SYMBOL_GPL(l2tp_session_create);
1799
1800/*****************************************************************************
1801 * Init and cleanup
1802 *****************************************************************************/
1803
1804static __net_init int l2tp_init_net(struct net *net)
1805{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001806 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001807 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001808
James Chapmanfd558d12010-04-02 06:18:33 +00001809 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001810 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001811
James Chapmanf7faffa2010-04-02 06:18:49 +00001812 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1813 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1814
James Chapmane02d4942010-04-02 06:19:16 +00001815 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001816
James Chapmanfd558d12010-04-02 06:18:33 +00001817 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001818}
1819
1820static struct pernet_operations l2tp_net_ops = {
1821 .init = l2tp_init_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001822 .id = &l2tp_net_id,
1823 .size = sizeof(struct l2tp_net),
1824};
1825
1826static int __init l2tp_init(void)
1827{
1828 int rc = 0;
1829
1830 rc = register_pernet_device(&l2tp_net_ops);
1831 if (rc)
1832 goto out;
1833
1834 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1835
1836out:
1837 return rc;
1838}
1839
1840static void __exit l2tp_exit(void)
1841{
1842 unregister_pernet_device(&l2tp_net_ops);
1843}
1844
1845module_init(l2tp_init);
1846module_exit(l2tp_exit);
1847
1848MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1849MODULE_DESCRIPTION("L2TP core");
1850MODULE_LICENSE("GPL");
1851MODULE_VERSION(L2TP_DRV_VERSION);
1852