blob: 32b2155e7ab496e93fe5c2b7396a94c8f7bcd09b [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*
2 * L2TP core.
3 *
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5 *
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
8 *
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
11 * Contributors:
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
20
Joe Perchesa4ca44f2012-05-16 09:55:56 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
James Chapmanfd558d12010-04-02 06:18:33 +000023#include <linux/module.h>
24#include <linux/string.h>
25#include <linux/list.h>
James Chapmane02d4942010-04-02 06:19:16 +000026#include <linux/rculist.h>
James Chapmanfd558d12010-04-02 06:18:33 +000027#include <linux/uaccess.h>
28
29#include <linux/kernel.h>
30#include <linux/spinlock.h>
31#include <linux/kthread.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/errno.h>
35#include <linux/jiffies.h>
36
37#include <linux/netdevice.h>
38#include <linux/net.h>
39#include <linux/inetdevice.h>
40#include <linux/skbuff.h>
41#include <linux/init.h>
James Chapman0d767512010-04-02 06:19:00 +000042#include <linux/in.h>
James Chapmanfd558d12010-04-02 06:18:33 +000043#include <linux/ip.h>
44#include <linux/udp.h>
James Chapman0d767512010-04-02 06:19:00 +000045#include <linux/l2tp.h>
James Chapmanfd558d12010-04-02 06:18:33 +000046#include <linux/hash.h>
47#include <linux/sort.h>
48#include <linux/file.h>
49#include <linux/nsproxy.h>
50#include <net/net_namespace.h>
51#include <net/netns/generic.h>
52#include <net/dst.h>
53#include <net/ip.h>
54#include <net/udp.h>
James Chapman309795f2010-04-02 06:19:10 +000055#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000056#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000057#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000058#include <net/inet6_connection_sock.h>
59#include <net/inet_ecn.h>
60#include <net/ip6_route.h>
David S. Millerd499bd22012-04-30 13:21:28 -040061#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000062
63#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000065
66#include "l2tp_core.h"
67
68#define L2TP_DRV_VERSION "V2.0"
69
70/* L2TP header constants */
71#define L2TP_HDRFLAG_T 0x8000
72#define L2TP_HDRFLAG_L 0x4000
73#define L2TP_HDRFLAG_S 0x0800
74#define L2TP_HDRFLAG_O 0x0200
75#define L2TP_HDRFLAG_P 0x0100
76
77#define L2TP_HDR_VER_MASK 0x000F
78#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000079#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000080
81/* L2TPv3 default L2-specific sublayer */
82#define L2TP_SLFLAG_S 0x40000000
83#define L2TP_SL_SEQ_MASK 0x00ffffff
84
85#define L2TP_HDR_SIZE_SEQ 10
86#define L2TP_HDR_SIZE_NOSEQ 6
87
88/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0
90
James Chapmanfd558d12010-04-02 06:18:33 +000091/* Private data stored for received packets in the skb.
92 */
93struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000094 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000095 u16 has_seq;
96 u16 length;
97 unsigned long expires;
98};
99
100#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101
102static atomic_t l2tp_tunnel_count;
103static atomic_t l2tp_session_count;
104
105/* per-net private data for this module */
106static unsigned int l2tp_net_id;
107struct l2tp_net {
108 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000109 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000110 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000111 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000112};
113
stephen hemmingerfc130842010-10-21 07:50:46 +0000114static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
115static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
116static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
117
James Chapmanfd558d12010-04-02 06:18:33 +0000118static inline struct l2tp_net *l2tp_pernet(struct net *net)
119{
120 BUG_ON(!net);
121
122 return net_generic(net, l2tp_net_id);
123}
124
stephen hemmingerfc130842010-10-21 07:50:46 +0000125
126/* Tunnel reference counts. Incremented per session that is added to
127 * the tunnel.
128 */
129static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
130{
131 atomic_inc(&tunnel->ref_count);
132}
133
134static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
135{
136 if (atomic_dec_and_test(&tunnel->ref_count))
137 l2tp_tunnel_free(tunnel);
138}
139#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000140#define l2tp_tunnel_inc_refcount(_t) \
141do { \
142 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
143 __func__, __LINE__, (_t)->name, \
144 atomic_read(&_t->ref_count)); \
145 l2tp_tunnel_inc_refcount_1(_t); \
146} while (0)
147#define l2tp_tunnel_dec_refcount(_t)
148do { \
149 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
150 __func__, __LINE__, (_t)->name, \
151 atomic_read(&_t->ref_count)); \
152 l2tp_tunnel_dec_refcount_1(_t); \
153} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000154#else
155#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
156#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
157#endif
158
James Chapmanf7faffa2010-04-02 06:18:49 +0000159/* Session hash global list for L2TPv3.
160 * The session_id SHOULD be random according to RFC3931, but several
161 * L2TP implementations use incrementing session_ids. So we do a real
162 * hash on the session_id, rather than a simple bitmask.
163 */
164static inline struct hlist_head *
165l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
166{
167 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
168
169}
170
171/* Lookup a session by id in the global session list
172 */
173static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
174{
175 struct l2tp_net *pn = l2tp_pernet(net);
176 struct hlist_head *session_list =
177 l2tp_session_id_hash_2(pn, session_id);
178 struct l2tp_session *session;
179 struct hlist_node *walk;
180
James Chapmane02d4942010-04-02 06:19:16 +0000181 rcu_read_lock_bh();
182 hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000183 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000184 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000185 return session;
186 }
187 }
James Chapmane02d4942010-04-02 06:19:16 +0000188 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000189
190 return NULL;
191}
192
James Chapmanfd558d12010-04-02 06:18:33 +0000193/* Session hash list.
194 * The session_id SHOULD be random according to RFC2661, but several
195 * L2TP implementations (Cisco and Microsoft) use incrementing
196 * session_ids. So we do a real hash on the session_id, rather than a
197 * simple bitmask.
198 */
199static inline struct hlist_head *
200l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
201{
202 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
203}
204
205/* Lookup a session by id
206 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000207struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000208{
James Chapmanf7faffa2010-04-02 06:18:49 +0000209 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000210 struct l2tp_session *session;
211 struct hlist_node *walk;
212
James Chapmanf7faffa2010-04-02 06:18:49 +0000213 /* In L2TPv3, session_ids are unique over all tunnels and we
214 * sometimes need to look them up before we know the
215 * tunnel.
216 */
217 if (tunnel == NULL)
218 return l2tp_session_find_2(net, session_id);
219
220 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000221 read_lock_bh(&tunnel->hlist_lock);
222 hlist_for_each_entry(session, walk, session_list, hlist) {
223 if (session->session_id == session_id) {
224 read_unlock_bh(&tunnel->hlist_lock);
225 return session;
226 }
227 }
228 read_unlock_bh(&tunnel->hlist_lock);
229
230 return NULL;
231}
232EXPORT_SYMBOL_GPL(l2tp_session_find);
233
234struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
235{
236 int hash;
237 struct hlist_node *walk;
238 struct l2tp_session *session;
239 int count = 0;
240
241 read_lock_bh(&tunnel->hlist_lock);
242 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
243 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
244 if (++count > nth) {
245 read_unlock_bh(&tunnel->hlist_lock);
246 return session;
247 }
248 }
249 }
250
251 read_unlock_bh(&tunnel->hlist_lock);
252
253 return NULL;
254}
255EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
256
James Chapman309795f2010-04-02 06:19:10 +0000257/* Lookup a session by interface name.
258 * This is very inefficient but is only used by management interfaces.
259 */
260struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
261{
262 struct l2tp_net *pn = l2tp_pernet(net);
263 int hash;
264 struct hlist_node *walk;
265 struct l2tp_session *session;
266
James Chapmane02d4942010-04-02 06:19:16 +0000267 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000268 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
James Chapmane02d4942010-04-02 06:19:16 +0000269 hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000270 if (!strcmp(session->ifname, ifname)) {
James Chapmane02d4942010-04-02 06:19:16 +0000271 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000272 return session;
273 }
274 }
275 }
276
James Chapmane02d4942010-04-02 06:19:16 +0000277 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000278
279 return NULL;
280}
281EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
282
James Chapmanfd558d12010-04-02 06:18:33 +0000283/* Lookup a tunnel by id
284 */
285struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
286{
287 struct l2tp_tunnel *tunnel;
288 struct l2tp_net *pn = l2tp_pernet(net);
289
James Chapmane02d4942010-04-02 06:19:16 +0000290 rcu_read_lock_bh();
291 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000292 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000293 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000294 return tunnel;
295 }
296 }
James Chapmane02d4942010-04-02 06:19:16 +0000297 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000298
299 return NULL;
300}
301EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
302
303struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
304{
305 struct l2tp_net *pn = l2tp_pernet(net);
306 struct l2tp_tunnel *tunnel;
307 int count = 0;
308
James Chapmane02d4942010-04-02 06:19:16 +0000309 rcu_read_lock_bh();
310 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000311 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000312 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000313 return tunnel;
314 }
315 }
316
James Chapmane02d4942010-04-02 06:19:16 +0000317 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000318
319 return NULL;
320}
321EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
322
323/*****************************************************************************
324 * Receive data handling
325 *****************************************************************************/
326
327/* Queue a skb in order. We come here only if the skb has an L2TP sequence
328 * number.
329 */
330static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
331{
332 struct sk_buff *skbp;
333 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000334 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapman5de7aee2012-04-29 21:48:46 +0000335 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000336
337 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000338 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000339 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
340 if (L2TP_SKB_CB(skbp)->ns > ns) {
341 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000342 l2tp_dbg(session, L2TP_MSG_SEQ,
343 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
344 session->name, ns, L2TP_SKB_CB(skbp)->ns,
345 skb_queue_len(&session->reorder_q));
James Chapman5de7aee2012-04-29 21:48:46 +0000346 u64_stats_update_begin(&sstats->syncp);
347 sstats->rx_oos_packets++;
348 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000349 goto out;
350 }
351 }
352
353 __skb_queue_tail(&session->reorder_q, skb);
354
355out:
356 spin_unlock_bh(&session->reorder_q.lock);
357}
358
359/* Dequeue a single skb.
360 */
361static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
362{
363 struct l2tp_tunnel *tunnel = session->tunnel;
364 int length = L2TP_SKB_CB(skb)->length;
James Chapman5de7aee2012-04-29 21:48:46 +0000365 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000366
367 /* We're about to requeue the skb, so return resources
368 * to its current owner (a socket receive buffer).
369 */
370 skb_orphan(skb);
371
James Chapman5de7aee2012-04-29 21:48:46 +0000372 tstats = &tunnel->stats;
373 u64_stats_update_begin(&tstats->syncp);
374 sstats = &session->stats;
375 u64_stats_update_begin(&sstats->syncp);
376 tstats->rx_packets++;
377 tstats->rx_bytes += length;
378 sstats->rx_packets++;
379 sstats->rx_bytes += length;
380 u64_stats_update_end(&tstats->syncp);
381 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000382
383 if (L2TP_SKB_CB(skb)->has_seq) {
384 /* Bump our Nr */
385 session->nr++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000386 if (tunnel->version == L2TP_HDR_VER_2)
387 session->nr &= 0xffff;
388 else
389 session->nr &= 0xffffff;
390
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000391 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
392 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000393 }
394
395 /* call private receive handler */
396 if (session->recv_skb != NULL)
397 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
398 else
399 kfree_skb(skb);
400
401 if (session->deref)
402 (*session->deref)(session);
403}
404
405/* Dequeue skbs from the session's reorder_q, subject to packet order.
406 * Skbs that have been in the queue for too long are simply discarded.
407 */
408static void l2tp_recv_dequeue(struct l2tp_session *session)
409{
410 struct sk_buff *skb;
411 struct sk_buff *tmp;
James Chapman5de7aee2012-04-29 21:48:46 +0000412 struct l2tp_stats *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +0000413
414 /* If the pkt at the head of the queue has the nr that we
415 * expect to send up next, dequeue it and any other
416 * in-sequence packets behind it.
417 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000418start:
James Chapmanfd558d12010-04-02 06:18:33 +0000419 spin_lock_bh(&session->reorder_q.lock);
James Chapman5de7aee2012-04-29 21:48:46 +0000420 sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000421 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
422 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
James Chapman5de7aee2012-04-29 21:48:46 +0000423 u64_stats_update_begin(&sstats->syncp);
424 sstats->rx_seq_discards++;
425 sstats->rx_errors++;
426 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000427 l2tp_dbg(session, L2TP_MSG_SEQ,
428 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
429 session->name, L2TP_SKB_CB(skb)->ns,
430 L2TP_SKB_CB(skb)->length, session->nr,
431 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000432 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000433 __skb_unlink(skb, &session->reorder_q);
434 kfree_skb(skb);
435 if (session->deref)
436 (*session->deref)(session);
437 continue;
438 }
439
440 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000441 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000442 l2tp_dbg(session, L2TP_MSG_SEQ,
443 "%s: advancing nr to next pkt: %u -> %u",
444 session->name, session->nr,
445 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000446 session->reorder_skip = 0;
447 session->nr = L2TP_SKB_CB(skb)->ns;
448 }
James Chapmanfd558d12010-04-02 06:18:33 +0000449 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000450 l2tp_dbg(session, L2TP_MSG_SEQ,
451 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
452 session->name, L2TP_SKB_CB(skb)->ns,
453 L2TP_SKB_CB(skb)->length, session->nr,
454 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000455 goto out;
456 }
457 }
458 __skb_unlink(skb, &session->reorder_q);
459
460 /* Process the skb. We release the queue lock while we
461 * do so to let other contexts process the queue.
462 */
463 spin_unlock_bh(&session->reorder_q.lock);
464 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000465 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000466 }
467
468out:
469 spin_unlock_bh(&session->reorder_q.lock);
470}
471
472static inline int l2tp_verify_udp_checksum(struct sock *sk,
473 struct sk_buff *skb)
474{
475 struct udphdr *uh = udp_hdr(skb);
476 u16 ulen = ntohs(uh->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000477 __wsum psum;
478
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000479 if (sk->sk_no_check || skb_csum_unnecessary(skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000480 return 0;
481
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000482#if IS_ENABLED(CONFIG_IPV6)
483 if (sk->sk_family == PF_INET6) {
484 if (!uh->check) {
485 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
486 return 1;
487 }
488 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
489 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
490 &ipv6_hdr(skb)->daddr, ulen,
491 IPPROTO_UDP, skb->csum)) {
492 skb->ip_summed = CHECKSUM_UNNECESSARY;
493 return 0;
494 }
495 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
496 &ipv6_hdr(skb)->daddr,
497 skb->len, IPPROTO_UDP,
498 0));
499 } else
500#endif
501 {
502 struct inet_sock *inet;
503 if (!uh->check)
504 return 0;
505 inet = inet_sk(sk);
506 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
507 ulen, IPPROTO_UDP, 0);
James Chapmanfd558d12010-04-02 06:18:33 +0000508
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000509 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
510 !csum_fold(csum_add(psum, skb->csum)))
511 return 0;
512 skb->csum = psum;
513 }
James Chapmanfd558d12010-04-02 06:18:33 +0000514
515 return __skb_checksum_complete(skb);
516}
517
James Chapmanf7faffa2010-04-02 06:18:49 +0000518/* Do receive processing of L2TP data frames. We handle both L2TPv2
519 * and L2TPv3 data frames here.
520 *
521 * L2TPv2 Data Message Header
522 *
523 * 0 1 2 3
524 * 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
525 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
526 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
527 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
528 * | Tunnel ID | Session ID |
529 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 * | Ns (opt) | Nr (opt) |
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * | Offset Size (opt) | Offset pad... (opt)
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 *
535 * Data frames are marked by T=0. All other fields are the same as
536 * those in L2TP control frames.
537 *
538 * L2TPv3 Data Message Header
539 *
540 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
541 * | L2TP Session Header |
542 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
543 * | L2-Specific Sublayer |
544 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
545 * | Tunnel Payload ...
546 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
547 *
548 * L2TPv3 Session Header Over IP
549 *
550 * 0 1 2 3
551 * 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
552 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553 * | Session ID |
554 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
555 * | Cookie (optional, maximum 64 bits)...
556 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
557 * |
558 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
559 *
560 * L2TPv3 L2-Specific Sublayer Format
561 *
562 * 0 1 2 3
563 * 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
564 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
565 * |x|S|x|x|x|x|x|x| Sequence Number |
566 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 *
568 * Cookie value, sublayer format and offset (pad) are negotiated with
569 * the peer when the session is set up. Unlike L2TPv2, we do not need
570 * to parse the packet header to determine if optional fields are
571 * present.
572 *
573 * Caller must already have parsed the frame and determined that it is
574 * a data (not control) frame before coming here. Fields up to the
575 * session-id have already been parsed and ptr points to the data
576 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000577 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000578void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
579 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
580 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000581{
James Chapmanf7faffa2010-04-02 06:18:49 +0000582 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000583 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000584 u32 ns, nr;
James Chapman5de7aee2012-04-29 21:48:46 +0000585 struct l2tp_stats *sstats = &session->stats;
James Chapmanfd558d12010-04-02 06:18:33 +0000586
587 /* The ref count is increased since we now hold a pointer to
588 * the session. Take care to decrement the refcnt when exiting
589 * this function from now on...
590 */
591 l2tp_session_inc_refcount(session);
592 if (session->ref)
593 (*session->ref)(session);
594
James Chapmanf7faffa2010-04-02 06:18:49 +0000595 /* Parse and check optional cookie */
596 if (session->peer_cookie_len > 0) {
597 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000598 l2tp_info(tunnel, L2TP_MSG_DATA,
599 "%s: cookie mismatch (%u/%u). Discarding.\n",
600 tunnel->name, tunnel->tunnel_id,
601 session->session_id);
James Chapman5de7aee2012-04-29 21:48:46 +0000602 u64_stats_update_begin(&sstats->syncp);
603 sstats->rx_cookie_discards++;
604 u64_stats_update_end(&sstats->syncp);
James Chapmanf7faffa2010-04-02 06:18:49 +0000605 goto discard;
606 }
607 ptr += session->peer_cookie_len;
608 }
609
James Chapmanfd558d12010-04-02 06:18:33 +0000610 /* Handle the optional sequence numbers. Sequence numbers are
611 * in different places for L2TPv2 and L2TPv3.
612 *
613 * If we are the LAC, enable/disable sequence numbers under
614 * the control of the LNS. If no sequence numbers present but
615 * we were expecting them, discard frame.
616 */
617 ns = nr = 0;
618 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000619 if (tunnel->version == L2TP_HDR_VER_2) {
620 if (hdrflags & L2TP_HDRFLAG_S) {
621 ns = ntohs(*(__be16 *) ptr);
622 ptr += 2;
623 nr = ntohs(*(__be16 *) ptr);
624 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000625
James Chapmanf7faffa2010-04-02 06:18:49 +0000626 /* Store L2TP info in the skb */
627 L2TP_SKB_CB(skb)->ns = ns;
628 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000629
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000630 l2tp_dbg(session, L2TP_MSG_SEQ,
631 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
632 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000633 }
634 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
635 u32 l2h = ntohl(*(__be32 *) ptr);
636
637 if (l2h & 0x40000000) {
638 ns = l2h & 0x00ffffff;
639
640 /* Store L2TP info in the skb */
641 L2TP_SKB_CB(skb)->ns = ns;
642 L2TP_SKB_CB(skb)->has_seq = 1;
643
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000644 l2tp_dbg(session, L2TP_MSG_SEQ,
645 "%s: recv data ns=%u, session nr=%u\n",
646 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000647 }
James Chapmanfd558d12010-04-02 06:18:33 +0000648 }
649
James Chapmanf7faffa2010-04-02 06:18:49 +0000650 /* Advance past L2-specific header, if present */
651 ptr += session->l2specific_len;
652
James Chapmanfd558d12010-04-02 06:18:33 +0000653 if (L2TP_SKB_CB(skb)->has_seq) {
654 /* Received a packet with sequence numbers. If we're the LNS,
655 * check if we sre sending sequence numbers and if not,
656 * configure it so.
657 */
658 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000659 l2tp_info(session, L2TP_MSG_SEQ,
660 "%s: requested to enable seq numbers by LNS\n",
661 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000662 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000663 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000664 }
665 } else {
666 /* No sequence numbers.
667 * If user has configured mandatory sequence numbers, discard.
668 */
669 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000670 l2tp_warn(session, L2TP_MSG_SEQ,
671 "%s: recv data has no seq numbers when required. Discarding.\n",
672 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000673 u64_stats_update_begin(&sstats->syncp);
674 sstats->rx_seq_discards++;
675 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000676 goto discard;
677 }
678
679 /* If we're the LAC and we're sending sequence numbers, the
680 * LNS has requested that we no longer send sequence numbers.
681 * If we're the LNS and we're sending sequence numbers, the
682 * LAC is broken. Discard the frame.
683 */
684 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000685 l2tp_info(session, L2TP_MSG_SEQ,
686 "%s: requested to disable seq numbers by LNS\n",
687 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000688 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000689 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000690 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000691 l2tp_warn(session, L2TP_MSG_SEQ,
692 "%s: recv data has no seq numbers when required. Discarding.\n",
693 session->name);
James Chapman5de7aee2012-04-29 21:48:46 +0000694 u64_stats_update_begin(&sstats->syncp);
695 sstats->rx_seq_discards++;
696 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000697 goto discard;
698 }
699 }
700
James Chapmanf7faffa2010-04-02 06:18:49 +0000701 /* Session data offset is handled differently for L2TPv2 and
702 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
703 * the header. For L2TPv3, the offset is negotiated using AVPs
704 * in the session setup control protocol.
705 */
706 if (tunnel->version == L2TP_HDR_VER_2) {
707 /* If offset bit set, skip it. */
708 if (hdrflags & L2TP_HDRFLAG_O) {
709 offset = ntohs(*(__be16 *)ptr);
710 ptr += 2 + offset;
711 }
712 } else
713 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000714
715 offset = ptr - optr;
716 if (!pskb_may_pull(skb, offset))
717 goto discard;
718
719 __skb_pull(skb, offset);
720
721 /* If caller wants to process the payload before we queue the
722 * packet, do so now.
723 */
724 if (payload_hook)
725 if ((*payload_hook)(skb))
726 goto discard;
727
728 /* Prepare skb for adding to the session's reorder_q. Hold
729 * packets for max reorder_timeout or 1 second if not
730 * reordering.
731 */
732 L2TP_SKB_CB(skb)->length = length;
733 L2TP_SKB_CB(skb)->expires = jiffies +
734 (session->reorder_timeout ? session->reorder_timeout : HZ);
735
736 /* Add packet to the session's receive queue. Reordering is done here, if
737 * enabled. Saved L2TP protocol info is stored in skb->sb[].
738 */
739 if (L2TP_SKB_CB(skb)->has_seq) {
740 if (session->reorder_timeout != 0) {
741 /* Packet reordering enabled. Add skb to session's
742 * reorder queue, in order of ns.
743 */
744 l2tp_recv_queue_skb(session, skb);
745 } else {
746 /* Packet reordering disabled. Discard out-of-sequence
747 * packets
748 */
749 if (L2TP_SKB_CB(skb)->ns != session->nr) {
James Chapman5de7aee2012-04-29 21:48:46 +0000750 u64_stats_update_begin(&sstats->syncp);
751 sstats->rx_seq_discards++;
752 u64_stats_update_end(&sstats->syncp);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000753 l2tp_dbg(session, L2TP_MSG_SEQ,
754 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
755 session->name, L2TP_SKB_CB(skb)->ns,
756 L2TP_SKB_CB(skb)->length, session->nr,
757 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000758 goto discard;
759 }
760 skb_queue_tail(&session->reorder_q, skb);
761 }
762 } else {
763 /* No sequence numbers. Add the skb to the tail of the
764 * reorder queue. This ensures that it will be
765 * delivered after all previous sequenced skbs.
766 */
767 skb_queue_tail(&session->reorder_q, skb);
768 }
769
770 /* Try to dequeue as many skbs from reorder_q as we can. */
771 l2tp_recv_dequeue(session);
772
773 l2tp_session_dec_refcount(session);
774
James Chapmanf7faffa2010-04-02 06:18:49 +0000775 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000776
777discard:
James Chapman5de7aee2012-04-29 21:48:46 +0000778 u64_stats_update_begin(&sstats->syncp);
779 sstats->rx_errors++;
780 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000781 kfree_skb(skb);
782
783 if (session->deref)
784 (*session->deref)(session);
785
786 l2tp_session_dec_refcount(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000787}
788EXPORT_SYMBOL(l2tp_recv_common);
789
790/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
791 * here. The skb is not on a list when we get here.
792 * Returns 0 if the packet was a data packet and was successfully passed on.
793 * Returns 1 if the packet was not a good data packet and could not be
794 * forwarded. All such packets are passed up to userspace to deal with.
795 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000796static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
797 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000798{
799 struct l2tp_session *session = NULL;
800 unsigned char *ptr, *optr;
801 u16 hdrflags;
802 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000803 u16 version;
804 int length;
James Chapman5de7aee2012-04-29 21:48:46 +0000805 struct l2tp_stats *tstats;
James Chapmanf7faffa2010-04-02 06:18:49 +0000806
807 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
808 goto discard_bad_csum;
809
810 /* UDP always verifies the packet length. */
811 __skb_pull(skb, sizeof(struct udphdr));
812
813 /* Short packet? */
814 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000815 l2tp_info(tunnel, L2TP_MSG_DATA,
816 "%s: recv short packet (len=%d)\n",
817 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000818 goto error;
819 }
820
James Chapmanf7faffa2010-04-02 06:18:49 +0000821 /* Trace packet contents, if enabled */
822 if (tunnel->debug & L2TP_MSG_DATA) {
823 length = min(32u, skb->len);
824 if (!pskb_may_pull(skb, length))
825 goto error;
826
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000827 pr_debug("%s: recv\n", tunnel->name);
828 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000829 }
830
Eric Dumazete50e7052011-11-08 13:59:44 -0500831 /* Point to L2TP header */
832 optr = ptr = skb->data;
833
James Chapmanf7faffa2010-04-02 06:18:49 +0000834 /* Get L2TP header flags */
835 hdrflags = ntohs(*(__be16 *) ptr);
836
837 /* Check protocol version */
838 version = hdrflags & L2TP_HDR_VER_MASK;
839 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000840 l2tp_info(tunnel, L2TP_MSG_DATA,
841 "%s: recv protocol version mismatch: got %d expected %d\n",
842 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000843 goto error;
844 }
845
846 /* Get length of L2TP packet */
847 length = skb->len;
848
849 /* If type is control packet, it is handled by userspace. */
850 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000851 l2tp_dbg(tunnel, L2TP_MSG_DATA,
852 "%s: recv control packet, len=%d\n",
853 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000854 goto error;
855 }
856
857 /* Skip flags */
858 ptr += 2;
859
860 if (tunnel->version == L2TP_HDR_VER_2) {
861 /* If length is present, skip it */
862 if (hdrflags & L2TP_HDRFLAG_L)
863 ptr += 2;
864
865 /* Extract tunnel and session ID */
866 tunnel_id = ntohs(*(__be16 *) ptr);
867 ptr += 2;
868 session_id = ntohs(*(__be16 *) ptr);
869 ptr += 2;
870 } else {
871 ptr += 2; /* skip reserved bits */
872 tunnel_id = tunnel->tunnel_id;
873 session_id = ntohl(*(__be32 *) ptr);
874 ptr += 4;
875 }
876
877 /* Find the session context */
878 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000879 if (!session || !session->recv_skb) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000880 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000881 l2tp_info(tunnel, L2TP_MSG_DATA,
882 "%s: no session found (%u/%u). Passing up.\n",
883 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000884 goto error;
885 }
886
887 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
James Chapmanfd558d12010-04-02 06:18:33 +0000888
889 return 0;
890
891discard_bad_csum:
892 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
893 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
James Chapman5de7aee2012-04-29 21:48:46 +0000894 tstats = &tunnel->stats;
895 u64_stats_update_begin(&tstats->syncp);
896 tstats->rx_errors++;
897 u64_stats_update_end(&tstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +0000898 kfree_skb(skb);
899
900 return 0;
901
902error:
903 /* Put UDP header back */
904 __skb_push(skb, sizeof(struct udphdr));
905
906 return 1;
907}
James Chapmanfd558d12010-04-02 06:18:33 +0000908
909/* UDP encapsulation receive handler. See net/ipv4/udp.c.
910 * Return codes:
911 * 0 : success.
912 * <0: error
913 * >0: skb should be passed up to userspace as UDP.
914 */
915int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
916{
917 struct l2tp_tunnel *tunnel;
918
919 tunnel = l2tp_sock_to_tunnel(sk);
920 if (tunnel == NULL)
921 goto pass_up;
922
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000923 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
924 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000925
926 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
927 goto pass_up_put;
928
929 sock_put(sk);
930 return 0;
931
932pass_up_put:
933 sock_put(sk);
934pass_up:
935 return 1;
936}
937EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
938
939/************************************************************************
940 * Transmit handling
941 ***********************************************************************/
942
943/* Build an L2TP header for the session into the buffer provided.
944 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000945static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000946{
James Chapmanf7faffa2010-04-02 06:18:49 +0000947 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000948 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000949 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000950 u16 flags = L2TP_HDR_VER_2;
951 u32 tunnel_id = tunnel->peer_tunnel_id;
952 u32 session_id = session->peer_session_id;
953
954 if (session->send_seq)
955 flags |= L2TP_HDRFLAG_S;
956
957 /* Setup L2TP header. */
958 *bufp++ = htons(flags);
959 *bufp++ = htons(tunnel_id);
960 *bufp++ = htons(session_id);
961 if (session->send_seq) {
962 *bufp++ = htons(session->ns);
963 *bufp++ = 0;
964 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000965 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000966 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
967 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000968 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000969
970 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000971}
972
James Chapmanf7faffa2010-04-02 06:18:49 +0000973static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000974{
James Chapman0d767512010-04-02 06:19:00 +0000975 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000976 char *bufp = buf;
977 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000978
James Chapman0d767512010-04-02 06:19:00 +0000979 /* Setup L2TP header. The header differs slightly for UDP and
980 * IP encapsulations. For UDP, there is 4 bytes of flags.
981 */
982 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
983 u16 flags = L2TP_HDR_VER_3;
984 *((__be16 *) bufp) = htons(flags);
985 bufp += 2;
986 *((__be16 *) bufp) = 0;
987 bufp += 2;
988 }
989
James Chapmanf7faffa2010-04-02 06:18:49 +0000990 *((__be32 *) bufp) = htonl(session->peer_session_id);
991 bufp += 4;
992 if (session->cookie_len) {
993 memcpy(bufp, &session->cookie[0], session->cookie_len);
994 bufp += session->cookie_len;
995 }
996 if (session->l2specific_len) {
997 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
998 u32 l2h = 0;
999 if (session->send_seq) {
1000 l2h = 0x40000000 | session->ns;
1001 session->ns++;
1002 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001003 l2tp_dbg(session, L2TP_MSG_SEQ,
1004 "%s: updated ns to %u\n",
1005 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001006 }
1007
1008 *((__be32 *) bufp) = htonl(l2h);
1009 }
1010 bufp += session->l2specific_len;
1011 }
1012 if (session->offset)
1013 bufp += session->offset;
1014
1015 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001016}
James Chapmanfd558d12010-04-02 06:18:33 +00001017
stephen hemmingerfc130842010-10-21 07:50:46 +00001018static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001019 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001020{
1021 struct l2tp_tunnel *tunnel = session->tunnel;
1022 unsigned int len = skb->len;
1023 int error;
James Chapman5de7aee2012-04-29 21:48:46 +00001024 struct l2tp_stats *tstats, *sstats;
James Chapmanfd558d12010-04-02 06:18:33 +00001025
1026 /* Debug */
1027 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001028 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1029 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001030 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001031 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1032 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001033
1034 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001035 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1036 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001037
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001038 pr_debug("%s: xmit\n", session->name);
1039 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1040 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001041 }
1042
1043 /* Queue the packet to IP for output */
Shan Wei4e15ed42010-04-15 16:43:08 +00001044 skb->local_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001045#if IS_ENABLED(CONFIG_IPV6)
1046 if (skb->sk->sk_family == PF_INET6)
1047 error = inet6_csk_xmit(skb, NULL);
1048 else
1049#endif
1050 error = ip_queue_xmit(skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001051
1052 /* Update stats */
James Chapman5de7aee2012-04-29 21:48:46 +00001053 tstats = &tunnel->stats;
1054 u64_stats_update_begin(&tstats->syncp);
1055 sstats = &session->stats;
1056 u64_stats_update_begin(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001057 if (error >= 0) {
James Chapman5de7aee2012-04-29 21:48:46 +00001058 tstats->tx_packets++;
1059 tstats->tx_bytes += len;
1060 sstats->tx_packets++;
1061 sstats->tx_bytes += len;
James Chapmanfd558d12010-04-02 06:18:33 +00001062 } else {
James Chapman5de7aee2012-04-29 21:48:46 +00001063 tstats->tx_errors++;
1064 sstats->tx_errors++;
James Chapmanfd558d12010-04-02 06:18:33 +00001065 }
James Chapman5de7aee2012-04-29 21:48:46 +00001066 u64_stats_update_end(&tstats->syncp);
1067 u64_stats_update_end(&sstats->syncp);
James Chapmanfd558d12010-04-02 06:18:33 +00001068
1069 return 0;
1070}
James Chapmanfd558d12010-04-02 06:18:33 +00001071
1072/* Automatically called when the skb is freed.
1073 */
1074static void l2tp_sock_wfree(struct sk_buff *skb)
1075{
1076 sock_put(skb->sk);
1077}
1078
1079/* For data skbs that we transmit, we associate with the tunnel socket
1080 * but don't do accounting.
1081 */
1082static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1083{
1084 sock_hold(sk);
1085 skb->sk = sk;
1086 skb->destructor = l2tp_sock_wfree;
1087}
1088
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001089#if IS_ENABLED(CONFIG_IPV6)
1090static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1091 int udp_len)
1092{
1093 struct ipv6_pinfo *np = inet6_sk(sk);
1094 struct udphdr *uh = udp_hdr(skb);
1095
1096 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1097 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1098 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1099 skb->ip_summed = CHECKSUM_UNNECESSARY;
1100 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1101 IPPROTO_UDP, csum);
1102 if (uh->check == 0)
1103 uh->check = CSUM_MANGLED_0;
1104 } else {
1105 skb->ip_summed = CHECKSUM_PARTIAL;
1106 skb->csum_start = skb_transport_header(skb) - skb->head;
1107 skb->csum_offset = offsetof(struct udphdr, check);
1108 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1109 udp_len, IPPROTO_UDP, 0);
1110 }
1111}
1112#endif
1113
James Chapmanfd558d12010-04-02 06:18:33 +00001114/* If caller requires the skb to have a ppp header, the header must be
1115 * inserted in the skb data before calling this function.
1116 */
1117int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1118{
1119 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001120 struct l2tp_tunnel *tunnel = session->tunnel;
1121 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001122 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001123 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001124 struct inet_sock *inet;
1125 __wsum csum;
1126 int old_headroom;
1127 int new_headroom;
1128 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001129 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1130 int udp_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001131
1132 /* Check that there's enough headroom in the skb to insert IP,
1133 * UDP and L2TP headers. If not enough, expand it to
1134 * make room. Adjust truesize.
1135 */
1136 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001137 uhlen + hdr_len;
James Chapmanfd558d12010-04-02 06:18:33 +00001138 old_headroom = skb_headroom(skb);
Eric Dumazet835acf52011-10-07 05:35:46 +00001139 if (skb_cow_head(skb, headroom)) {
1140 dev_kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +00001141 goto abort;
Eric Dumazet835acf52011-10-07 05:35:46 +00001142 }
James Chapmanfd558d12010-04-02 06:18:33 +00001143
1144 new_headroom = skb_headroom(skb);
1145 skb_orphan(skb);
1146 skb->truesize += new_headroom - old_headroom;
1147
1148 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001149 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001150
James Chapman0d767512010-04-02 06:19:00 +00001151 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001152 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1153 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1154 IPSKB_REROUTED);
1155 nf_reset(skb);
1156
David S. Miller6af88da2011-05-08 13:45:20 -07001157 bh_lock_sock(sk);
1158 if (sock_owned_by_user(sk)) {
1159 dev_kfree_skb(skb);
1160 goto out_unlock;
1161 }
1162
James Chapmanfd558d12010-04-02 06:18:33 +00001163 /* Get routing info from the tunnel socket */
1164 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001165 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001166
David S. Millerd9d8da82011-05-06 22:23:20 -07001167 inet = inet_sk(sk);
1168 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001169 switch (tunnel->encap) {
1170 case L2TP_ENCAPTYPE_UDP:
1171 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001172 __skb_push(skb, sizeof(*uh));
1173 skb_reset_transport_header(skb);
1174 uh = udp_hdr(skb);
1175 uh->source = inet->inet_sport;
1176 uh->dest = inet->inet_dport;
1177 udp_len = uhlen + hdr_len + data_len;
1178 uh->len = htons(udp_len);
1179 uh->check = 0;
1180
1181 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001182#if IS_ENABLED(CONFIG_IPV6)
1183 if (sk->sk_family == PF_INET6)
1184 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1185 else
1186#endif
James Chapman0d767512010-04-02 06:19:00 +00001187 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1188 skb->ip_summed = CHECKSUM_NONE;
1189 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1190 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1191 skb->ip_summed = CHECKSUM_COMPLETE;
1192 csum = skb_checksum(skb, 0, udp_len, 0);
1193 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1194 inet->inet_daddr,
1195 udp_len, IPPROTO_UDP, csum);
1196 if (uh->check == 0)
1197 uh->check = CSUM_MANGLED_0;
1198 } else {
1199 skb->ip_summed = CHECKSUM_PARTIAL;
1200 skb->csum_start = skb_transport_header(skb) - skb->head;
1201 skb->csum_offset = offsetof(struct udphdr, check);
1202 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1203 inet->inet_daddr,
1204 udp_len, IPPROTO_UDP, 0);
1205 }
1206 break;
1207
1208 case L2TP_ENCAPTYPE_IP:
1209 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001210 }
1211
James Chapman0d767512010-04-02 06:19:00 +00001212 l2tp_skb_set_owner_w(skb, sk);
1213
David S. Millerd9d8da82011-05-06 22:23:20 -07001214 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001215out_unlock:
1216 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001217
1218abort:
1219 return 0;
1220}
1221EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1222
1223/*****************************************************************************
1224 * Tinnel and session create/destroy.
1225 *****************************************************************************/
1226
1227/* Tunnel socket destruct hook.
1228 * The tunnel context is deleted only when all session sockets have been
1229 * closed.
1230 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001231static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001232{
1233 struct l2tp_tunnel *tunnel;
1234
1235 tunnel = sk->sk_user_data;
1236 if (tunnel == NULL)
1237 goto end;
1238
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001239 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001240
1241 /* Close all sessions */
1242 l2tp_tunnel_closeall(tunnel);
1243
James Chapman0d767512010-04-02 06:19:00 +00001244 switch (tunnel->encap) {
1245 case L2TP_ENCAPTYPE_UDP:
1246 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1247 (udp_sk(sk))->encap_type = 0;
1248 (udp_sk(sk))->encap_rcv = NULL;
1249 break;
1250 case L2TP_ENCAPTYPE_IP:
1251 break;
1252 }
James Chapmanfd558d12010-04-02 06:18:33 +00001253
1254 /* Remove hooks into tunnel socket */
1255 tunnel->sock = NULL;
1256 sk->sk_destruct = tunnel->old_sk_destruct;
1257 sk->sk_user_data = NULL;
1258
1259 /* Call the original destructor */
1260 if (sk->sk_destruct)
1261 (*sk->sk_destruct)(sk);
1262
1263 /* We're finished with the socket */
1264 l2tp_tunnel_dec_refcount(tunnel);
1265
1266end:
1267 return;
1268}
James Chapmanfd558d12010-04-02 06:18:33 +00001269
1270/* When the tunnel is closed, all the attached sessions need to go too.
1271 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001272static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001273{
1274 int hash;
1275 struct hlist_node *walk;
1276 struct hlist_node *tmp;
1277 struct l2tp_session *session;
1278
1279 BUG_ON(tunnel == NULL);
1280
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001281 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1282 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001283
1284 write_lock_bh(&tunnel->hlist_lock);
1285 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1286again:
1287 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1288 session = hlist_entry(walk, struct l2tp_session, hlist);
1289
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001290 l2tp_info(session, L2TP_MSG_CONTROL,
1291 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001292
1293 hlist_del_init(&session->hlist);
1294
1295 /* Since we should hold the sock lock while
1296 * doing any unbinding, we need to release the
1297 * lock we're holding before taking that lock.
1298 * Hold a reference to the sock so it doesn't
1299 * disappear as we're jumping between locks.
1300 */
1301 if (session->ref != NULL)
1302 (*session->ref)(session);
1303
1304 write_unlock_bh(&tunnel->hlist_lock);
1305
James Chapmanf7faffa2010-04-02 06:18:49 +00001306 if (tunnel->version != L2TP_HDR_VER_2) {
1307 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1308
James Chapmane02d4942010-04-02 06:19:16 +00001309 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1310 hlist_del_init_rcu(&session->global_hlist);
1311 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1312 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001313 }
1314
James Chapmanfd558d12010-04-02 06:18:33 +00001315 if (session->session_close != NULL)
1316 (*session->session_close)(session);
1317
1318 if (session->deref != NULL)
1319 (*session->deref)(session);
1320
1321 write_lock_bh(&tunnel->hlist_lock);
1322
1323 /* Now restart from the beginning of this hash
1324 * chain. We always remove a session from the
1325 * list so we are guaranteed to make forward
1326 * progress.
1327 */
1328 goto again;
1329 }
1330 }
1331 write_unlock_bh(&tunnel->hlist_lock);
1332}
James Chapmanfd558d12010-04-02 06:18:33 +00001333
1334/* Really kill the tunnel.
1335 * Come here only when all sessions have been cleared from the tunnel.
1336 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001337static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001338{
1339 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1340
1341 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1342 BUG_ON(tunnel->sock != NULL);
1343
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001344 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001345
1346 /* Remove from tunnel list */
James Chapmane02d4942010-04-02 06:19:16 +00001347 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1348 list_del_rcu(&tunnel->list);
1349 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1350 synchronize_rcu();
James Chapmanfd558d12010-04-02 06:18:33 +00001351
1352 atomic_dec(&l2tp_tunnel_count);
1353 kfree(tunnel);
1354}
James Chapmanfd558d12010-04-02 06:18:33 +00001355
James Chapman789a4a22010-04-02 06:19:40 +00001356/* Create a socket for the tunnel, if one isn't set up by
1357 * userspace. This is used for static tunnels where there is no
1358 * managing L2TP daemon.
1359 */
1360static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1361{
1362 int err = -EINVAL;
1363 struct sockaddr_in udp_addr;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001364#if IS_ENABLED(CONFIG_IPV6)
1365 struct sockaddr_in6 udp6_addr;
James Chapman5dac94e2012-04-29 21:48:55 +00001366 struct sockaddr_l2tpip6 ip6_addr;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001367#endif
James Chapman789a4a22010-04-02 06:19:40 +00001368 struct sockaddr_l2tpip ip_addr;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001369 struct socket *sock = NULL;
James Chapman789a4a22010-04-02 06:19:40 +00001370
1371 switch (cfg->encap) {
1372 case L2TP_ENCAPTYPE_UDP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001373#if IS_ENABLED(CONFIG_IPV6)
1374 if (cfg->local_ip6 && cfg->peer_ip6) {
1375 err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp);
1376 if (err < 0)
1377 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001378
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001379 sock = *sockp;
James Chapman789a4a22010-04-02 06:19:40 +00001380
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001381 memset(&udp6_addr, 0, sizeof(udp6_addr));
1382 udp6_addr.sin6_family = AF_INET6;
1383 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1384 sizeof(udp6_addr.sin6_addr));
1385 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1386 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1387 sizeof(udp6_addr));
1388 if (err < 0)
1389 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001390
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001391 udp6_addr.sin6_family = AF_INET6;
1392 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1393 sizeof(udp6_addr.sin6_addr));
1394 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1395 err = kernel_connect(sock,
1396 (struct sockaddr *) &udp6_addr,
1397 sizeof(udp6_addr), 0);
1398 if (err < 0)
1399 goto out;
1400 } else
1401#endif
1402 {
1403 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1404 if (err < 0)
1405 goto out;
1406
1407 sock = *sockp;
1408
1409 memset(&udp_addr, 0, sizeof(udp_addr));
1410 udp_addr.sin_family = AF_INET;
1411 udp_addr.sin_addr = cfg->local_ip;
1412 udp_addr.sin_port = htons(cfg->local_udp_port);
1413 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1414 sizeof(udp_addr));
1415 if (err < 0)
1416 goto out;
1417
1418 udp_addr.sin_family = AF_INET;
1419 udp_addr.sin_addr = cfg->peer_ip;
1420 udp_addr.sin_port = htons(cfg->peer_udp_port);
1421 err = kernel_connect(sock,
1422 (struct sockaddr *) &udp_addr,
1423 sizeof(udp_addr), 0);
1424 if (err < 0)
1425 goto out;
1426 }
James Chapman789a4a22010-04-02 06:19:40 +00001427
1428 if (!cfg->use_udp_checksums)
1429 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1430
1431 break;
1432
1433 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001434#if IS_ENABLED(CONFIG_IPV6)
1435 if (cfg->local_ip6 && cfg->peer_ip6) {
James Chapman5dac94e2012-04-29 21:48:55 +00001436 err = sock_create(AF_INET6, SOCK_DGRAM, IPPROTO_L2TP,
1437 sockp);
1438 if (err < 0)
1439 goto out;
1440
1441 sock = *sockp;
1442
1443 memset(&ip6_addr, 0, sizeof(ip6_addr));
1444 ip6_addr.l2tp_family = AF_INET6;
1445 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1446 sizeof(ip6_addr.l2tp_addr));
1447 ip6_addr.l2tp_conn_id = tunnel_id;
1448 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1449 sizeof(ip6_addr));
1450 if (err < 0)
1451 goto out;
1452
1453 ip6_addr.l2tp_family = AF_INET6;
1454 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1455 sizeof(ip6_addr.l2tp_addr));
1456 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1457 err = kernel_connect(sock,
1458 (struct sockaddr *) &ip6_addr,
1459 sizeof(ip6_addr), 0);
1460 if (err < 0)
1461 goto out;
1462 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001463#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001464 {
1465 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP,
1466 sockp);
1467 if (err < 0)
1468 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001469
James Chapman5dac94e2012-04-29 21:48:55 +00001470 sock = *sockp;
James Chapman789a4a22010-04-02 06:19:40 +00001471
James Chapman5dac94e2012-04-29 21:48:55 +00001472 memset(&ip_addr, 0, sizeof(ip_addr));
1473 ip_addr.l2tp_family = AF_INET;
1474 ip_addr.l2tp_addr = cfg->local_ip;
1475 ip_addr.l2tp_conn_id = tunnel_id;
1476 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1477 sizeof(ip_addr));
1478 if (err < 0)
1479 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001480
James Chapman5dac94e2012-04-29 21:48:55 +00001481 ip_addr.l2tp_family = AF_INET;
1482 ip_addr.l2tp_addr = cfg->peer_ip;
1483 ip_addr.l2tp_conn_id = peer_tunnel_id;
1484 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1485 sizeof(ip_addr), 0);
1486 if (err < 0)
1487 goto out;
1488 }
James Chapman789a4a22010-04-02 06:19:40 +00001489 break;
1490
1491 default:
1492 goto out;
1493 }
1494
1495out:
1496 if ((err < 0) && sock) {
1497 sock_release(sock);
1498 *sockp = NULL;
1499 }
1500
1501 return err;
1502}
1503
James Chapmanfd558d12010-04-02 06:18:33 +00001504int 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)
1505{
1506 struct l2tp_tunnel *tunnel = NULL;
1507 int err;
1508 struct socket *sock = NULL;
1509 struct sock *sk = NULL;
1510 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001511 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001512
1513 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001514 * the userspace L2TP daemon. If not specified, create a
1515 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001516 */
James Chapman789a4a22010-04-02 06:19:40 +00001517 if (fd < 0) {
1518 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1519 if (err < 0)
1520 goto err;
1521 } else {
1522 err = -EBADF;
1523 sock = sockfd_lookup(fd, &err);
1524 if (!sock) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001525 pr_err("tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001526 tunnel_id, fd, err);
1527 goto err;
1528 }
James Chapmanfd558d12010-04-02 06:18:33 +00001529 }
1530
1531 sk = sock->sk;
1532
James Chapman0d767512010-04-02 06:19:00 +00001533 if (cfg != NULL)
1534 encap = cfg->encap;
1535
James Chapmanfd558d12010-04-02 06:18:33 +00001536 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001537 switch (encap) {
1538 case L2TP_ENCAPTYPE_UDP:
1539 err = -EPROTONOSUPPORT;
1540 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001541 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001542 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1543 goto err;
1544 }
1545 break;
1546 case L2TP_ENCAPTYPE_IP:
1547 err = -EPROTONOSUPPORT;
1548 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001549 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001550 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1551 goto err;
1552 }
1553 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001554 }
1555
1556 /* Check if this socket has already been prepped */
1557 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1558 if (tunnel != NULL) {
1559 /* This socket has already been prepped */
1560 err = -EBUSY;
1561 goto err;
1562 }
1563
James Chapmanfd558d12010-04-02 06:18:33 +00001564 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1565 if (tunnel == NULL) {
1566 err = -ENOMEM;
1567 goto err;
1568 }
1569
1570 tunnel->version = version;
1571 tunnel->tunnel_id = tunnel_id;
1572 tunnel->peer_tunnel_id = peer_tunnel_id;
1573 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1574
1575 tunnel->magic = L2TP_TUNNEL_MAGIC;
1576 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1577 rwlock_init(&tunnel->hlist_lock);
1578
1579 /* The net we belong to */
1580 tunnel->l2tp_net = net;
1581 pn = l2tp_pernet(net);
1582
James Chapman0d767512010-04-02 06:19:00 +00001583 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001584 tunnel->debug = cfg->debug;
1585
1586 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001587 tunnel->encap = encap;
1588 if (encap == L2TP_ENCAPTYPE_UDP) {
1589 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1590 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1591 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001592#if IS_ENABLED(CONFIG_IPV6)
1593 if (sk->sk_family == PF_INET6)
1594 udpv6_encap_enable();
1595 else
1596#endif
Eric Dumazet447167b2012-04-11 23:05:28 +00001597 udp_encap_enable();
James Chapman0d767512010-04-02 06:19:00 +00001598 }
James Chapmanfd558d12010-04-02 06:18:33 +00001599
1600 sk->sk_user_data = tunnel;
1601
1602 /* Hook on the tunnel socket destructor so that we can cleanup
1603 * if the tunnel socket goes away.
1604 */
1605 tunnel->old_sk_destruct = sk->sk_destruct;
1606 sk->sk_destruct = &l2tp_tunnel_destruct;
1607 tunnel->sock = sk;
1608 sk->sk_allocation = GFP_ATOMIC;
1609
1610 /* Add tunnel to our list */
1611 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001612 atomic_inc(&l2tp_tunnel_count);
1613
1614 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001615 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001616 */
1617 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001618 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1619 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1620 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001621
1622 err = 0;
1623err:
1624 if (tunnelp)
1625 *tunnelp = tunnel;
1626
James Chapman789a4a22010-04-02 06:19:40 +00001627 /* If tunnel's socket was created by the kernel, it doesn't
1628 * have a file.
1629 */
1630 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001631 sockfd_put(sock);
1632
1633 return err;
1634}
1635EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1636
James Chapman309795f2010-04-02 06:19:10 +00001637/* This function is used by the netlink TUNNEL_DELETE command.
1638 */
1639int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1640{
1641 int err = 0;
James Chapman789a4a22010-04-02 06:19:40 +00001642 struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
James Chapman309795f2010-04-02 06:19:10 +00001643
1644 /* Force the tunnel socket to close. This will eventually
1645 * cause the tunnel to be deleted via the normal socket close
1646 * mechanisms when userspace closes the tunnel socket.
1647 */
James Chapman789a4a22010-04-02 06:19:40 +00001648 if (sock != NULL) {
1649 err = inet_shutdown(sock, 2);
1650
1651 /* If the tunnel's socket was created by the kernel,
1652 * close the socket here since the socket was not
1653 * created by userspace.
1654 */
1655 if (sock->file == NULL)
1656 err = inet_release(sock);
1657 }
James Chapman309795f2010-04-02 06:19:10 +00001658
1659 return err;
1660}
1661EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1662
James Chapmanfd558d12010-04-02 06:18:33 +00001663/* Really kill the session.
1664 */
1665void l2tp_session_free(struct l2tp_session *session)
1666{
1667 struct l2tp_tunnel *tunnel;
1668
1669 BUG_ON(atomic_read(&session->ref_count) != 0);
1670
1671 tunnel = session->tunnel;
1672 if (tunnel != NULL) {
1673 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1674
1675 /* Delete the session from the hash */
1676 write_lock_bh(&tunnel->hlist_lock);
1677 hlist_del_init(&session->hlist);
1678 write_unlock_bh(&tunnel->hlist_lock);
1679
James Chapmanf7faffa2010-04-02 06:18:49 +00001680 /* Unlink from the global hash if not L2TPv2 */
1681 if (tunnel->version != L2TP_HDR_VER_2) {
1682 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1683
James Chapmane02d4942010-04-02 06:19:16 +00001684 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1685 hlist_del_init_rcu(&session->global_hlist);
1686 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1687 synchronize_rcu();
James Chapmanf7faffa2010-04-02 06:18:49 +00001688 }
1689
James Chapmanfd558d12010-04-02 06:18:33 +00001690 if (session->session_id != 0)
1691 atomic_dec(&l2tp_session_count);
1692
1693 sock_put(tunnel->sock);
1694
1695 /* This will delete the tunnel context if this
1696 * is the last session on the tunnel.
1697 */
1698 session->tunnel = NULL;
1699 l2tp_tunnel_dec_refcount(tunnel);
1700 }
1701
1702 kfree(session);
1703
1704 return;
1705}
1706EXPORT_SYMBOL_GPL(l2tp_session_free);
1707
James Chapman309795f2010-04-02 06:19:10 +00001708/* This function is used by the netlink SESSION_DELETE command and by
1709 pseudowire modules.
1710 */
1711int l2tp_session_delete(struct l2tp_session *session)
1712{
1713 if (session->session_close != NULL)
1714 (*session->session_close)(session);
1715
1716 l2tp_session_dec_refcount(session);
1717
1718 return 0;
1719}
1720EXPORT_SYMBOL_GPL(l2tp_session_delete);
1721
1722
James Chapmanf7faffa2010-04-02 06:18:49 +00001723/* We come here whenever a session's send_seq, cookie_len or
1724 * l2specific_len parameters are set.
1725 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001726static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001727{
1728 if (version == L2TP_HDR_VER_2) {
1729 session->hdr_len = 6;
1730 if (session->send_seq)
1731 session->hdr_len += 4;
1732 } else {
James Chapman0d767512010-04-02 06:19:00 +00001733 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1734 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1735 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001736 }
1737
1738}
James Chapmanf7faffa2010-04-02 06:18:49 +00001739
James Chapmanfd558d12010-04-02 06:18:33 +00001740struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1741{
1742 struct l2tp_session *session;
1743
1744 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1745 if (session != NULL) {
1746 session->magic = L2TP_SESSION_MAGIC;
1747 session->tunnel = tunnel;
1748
1749 session->session_id = session_id;
1750 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001751 session->nr = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001752
1753 sprintf(&session->name[0], "sess %u/%u",
1754 tunnel->tunnel_id, session->session_id);
1755
1756 skb_queue_head_init(&session->reorder_q);
1757
1758 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001759 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001760
1761 /* Inherit debug options from tunnel */
1762 session->debug = tunnel->debug;
1763
1764 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001765 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001766 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001767 session->mtu = cfg->mtu;
1768 session->mru = cfg->mru;
1769 session->send_seq = cfg->send_seq;
1770 session->recv_seq = cfg->recv_seq;
1771 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001772 session->reorder_timeout = cfg->reorder_timeout;
1773 session->offset = cfg->offset;
1774 session->l2specific_type = cfg->l2specific_type;
1775 session->l2specific_len = cfg->l2specific_len;
1776 session->cookie_len = cfg->cookie_len;
1777 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1778 session->peer_cookie_len = cfg->peer_cookie_len;
1779 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001780 }
1781
James Chapmanf7faffa2010-04-02 06:18:49 +00001782 if (tunnel->version == L2TP_HDR_VER_2)
1783 session->build_header = l2tp_build_l2tpv2_header;
1784 else
1785 session->build_header = l2tp_build_l2tpv3_header;
1786
1787 l2tp_session_set_header_len(session, tunnel->version);
1788
James Chapmanfd558d12010-04-02 06:18:33 +00001789 /* Bump the reference count. The session context is deleted
1790 * only when this drops to zero.
1791 */
1792 l2tp_session_inc_refcount(session);
1793 l2tp_tunnel_inc_refcount(tunnel);
1794
1795 /* Ensure tunnel socket isn't deleted */
1796 sock_hold(tunnel->sock);
1797
1798 /* Add session to the tunnel's hash list */
1799 write_lock_bh(&tunnel->hlist_lock);
1800 hlist_add_head(&session->hlist,
1801 l2tp_session_id_hash(tunnel, session_id));
1802 write_unlock_bh(&tunnel->hlist_lock);
1803
James Chapmanf7faffa2010-04-02 06:18:49 +00001804 /* And to the global session list if L2TPv3 */
1805 if (tunnel->version != L2TP_HDR_VER_2) {
1806 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1807
James Chapmane02d4942010-04-02 06:19:16 +00001808 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1809 hlist_add_head_rcu(&session->global_hlist,
1810 l2tp_session_id_hash_2(pn, session_id));
1811 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001812 }
1813
James Chapmanfd558d12010-04-02 06:18:33 +00001814 /* Ignore management session in session count value */
1815 if (session->session_id != 0)
1816 atomic_inc(&l2tp_session_count);
1817 }
1818
1819 return session;
1820}
1821EXPORT_SYMBOL_GPL(l2tp_session_create);
1822
1823/*****************************************************************************
1824 * Init and cleanup
1825 *****************************************************************************/
1826
1827static __net_init int l2tp_init_net(struct net *net)
1828{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001829 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001830 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001831
James Chapmanfd558d12010-04-02 06:18:33 +00001832 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001833 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001834
James Chapmanf7faffa2010-04-02 06:18:49 +00001835 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1836 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1837
James Chapmane02d4942010-04-02 06:19:16 +00001838 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001839
James Chapmanfd558d12010-04-02 06:18:33 +00001840 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001841}
1842
1843static struct pernet_operations l2tp_net_ops = {
1844 .init = l2tp_init_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001845 .id = &l2tp_net_id,
1846 .size = sizeof(struct l2tp_net),
1847};
1848
1849static int __init l2tp_init(void)
1850{
1851 int rc = 0;
1852
1853 rc = register_pernet_device(&l2tp_net_ops);
1854 if (rc)
1855 goto out;
1856
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001857 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001858
1859out:
1860 return rc;
1861}
1862
1863static void __exit l2tp_exit(void)
1864{
1865 unregister_pernet_device(&l2tp_net_ops);
1866}
1867
1868module_init(l2tp_init);
1869module_exit(l2tp_exit);
1870
1871MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1872MODULE_DESCRIPTION("L2TP core");
1873MODULE_LICENSE("GPL");
1874MODULE_VERSION(L2TP_DRV_VERSION);
1875