]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/l2tp/l2tp_core.c
cpufreq: CPPC: Don't set transition_latency
[mirror_ubuntu-bionic-kernel.git] / net / l2tp / l2tp_core.c
1 /*
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/list.h>
26 #include <linux/rculist.h>
27 #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>
42 #include <linux/in.h>
43 #include <linux/ip.h>
44 #include <linux/udp.h>
45 #include <linux/l2tp.h>
46 #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>
55 #include <net/udp_tunnel.h>
56 #include <net/inet_common.h>
57 #include <net/xfrm.h>
58 #include <net/protocol.h>
59 #include <net/inet6_connection_sock.h>
60 #include <net/inet_ecn.h>
61 #include <net/ip6_route.h>
62 #include <net/ip6_checksum.h>
63
64 #include <asm/byteorder.h>
65 #include <linux/atomic.h>
66
67 #include "l2tp_core.h"
68
69 #define L2TP_DRV_VERSION "V2.0"
70
71 /* L2TP header constants */
72 #define L2TP_HDRFLAG_T 0x8000
73 #define L2TP_HDRFLAG_L 0x4000
74 #define L2TP_HDRFLAG_S 0x0800
75 #define L2TP_HDRFLAG_O 0x0200
76 #define L2TP_HDRFLAG_P 0x0100
77
78 #define L2TP_HDR_VER_MASK 0x000F
79 #define L2TP_HDR_VER_2 0x0002
80 #define L2TP_HDR_VER_3 0x0003
81
82 /* L2TPv3 default L2-specific sublayer */
83 #define L2TP_SLFLAG_S 0x40000000
84 #define L2TP_SL_SEQ_MASK 0x00ffffff
85
86 #define L2TP_HDR_SIZE_SEQ 10
87 #define L2TP_HDR_SIZE_NOSEQ 6
88
89 /* Default trace flags */
90 #define L2TP_DEFAULT_DEBUG_FLAGS 0
91
92 /* Private data stored for received packets in the skb.
93 */
94 struct l2tp_skb_cb {
95 u32 ns;
96 u16 has_seq;
97 u16 length;
98 unsigned long expires;
99 };
100
101 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102
103 static struct workqueue_struct *l2tp_wq;
104
105 /* per-net private data for this module */
106 static unsigned int l2tp_net_id;
107 struct l2tp_net {
108 struct list_head l2tp_tunnel_list;
109 spinlock_t l2tp_tunnel_list_lock;
110 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
111 spinlock_t l2tp_session_hlist_lock;
112 };
113
114 #if IS_ENABLED(CONFIG_IPV6)
115 static bool l2tp_sk_is_v6(struct sock *sk)
116 {
117 return sk->sk_family == PF_INET6 &&
118 !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
119 }
120 #endif
121
122 static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
123 {
124 return sk->sk_user_data;
125 }
126
127 static inline struct l2tp_net *l2tp_pernet(const struct net *net)
128 {
129 BUG_ON(!net);
130
131 return net_generic(net, l2tp_net_id);
132 }
133
134 /* Session hash global list for L2TPv3.
135 * The session_id SHOULD be random according to RFC3931, but several
136 * L2TP implementations use incrementing session_ids. So we do a real
137 * hash on the session_id, rather than a simple bitmask.
138 */
139 static inline struct hlist_head *
140 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
141 {
142 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
143
144 }
145
146 /* Session hash list.
147 * The session_id SHOULD be random according to RFC2661, but several
148 * L2TP implementations (Cisco and Microsoft) use incrementing
149 * session_ids. So we do a real hash on the session_id, rather than a
150 * simple bitmask.
151 */
152 static inline struct hlist_head *
153 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
154 {
155 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
156 }
157
158 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
159 {
160 sock_put(tunnel->sock);
161 /* the tunnel is freed in the socket destructor */
162 }
163 EXPORT_SYMBOL(l2tp_tunnel_free);
164
165 /* Lookup a tunnel. A new reference is held on the returned tunnel. */
166 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
167 {
168 const struct l2tp_net *pn = l2tp_pernet(net);
169 struct l2tp_tunnel *tunnel;
170
171 rcu_read_lock_bh();
172 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
173 if (tunnel->tunnel_id == tunnel_id) {
174 l2tp_tunnel_inc_refcount(tunnel);
175 rcu_read_unlock_bh();
176
177 return tunnel;
178 }
179 }
180 rcu_read_unlock_bh();
181
182 return NULL;
183 }
184 EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
185
186 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
187 {
188 const struct l2tp_net *pn = l2tp_pernet(net);
189 struct l2tp_tunnel *tunnel;
190 int count = 0;
191
192 rcu_read_lock_bh();
193 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
194 if (++count > nth) {
195 l2tp_tunnel_inc_refcount(tunnel);
196 rcu_read_unlock_bh();
197 return tunnel;
198 }
199 }
200 rcu_read_unlock_bh();
201
202 return NULL;
203 }
204 EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
205
206 /* Lookup a session. A new reference is held on the returned session. */
207 struct l2tp_session *l2tp_session_get(const struct net *net,
208 struct l2tp_tunnel *tunnel,
209 u32 session_id)
210 {
211 struct hlist_head *session_list;
212 struct l2tp_session *session;
213
214 if (!tunnel) {
215 struct l2tp_net *pn = l2tp_pernet(net);
216
217 session_list = l2tp_session_id_hash_2(pn, session_id);
218
219 rcu_read_lock_bh();
220 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
221 if (session->session_id == session_id) {
222 l2tp_session_inc_refcount(session);
223 rcu_read_unlock_bh();
224
225 return session;
226 }
227 }
228 rcu_read_unlock_bh();
229
230 return NULL;
231 }
232
233 session_list = l2tp_session_id_hash(tunnel, session_id);
234 read_lock_bh(&tunnel->hlist_lock);
235 hlist_for_each_entry(session, session_list, hlist) {
236 if (session->session_id == session_id) {
237 l2tp_session_inc_refcount(session);
238 read_unlock_bh(&tunnel->hlist_lock);
239
240 return session;
241 }
242 }
243 read_unlock_bh(&tunnel->hlist_lock);
244
245 return NULL;
246 }
247 EXPORT_SYMBOL_GPL(l2tp_session_get);
248
249 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
250 {
251 int hash;
252 struct l2tp_session *session;
253 int count = 0;
254
255 read_lock_bh(&tunnel->hlist_lock);
256 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
257 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
258 if (++count > nth) {
259 l2tp_session_inc_refcount(session);
260 read_unlock_bh(&tunnel->hlist_lock);
261 return session;
262 }
263 }
264 }
265
266 read_unlock_bh(&tunnel->hlist_lock);
267
268 return NULL;
269 }
270 EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
271
272 /* Lookup a session by interface name.
273 * This is very inefficient but is only used by management interfaces.
274 */
275 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
276 const char *ifname)
277 {
278 struct l2tp_net *pn = l2tp_pernet(net);
279 int hash;
280 struct l2tp_session *session;
281
282 rcu_read_lock_bh();
283 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
284 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
285 if (!strcmp(session->ifname, ifname)) {
286 l2tp_session_inc_refcount(session);
287 rcu_read_unlock_bh();
288
289 return session;
290 }
291 }
292 }
293
294 rcu_read_unlock_bh();
295
296 return NULL;
297 }
298 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
299
300 int l2tp_session_register(struct l2tp_session *session,
301 struct l2tp_tunnel *tunnel)
302 {
303 struct l2tp_session *session_walk;
304 struct hlist_head *g_head;
305 struct hlist_head *head;
306 struct l2tp_net *pn;
307 int err;
308
309 head = l2tp_session_id_hash(tunnel, session->session_id);
310
311 write_lock_bh(&tunnel->hlist_lock);
312 if (!tunnel->acpt_newsess) {
313 err = -ENODEV;
314 goto err_tlock;
315 }
316
317 hlist_for_each_entry(session_walk, head, hlist)
318 if (session_walk->session_id == session->session_id) {
319 err = -EEXIST;
320 goto err_tlock;
321 }
322
323 if (tunnel->version == L2TP_HDR_VER_3) {
324 pn = l2tp_pernet(tunnel->l2tp_net);
325 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
326 session->session_id);
327
328 spin_lock_bh(&pn->l2tp_session_hlist_lock);
329
330 hlist_for_each_entry(session_walk, g_head, global_hlist)
331 if (session_walk->session_id == session->session_id) {
332 err = -EEXIST;
333 goto err_tlock_pnlock;
334 }
335
336 l2tp_tunnel_inc_refcount(tunnel);
337 hlist_add_head_rcu(&session->global_hlist, g_head);
338
339 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
340 } else {
341 l2tp_tunnel_inc_refcount(tunnel);
342 }
343
344 hlist_add_head(&session->hlist, head);
345 write_unlock_bh(&tunnel->hlist_lock);
346
347 return 0;
348
349 err_tlock_pnlock:
350 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
351 err_tlock:
352 write_unlock_bh(&tunnel->hlist_lock);
353
354 return err;
355 }
356 EXPORT_SYMBOL_GPL(l2tp_session_register);
357
358 /*****************************************************************************
359 * Receive data handling
360 *****************************************************************************/
361
362 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
363 * number.
364 */
365 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
366 {
367 struct sk_buff *skbp;
368 struct sk_buff *tmp;
369 u32 ns = L2TP_SKB_CB(skb)->ns;
370
371 spin_lock_bh(&session->reorder_q.lock);
372 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
373 if (L2TP_SKB_CB(skbp)->ns > ns) {
374 __skb_queue_before(&session->reorder_q, skbp, skb);
375 l2tp_dbg(session, L2TP_MSG_SEQ,
376 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
377 session->name, ns, L2TP_SKB_CB(skbp)->ns,
378 skb_queue_len(&session->reorder_q));
379 atomic_long_inc(&session->stats.rx_oos_packets);
380 goto out;
381 }
382 }
383
384 __skb_queue_tail(&session->reorder_q, skb);
385
386 out:
387 spin_unlock_bh(&session->reorder_q.lock);
388 }
389
390 /* Dequeue a single skb.
391 */
392 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
393 {
394 struct l2tp_tunnel *tunnel = session->tunnel;
395 int length = L2TP_SKB_CB(skb)->length;
396
397 /* We're about to requeue the skb, so return resources
398 * to its current owner (a socket receive buffer).
399 */
400 skb_orphan(skb);
401
402 atomic_long_inc(&tunnel->stats.rx_packets);
403 atomic_long_add(length, &tunnel->stats.rx_bytes);
404 atomic_long_inc(&session->stats.rx_packets);
405 atomic_long_add(length, &session->stats.rx_bytes);
406
407 if (L2TP_SKB_CB(skb)->has_seq) {
408 /* Bump our Nr */
409 session->nr++;
410 session->nr &= session->nr_max;
411
412 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
413 session->name, session->nr);
414 }
415
416 /* call private receive handler */
417 if (session->recv_skb != NULL)
418 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
419 else
420 kfree_skb(skb);
421 }
422
423 /* Dequeue skbs from the session's reorder_q, subject to packet order.
424 * Skbs that have been in the queue for too long are simply discarded.
425 */
426 static void l2tp_recv_dequeue(struct l2tp_session *session)
427 {
428 struct sk_buff *skb;
429 struct sk_buff *tmp;
430
431 /* If the pkt at the head of the queue has the nr that we
432 * expect to send up next, dequeue it and any other
433 * in-sequence packets behind it.
434 */
435 start:
436 spin_lock_bh(&session->reorder_q.lock);
437 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
438 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
439 atomic_long_inc(&session->stats.rx_seq_discards);
440 atomic_long_inc(&session->stats.rx_errors);
441 l2tp_dbg(session, L2TP_MSG_SEQ,
442 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
443 session->name, L2TP_SKB_CB(skb)->ns,
444 L2TP_SKB_CB(skb)->length, session->nr,
445 skb_queue_len(&session->reorder_q));
446 session->reorder_skip = 1;
447 __skb_unlink(skb, &session->reorder_q);
448 kfree_skb(skb);
449 continue;
450 }
451
452 if (L2TP_SKB_CB(skb)->has_seq) {
453 if (session->reorder_skip) {
454 l2tp_dbg(session, L2TP_MSG_SEQ,
455 "%s: advancing nr to next pkt: %u -> %u",
456 session->name, session->nr,
457 L2TP_SKB_CB(skb)->ns);
458 session->reorder_skip = 0;
459 session->nr = L2TP_SKB_CB(skb)->ns;
460 }
461 if (L2TP_SKB_CB(skb)->ns != session->nr) {
462 l2tp_dbg(session, L2TP_MSG_SEQ,
463 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
464 session->name, L2TP_SKB_CB(skb)->ns,
465 L2TP_SKB_CB(skb)->length, session->nr,
466 skb_queue_len(&session->reorder_q));
467 goto out;
468 }
469 }
470 __skb_unlink(skb, &session->reorder_q);
471
472 /* Process the skb. We release the queue lock while we
473 * do so to let other contexts process the queue.
474 */
475 spin_unlock_bh(&session->reorder_q.lock);
476 l2tp_recv_dequeue_skb(session, skb);
477 goto start;
478 }
479
480 out:
481 spin_unlock_bh(&session->reorder_q.lock);
482 }
483
484 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
485 {
486 u32 nws;
487
488 if (nr >= session->nr)
489 nws = nr - session->nr;
490 else
491 nws = (session->nr_max + 1) - (session->nr - nr);
492
493 return nws < session->nr_window_size;
494 }
495
496 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
497 * acceptable, else non-zero.
498 */
499 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
500 {
501 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
502 /* Packet sequence number is outside allowed window.
503 * Discard it.
504 */
505 l2tp_dbg(session, L2TP_MSG_SEQ,
506 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
507 session->name, L2TP_SKB_CB(skb)->ns,
508 L2TP_SKB_CB(skb)->length, session->nr);
509 goto discard;
510 }
511
512 if (session->reorder_timeout != 0) {
513 /* Packet reordering enabled. Add skb to session's
514 * reorder queue, in order of ns.
515 */
516 l2tp_recv_queue_skb(session, skb);
517 goto out;
518 }
519
520 /* Packet reordering disabled. Discard out-of-sequence packets, while
521 * tracking the number if in-sequence packets after the first OOS packet
522 * is seen. After nr_oos_count_max in-sequence packets, reset the
523 * sequence number to re-enable packet reception.
524 */
525 if (L2TP_SKB_CB(skb)->ns == session->nr) {
526 skb_queue_tail(&session->reorder_q, skb);
527 } else {
528 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
529 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
530
531 if (nr_oos == nr_next)
532 session->nr_oos_count++;
533 else
534 session->nr_oos_count = 0;
535
536 session->nr_oos = nr_oos;
537 if (session->nr_oos_count > session->nr_oos_count_max) {
538 session->reorder_skip = 1;
539 l2tp_dbg(session, L2TP_MSG_SEQ,
540 "%s: %d oos packets received. Resetting sequence numbers\n",
541 session->name, session->nr_oos_count);
542 }
543 if (!session->reorder_skip) {
544 atomic_long_inc(&session->stats.rx_seq_discards);
545 l2tp_dbg(session, L2TP_MSG_SEQ,
546 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
547 session->name, L2TP_SKB_CB(skb)->ns,
548 L2TP_SKB_CB(skb)->length, session->nr,
549 skb_queue_len(&session->reorder_q));
550 goto discard;
551 }
552 skb_queue_tail(&session->reorder_q, skb);
553 }
554
555 out:
556 return 0;
557
558 discard:
559 return 1;
560 }
561
562 /* Do receive processing of L2TP data frames. We handle both L2TPv2
563 * and L2TPv3 data frames here.
564 *
565 * L2TPv2 Data Message Header
566 *
567 * 0 1 2 3
568 * 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
569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
572 * | Tunnel ID | Session ID |
573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
574 * | Ns (opt) | Nr (opt) |
575 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
576 * | Offset Size (opt) | Offset pad... (opt)
577 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
578 *
579 * Data frames are marked by T=0. All other fields are the same as
580 * those in L2TP control frames.
581 *
582 * L2TPv3 Data Message Header
583 *
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 * | L2TP Session Header |
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | L2-Specific Sublayer |
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * | Tunnel Payload ...
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 *
592 * L2TPv3 Session Header Over IP
593 *
594 * 0 1 2 3
595 * 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
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | Session ID |
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * | Cookie (optional, maximum 64 bits)...
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * |
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 *
604 * L2TPv3 L2-Specific Sublayer Format
605 *
606 * 0 1 2 3
607 * 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
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * |x|S|x|x|x|x|x|x| Sequence Number |
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 *
612 * Cookie value, sublayer format and offset (pad) are negotiated with
613 * the peer when the session is set up. Unlike L2TPv2, we do not need
614 * to parse the packet header to determine if optional fields are
615 * present.
616 *
617 * Caller must already have parsed the frame and determined that it is
618 * a data (not control) frame before coming here. Fields up to the
619 * session-id have already been parsed and ptr points to the data
620 * after the session-id.
621 */
622 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
623 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
624 int length, int (*payload_hook)(struct sk_buff *skb))
625 {
626 struct l2tp_tunnel *tunnel = session->tunnel;
627 int offset;
628 u32 ns, nr;
629
630 /* Parse and check optional cookie */
631 if (session->peer_cookie_len > 0) {
632 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
633 l2tp_info(tunnel, L2TP_MSG_DATA,
634 "%s: cookie mismatch (%u/%u). Discarding.\n",
635 tunnel->name, tunnel->tunnel_id,
636 session->session_id);
637 atomic_long_inc(&session->stats.rx_cookie_discards);
638 goto discard;
639 }
640 ptr += session->peer_cookie_len;
641 }
642
643 /* Handle the optional sequence numbers. Sequence numbers are
644 * in different places for L2TPv2 and L2TPv3.
645 *
646 * If we are the LAC, enable/disable sequence numbers under
647 * the control of the LNS. If no sequence numbers present but
648 * we were expecting them, discard frame.
649 */
650 ns = nr = 0;
651 L2TP_SKB_CB(skb)->has_seq = 0;
652 if (tunnel->version == L2TP_HDR_VER_2) {
653 if (hdrflags & L2TP_HDRFLAG_S) {
654 ns = ntohs(*(__be16 *) ptr);
655 ptr += 2;
656 nr = ntohs(*(__be16 *) ptr);
657 ptr += 2;
658
659 /* Store L2TP info in the skb */
660 L2TP_SKB_CB(skb)->ns = ns;
661 L2TP_SKB_CB(skb)->has_seq = 1;
662
663 l2tp_dbg(session, L2TP_MSG_SEQ,
664 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
665 session->name, ns, nr, session->nr);
666 }
667 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
668 u32 l2h = ntohl(*(__be32 *) ptr);
669
670 if (l2h & 0x40000000) {
671 ns = l2h & 0x00ffffff;
672
673 /* Store L2TP info in the skb */
674 L2TP_SKB_CB(skb)->ns = ns;
675 L2TP_SKB_CB(skb)->has_seq = 1;
676
677 l2tp_dbg(session, L2TP_MSG_SEQ,
678 "%s: recv data ns=%u, session nr=%u\n",
679 session->name, ns, session->nr);
680 }
681 }
682
683 /* Advance past L2-specific header, if present */
684 ptr += session->l2specific_len;
685
686 if (L2TP_SKB_CB(skb)->has_seq) {
687 /* Received a packet with sequence numbers. If we're the LNS,
688 * check if we sre sending sequence numbers and if not,
689 * configure it so.
690 */
691 if ((!session->lns_mode) && (!session->send_seq)) {
692 l2tp_info(session, L2TP_MSG_SEQ,
693 "%s: requested to enable seq numbers by LNS\n",
694 session->name);
695 session->send_seq = 1;
696 l2tp_session_set_header_len(session, tunnel->version);
697 }
698 } else {
699 /* No sequence numbers.
700 * If user has configured mandatory sequence numbers, discard.
701 */
702 if (session->recv_seq) {
703 l2tp_warn(session, L2TP_MSG_SEQ,
704 "%s: recv data has no seq numbers when required. Discarding.\n",
705 session->name);
706 atomic_long_inc(&session->stats.rx_seq_discards);
707 goto discard;
708 }
709
710 /* If we're the LAC and we're sending sequence numbers, the
711 * LNS has requested that we no longer send sequence numbers.
712 * If we're the LNS and we're sending sequence numbers, the
713 * LAC is broken. Discard the frame.
714 */
715 if ((!session->lns_mode) && (session->send_seq)) {
716 l2tp_info(session, L2TP_MSG_SEQ,
717 "%s: requested to disable seq numbers by LNS\n",
718 session->name);
719 session->send_seq = 0;
720 l2tp_session_set_header_len(session, tunnel->version);
721 } else if (session->send_seq) {
722 l2tp_warn(session, L2TP_MSG_SEQ,
723 "%s: recv data has no seq numbers when required. Discarding.\n",
724 session->name);
725 atomic_long_inc(&session->stats.rx_seq_discards);
726 goto discard;
727 }
728 }
729
730 /* Session data offset is handled differently for L2TPv2 and
731 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
732 * the header. For L2TPv3, the offset is negotiated using AVPs
733 * in the session setup control protocol.
734 */
735 if (tunnel->version == L2TP_HDR_VER_2) {
736 /* If offset bit set, skip it. */
737 if (hdrflags & L2TP_HDRFLAG_O) {
738 offset = ntohs(*(__be16 *)ptr);
739 ptr += 2 + offset;
740 }
741 } else
742 ptr += session->offset;
743
744 offset = ptr - optr;
745 if (!pskb_may_pull(skb, offset))
746 goto discard;
747
748 __skb_pull(skb, offset);
749
750 /* If caller wants to process the payload before we queue the
751 * packet, do so now.
752 */
753 if (payload_hook)
754 if ((*payload_hook)(skb))
755 goto discard;
756
757 /* Prepare skb for adding to the session's reorder_q. Hold
758 * packets for max reorder_timeout or 1 second if not
759 * reordering.
760 */
761 L2TP_SKB_CB(skb)->length = length;
762 L2TP_SKB_CB(skb)->expires = jiffies +
763 (session->reorder_timeout ? session->reorder_timeout : HZ);
764
765 /* Add packet to the session's receive queue. Reordering is done here, if
766 * enabled. Saved L2TP protocol info is stored in skb->sb[].
767 */
768 if (L2TP_SKB_CB(skb)->has_seq) {
769 if (l2tp_recv_data_seq(session, skb))
770 goto discard;
771 } else {
772 /* No sequence numbers. Add the skb to the tail of the
773 * reorder queue. This ensures that it will be
774 * delivered after all previous sequenced skbs.
775 */
776 skb_queue_tail(&session->reorder_q, skb);
777 }
778
779 /* Try to dequeue as many skbs from reorder_q as we can. */
780 l2tp_recv_dequeue(session);
781
782 return;
783
784 discard:
785 atomic_long_inc(&session->stats.rx_errors);
786 kfree_skb(skb);
787 }
788 EXPORT_SYMBOL(l2tp_recv_common);
789
790 /* Drop skbs from the session's reorder_q
791 */
792 int l2tp_session_queue_purge(struct l2tp_session *session)
793 {
794 struct sk_buff *skb = NULL;
795 BUG_ON(!session);
796 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
797 while ((skb = skb_dequeue(&session->reorder_q))) {
798 atomic_long_inc(&session->stats.rx_errors);
799 kfree_skb(skb);
800 }
801 return 0;
802 }
803 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
804
805 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
806 * here. The skb is not on a list when we get here.
807 * Returns 0 if the packet was a data packet and was successfully passed on.
808 * Returns 1 if the packet was not a good data packet and could not be
809 * forwarded. All such packets are passed up to userspace to deal with.
810 */
811 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
812 int (*payload_hook)(struct sk_buff *skb))
813 {
814 struct l2tp_session *session = NULL;
815 unsigned char *ptr, *optr;
816 u16 hdrflags;
817 u32 tunnel_id, session_id;
818 u16 version;
819 int length;
820
821 /* UDP has verifed checksum */
822
823 /* UDP always verifies the packet length. */
824 __skb_pull(skb, sizeof(struct udphdr));
825
826 /* Short packet? */
827 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
828 l2tp_info(tunnel, L2TP_MSG_DATA,
829 "%s: recv short packet (len=%d)\n",
830 tunnel->name, skb->len);
831 goto error;
832 }
833
834 /* Trace packet contents, if enabled */
835 if (tunnel->debug & L2TP_MSG_DATA) {
836 length = min(32u, skb->len);
837 if (!pskb_may_pull(skb, length))
838 goto error;
839
840 pr_debug("%s: recv\n", tunnel->name);
841 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
842 }
843
844 /* Point to L2TP header */
845 optr = ptr = skb->data;
846
847 /* Get L2TP header flags */
848 hdrflags = ntohs(*(__be16 *) ptr);
849
850 /* Check protocol version */
851 version = hdrflags & L2TP_HDR_VER_MASK;
852 if (version != tunnel->version) {
853 l2tp_info(tunnel, L2TP_MSG_DATA,
854 "%s: recv protocol version mismatch: got %d expected %d\n",
855 tunnel->name, version, tunnel->version);
856 goto error;
857 }
858
859 /* Get length of L2TP packet */
860 length = skb->len;
861
862 /* If type is control packet, it is handled by userspace. */
863 if (hdrflags & L2TP_HDRFLAG_T) {
864 l2tp_dbg(tunnel, L2TP_MSG_DATA,
865 "%s: recv control packet, len=%d\n",
866 tunnel->name, length);
867 goto error;
868 }
869
870 /* Skip flags */
871 ptr += 2;
872
873 if (tunnel->version == L2TP_HDR_VER_2) {
874 /* If length is present, skip it */
875 if (hdrflags & L2TP_HDRFLAG_L)
876 ptr += 2;
877
878 /* Extract tunnel and session ID */
879 tunnel_id = ntohs(*(__be16 *) ptr);
880 ptr += 2;
881 session_id = ntohs(*(__be16 *) ptr);
882 ptr += 2;
883 } else {
884 ptr += 2; /* skip reserved bits */
885 tunnel_id = tunnel->tunnel_id;
886 session_id = ntohl(*(__be32 *) ptr);
887 ptr += 4;
888 }
889
890 /* Find the session context */
891 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
892 if (!session || !session->recv_skb) {
893 if (session)
894 l2tp_session_dec_refcount(session);
895
896 /* Not found? Pass to userspace to deal with */
897 l2tp_info(tunnel, L2TP_MSG_DATA,
898 "%s: no session found (%u/%u). Passing up.\n",
899 tunnel->name, tunnel_id, session_id);
900 goto error;
901 }
902
903 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
904 l2tp_session_dec_refcount(session);
905
906 return 0;
907
908 error:
909 /* Put UDP header back */
910 __skb_push(skb, sizeof(struct udphdr));
911
912 return 1;
913 }
914
915 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
916 * Return codes:
917 * 0 : success.
918 * <0: error
919 * >0: skb should be passed up to userspace as UDP.
920 */
921 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
922 {
923 struct l2tp_tunnel *tunnel;
924
925 tunnel = l2tp_tunnel(sk);
926 if (tunnel == NULL)
927 goto pass_up;
928
929 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
930 tunnel->name, skb->len);
931
932 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
933 goto pass_up;
934
935 return 0;
936
937 pass_up:
938 return 1;
939 }
940 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
941
942 /************************************************************************
943 * Transmit handling
944 ***********************************************************************/
945
946 /* Build an L2TP header for the session into the buffer provided.
947 */
948 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
949 {
950 struct l2tp_tunnel *tunnel = session->tunnel;
951 __be16 *bufp = buf;
952 __be16 *optr = buf;
953 u16 flags = L2TP_HDR_VER_2;
954 u32 tunnel_id = tunnel->peer_tunnel_id;
955 u32 session_id = session->peer_session_id;
956
957 if (session->send_seq)
958 flags |= L2TP_HDRFLAG_S;
959
960 /* Setup L2TP header. */
961 *bufp++ = htons(flags);
962 *bufp++ = htons(tunnel_id);
963 *bufp++ = htons(session_id);
964 if (session->send_seq) {
965 *bufp++ = htons(session->ns);
966 *bufp++ = 0;
967 session->ns++;
968 session->ns &= 0xffff;
969 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
970 session->name, session->ns);
971 }
972
973 return bufp - optr;
974 }
975
976 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
977 {
978 struct l2tp_tunnel *tunnel = session->tunnel;
979 char *bufp = buf;
980 char *optr = bufp;
981
982 /* Setup L2TP header. The header differs slightly for UDP and
983 * IP encapsulations. For UDP, there is 4 bytes of flags.
984 */
985 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
986 u16 flags = L2TP_HDR_VER_3;
987 *((__be16 *) bufp) = htons(flags);
988 bufp += 2;
989 *((__be16 *) bufp) = 0;
990 bufp += 2;
991 }
992
993 *((__be32 *) bufp) = htonl(session->peer_session_id);
994 bufp += 4;
995 if (session->cookie_len) {
996 memcpy(bufp, &session->cookie[0], session->cookie_len);
997 bufp += session->cookie_len;
998 }
999 if (session->l2specific_len) {
1000 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1001 u32 l2h = 0;
1002 if (session->send_seq) {
1003 l2h = 0x40000000 | session->ns;
1004 session->ns++;
1005 session->ns &= 0xffffff;
1006 l2tp_dbg(session, L2TP_MSG_SEQ,
1007 "%s: updated ns to %u\n",
1008 session->name, session->ns);
1009 }
1010
1011 *((__be32 *) bufp) = htonl(l2h);
1012 }
1013 bufp += session->l2specific_len;
1014 }
1015 if (session->offset)
1016 bufp += session->offset;
1017
1018 return bufp - optr;
1019 }
1020
1021 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1022 struct flowi *fl, size_t data_len)
1023 {
1024 struct l2tp_tunnel *tunnel = session->tunnel;
1025 unsigned int len = skb->len;
1026 int error;
1027
1028 /* Debug */
1029 if (session->send_seq)
1030 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
1031 session->name, data_len, session->ns - 1);
1032 else
1033 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
1034 session->name, data_len);
1035
1036 if (session->debug & L2TP_MSG_DATA) {
1037 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1038 unsigned char *datap = skb->data + uhlen;
1039
1040 pr_debug("%s: xmit\n", session->name);
1041 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1042 datap, min_t(size_t, 32, len - uhlen));
1043 }
1044
1045 /* Queue the packet to IP for output */
1046 skb->ignore_df = 1;
1047 #if IS_ENABLED(CONFIG_IPV6)
1048 if (l2tp_sk_is_v6(tunnel->sock))
1049 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1050 else
1051 #endif
1052 error = ip_queue_xmit(tunnel->sock, skb, fl);
1053
1054 /* Update stats */
1055 if (error >= 0) {
1056 atomic_long_inc(&tunnel->stats.tx_packets);
1057 atomic_long_add(len, &tunnel->stats.tx_bytes);
1058 atomic_long_inc(&session->stats.tx_packets);
1059 atomic_long_add(len, &session->stats.tx_bytes);
1060 } else {
1061 atomic_long_inc(&tunnel->stats.tx_errors);
1062 atomic_long_inc(&session->stats.tx_errors);
1063 }
1064
1065 return 0;
1066 }
1067
1068 /* If caller requires the skb to have a ppp header, the header must be
1069 * inserted in the skb data before calling this function.
1070 */
1071 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1072 {
1073 int data_len = skb->len;
1074 struct l2tp_tunnel *tunnel = session->tunnel;
1075 struct sock *sk = tunnel->sock;
1076 struct flowi *fl;
1077 struct udphdr *uh;
1078 struct inet_sock *inet;
1079 int headroom;
1080 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1081 int udp_len;
1082 int ret = NET_XMIT_SUCCESS;
1083
1084 /* Check that there's enough headroom in the skb to insert IP,
1085 * UDP and L2TP headers. If not enough, expand it to
1086 * make room. Adjust truesize.
1087 */
1088 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1089 uhlen + hdr_len;
1090 if (skb_cow_head(skb, headroom)) {
1091 kfree_skb(skb);
1092 return NET_XMIT_DROP;
1093 }
1094
1095 /* Setup L2TP header */
1096 session->build_header(session, __skb_push(skb, hdr_len));
1097
1098 /* Reset skb netfilter state */
1099 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1100 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1101 IPSKB_REROUTED);
1102 nf_reset(skb);
1103
1104 bh_lock_sock(sk);
1105 if (sock_owned_by_user(sk)) {
1106 kfree_skb(skb);
1107 ret = NET_XMIT_DROP;
1108 goto out_unlock;
1109 }
1110
1111 /* The user-space may change the connection status for the user-space
1112 * provided socket at run time: we must check it under the socket lock
1113 */
1114 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1115 kfree_skb(skb);
1116 ret = NET_XMIT_DROP;
1117 goto out_unlock;
1118 }
1119
1120 /* Get routing info from the tunnel socket */
1121 skb_dst_drop(skb);
1122 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1123
1124 inet = inet_sk(sk);
1125 fl = &inet->cork.fl;
1126 switch (tunnel->encap) {
1127 case L2TP_ENCAPTYPE_UDP:
1128 /* Setup UDP header */
1129 __skb_push(skb, sizeof(*uh));
1130 skb_reset_transport_header(skb);
1131 uh = udp_hdr(skb);
1132 uh->source = inet->inet_sport;
1133 uh->dest = inet->inet_dport;
1134 udp_len = uhlen + hdr_len + data_len;
1135 uh->len = htons(udp_len);
1136
1137 /* Calculate UDP checksum if configured to do so */
1138 #if IS_ENABLED(CONFIG_IPV6)
1139 if (l2tp_sk_is_v6(sk))
1140 udp6_set_csum(udp_get_no_check6_tx(sk),
1141 skb, &inet6_sk(sk)->saddr,
1142 &sk->sk_v6_daddr, udp_len);
1143 else
1144 #endif
1145 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1146 inet->inet_daddr, udp_len);
1147 break;
1148
1149 case L2TP_ENCAPTYPE_IP:
1150 break;
1151 }
1152
1153 l2tp_xmit_core(session, skb, fl, data_len);
1154 out_unlock:
1155 bh_unlock_sock(sk);
1156
1157 return ret;
1158 }
1159 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1160
1161 /*****************************************************************************
1162 * Tinnel and session create/destroy.
1163 *****************************************************************************/
1164
1165 /* Tunnel socket destruct hook.
1166 * The tunnel context is deleted only when all session sockets have been
1167 * closed.
1168 */
1169 static void l2tp_tunnel_destruct(struct sock *sk)
1170 {
1171 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1172
1173 if (tunnel == NULL)
1174 goto end;
1175
1176 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1177
1178 /* Disable udp encapsulation */
1179 switch (tunnel->encap) {
1180 case L2TP_ENCAPTYPE_UDP:
1181 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1182 (udp_sk(sk))->encap_type = 0;
1183 (udp_sk(sk))->encap_rcv = NULL;
1184 (udp_sk(sk))->encap_destroy = NULL;
1185 break;
1186 case L2TP_ENCAPTYPE_IP:
1187 break;
1188 }
1189
1190 /* Remove hooks into tunnel socket */
1191 sk->sk_destruct = tunnel->old_sk_destruct;
1192 sk->sk_user_data = NULL;
1193
1194 /* Call the original destructor */
1195 if (sk->sk_destruct)
1196 (*sk->sk_destruct)(sk);
1197
1198 kfree_rcu(tunnel, rcu);
1199 end:
1200 return;
1201 }
1202
1203 /* When the tunnel is closed, all the attached sessions need to go too.
1204 */
1205 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1206 {
1207 int hash;
1208 struct hlist_node *walk;
1209 struct hlist_node *tmp;
1210 struct l2tp_session *session;
1211
1212 BUG_ON(tunnel == NULL);
1213
1214 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1215 tunnel->name);
1216
1217 write_lock_bh(&tunnel->hlist_lock);
1218 tunnel->acpt_newsess = false;
1219 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1220 again:
1221 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1222 session = hlist_entry(walk, struct l2tp_session, hlist);
1223
1224 l2tp_info(session, L2TP_MSG_CONTROL,
1225 "%s: closing session\n", session->name);
1226
1227 hlist_del_init(&session->hlist);
1228
1229 if (test_and_set_bit(0, &session->dead))
1230 goto again;
1231
1232 write_unlock_bh(&tunnel->hlist_lock);
1233
1234 __l2tp_session_unhash(session);
1235 l2tp_session_queue_purge(session);
1236
1237 if (session->session_close != NULL)
1238 (*session->session_close)(session);
1239
1240 l2tp_session_dec_refcount(session);
1241
1242 write_lock_bh(&tunnel->hlist_lock);
1243
1244 /* Now restart from the beginning of this hash
1245 * chain. We always remove a session from the
1246 * list so we are guaranteed to make forward
1247 * progress.
1248 */
1249 goto again;
1250 }
1251 }
1252 write_unlock_bh(&tunnel->hlist_lock);
1253 }
1254 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1255
1256 /* Tunnel socket destroy hook for UDP encapsulation */
1257 static void l2tp_udp_encap_destroy(struct sock *sk)
1258 {
1259 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1260
1261 if (tunnel)
1262 l2tp_tunnel_delete(tunnel);
1263 }
1264
1265 /* Workqueue tunnel deletion function */
1266 static void l2tp_tunnel_del_work(struct work_struct *work)
1267 {
1268 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1269 del_work);
1270 struct sock *sk = tunnel->sock;
1271 struct socket *sock = sk->sk_socket;
1272 struct l2tp_net *pn;
1273
1274 l2tp_tunnel_closeall(tunnel);
1275
1276 /* If the tunnel socket was created within the kernel, use
1277 * the sk API to release it here.
1278 */
1279 if (tunnel->fd < 0) {
1280 if (sock) {
1281 kernel_sock_shutdown(sock, SHUT_RDWR);
1282 sock_release(sock);
1283 }
1284 }
1285
1286 /* Remove the tunnel struct from the tunnel list */
1287 pn = l2tp_pernet(tunnel->l2tp_net);
1288 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1289 list_del_rcu(&tunnel->list);
1290 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1291
1292 /* drop initial ref */
1293 l2tp_tunnel_dec_refcount(tunnel);
1294
1295 /* drop workqueue ref */
1296 l2tp_tunnel_dec_refcount(tunnel);
1297 }
1298
1299 /* Create a socket for the tunnel, if one isn't set up by
1300 * userspace. This is used for static tunnels where there is no
1301 * managing L2TP daemon.
1302 *
1303 * Since we don't want these sockets to keep a namespace alive by
1304 * themselves, we drop the socket's namespace refcount after creation.
1305 * These sockets are freed when the namespace exits using the pernet
1306 * exit hook.
1307 */
1308 static int l2tp_tunnel_sock_create(struct net *net,
1309 u32 tunnel_id,
1310 u32 peer_tunnel_id,
1311 struct l2tp_tunnel_cfg *cfg,
1312 struct socket **sockp)
1313 {
1314 int err = -EINVAL;
1315 struct socket *sock = NULL;
1316 struct udp_port_cfg udp_conf;
1317
1318 switch (cfg->encap) {
1319 case L2TP_ENCAPTYPE_UDP:
1320 memset(&udp_conf, 0, sizeof(udp_conf));
1321
1322 #if IS_ENABLED(CONFIG_IPV6)
1323 if (cfg->local_ip6 && cfg->peer_ip6) {
1324 udp_conf.family = AF_INET6;
1325 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1326 sizeof(udp_conf.local_ip6));
1327 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1328 sizeof(udp_conf.peer_ip6));
1329 udp_conf.use_udp6_tx_checksums =
1330 ! cfg->udp6_zero_tx_checksums;
1331 udp_conf.use_udp6_rx_checksums =
1332 ! cfg->udp6_zero_rx_checksums;
1333 } else
1334 #endif
1335 {
1336 udp_conf.family = AF_INET;
1337 udp_conf.local_ip = cfg->local_ip;
1338 udp_conf.peer_ip = cfg->peer_ip;
1339 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1340 }
1341
1342 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1343 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1344
1345 err = udp_sock_create(net, &udp_conf, &sock);
1346 if (err < 0)
1347 goto out;
1348
1349 break;
1350
1351 case L2TP_ENCAPTYPE_IP:
1352 #if IS_ENABLED(CONFIG_IPV6)
1353 if (cfg->local_ip6 && cfg->peer_ip6) {
1354 struct sockaddr_l2tpip6 ip6_addr = {0};
1355
1356 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1357 IPPROTO_L2TP, &sock);
1358 if (err < 0)
1359 goto out;
1360
1361 ip6_addr.l2tp_family = AF_INET6;
1362 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1363 sizeof(ip6_addr.l2tp_addr));
1364 ip6_addr.l2tp_conn_id = tunnel_id;
1365 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1366 sizeof(ip6_addr));
1367 if (err < 0)
1368 goto out;
1369
1370 ip6_addr.l2tp_family = AF_INET6;
1371 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1372 sizeof(ip6_addr.l2tp_addr));
1373 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1374 err = kernel_connect(sock,
1375 (struct sockaddr *) &ip6_addr,
1376 sizeof(ip6_addr), 0);
1377 if (err < 0)
1378 goto out;
1379 } else
1380 #endif
1381 {
1382 struct sockaddr_l2tpip ip_addr = {0};
1383
1384 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1385 IPPROTO_L2TP, &sock);
1386 if (err < 0)
1387 goto out;
1388
1389 ip_addr.l2tp_family = AF_INET;
1390 ip_addr.l2tp_addr = cfg->local_ip;
1391 ip_addr.l2tp_conn_id = tunnel_id;
1392 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1393 sizeof(ip_addr));
1394 if (err < 0)
1395 goto out;
1396
1397 ip_addr.l2tp_family = AF_INET;
1398 ip_addr.l2tp_addr = cfg->peer_ip;
1399 ip_addr.l2tp_conn_id = peer_tunnel_id;
1400 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1401 sizeof(ip_addr), 0);
1402 if (err < 0)
1403 goto out;
1404 }
1405 break;
1406
1407 default:
1408 goto out;
1409 }
1410
1411 out:
1412 *sockp = sock;
1413 if ((err < 0) && sock) {
1414 kernel_sock_shutdown(sock, SHUT_RDWR);
1415 sock_release(sock);
1416 *sockp = NULL;
1417 }
1418
1419 return err;
1420 }
1421
1422 static struct lock_class_key l2tp_socket_class;
1423
1424 int 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)
1425 {
1426 struct l2tp_tunnel *tunnel = NULL;
1427 int err;
1428 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1429
1430 if (cfg != NULL)
1431 encap = cfg->encap;
1432
1433 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1434 if (tunnel == NULL) {
1435 err = -ENOMEM;
1436 goto err;
1437 }
1438
1439 tunnel->version = version;
1440 tunnel->tunnel_id = tunnel_id;
1441 tunnel->peer_tunnel_id = peer_tunnel_id;
1442 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1443
1444 tunnel->magic = L2TP_TUNNEL_MAGIC;
1445 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1446 rwlock_init(&tunnel->hlist_lock);
1447 tunnel->acpt_newsess = true;
1448
1449 if (cfg != NULL)
1450 tunnel->debug = cfg->debug;
1451
1452 tunnel->encap = encap;
1453
1454 refcount_set(&tunnel->ref_count, 1);
1455 tunnel->fd = fd;
1456
1457 /* Init delete workqueue struct */
1458 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1459
1460 INIT_LIST_HEAD(&tunnel->list);
1461
1462 err = 0;
1463 err:
1464 if (tunnelp)
1465 *tunnelp = tunnel;
1466
1467 return err;
1468 }
1469 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1470
1471 static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1472 enum l2tp_encap_type encap)
1473 {
1474 if (!net_eq(sock_net(sk), net))
1475 return -EINVAL;
1476
1477 if (sk->sk_type != SOCK_DGRAM)
1478 return -EPROTONOSUPPORT;
1479
1480 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1481 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1482 return -EPROTONOSUPPORT;
1483
1484 if (sk->sk_user_data)
1485 return -EBUSY;
1486
1487 return 0;
1488 }
1489
1490 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1491 struct l2tp_tunnel_cfg *cfg)
1492 {
1493 struct l2tp_tunnel *tunnel_walk;
1494 struct l2tp_net *pn;
1495 struct socket *sock;
1496 struct sock *sk;
1497 int ret;
1498
1499 if (tunnel->fd < 0) {
1500 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1501 tunnel->peer_tunnel_id, cfg,
1502 &sock);
1503 if (ret < 0)
1504 goto err;
1505 } else {
1506 sock = sockfd_lookup(tunnel->fd, &ret);
1507 if (!sock)
1508 goto err;
1509
1510 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1511 if (ret < 0)
1512 goto err_sock;
1513 }
1514
1515 sk = sock->sk;
1516
1517 sock_hold(sk);
1518 tunnel->sock = sk;
1519 tunnel->l2tp_net = net;
1520
1521 pn = l2tp_pernet(net);
1522
1523 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1524 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1525 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1526 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1527
1528 ret = -EEXIST;
1529 goto err_sock;
1530 }
1531 }
1532 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1533 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1534
1535 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1536 struct udp_tunnel_sock_cfg udp_cfg = {
1537 .sk_user_data = tunnel,
1538 .encap_type = UDP_ENCAP_L2TPINUDP,
1539 .encap_rcv = l2tp_udp_encap_recv,
1540 .encap_destroy = l2tp_udp_encap_destroy,
1541 };
1542
1543 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1544 } else {
1545 sk->sk_user_data = tunnel;
1546 }
1547
1548 tunnel->old_sk_destruct = sk->sk_destruct;
1549 sk->sk_destruct = &l2tp_tunnel_destruct;
1550 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1551 "l2tp_sock");
1552 sk->sk_allocation = GFP_ATOMIC;
1553
1554 if (tunnel->fd >= 0)
1555 sockfd_put(sock);
1556
1557 return 0;
1558
1559 err_sock:
1560 if (tunnel->fd < 0)
1561 sock_release(sock);
1562 else
1563 sockfd_put(sock);
1564 err:
1565 return ret;
1566 }
1567 EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1568
1569 /* This function is used by the netlink TUNNEL_DELETE command.
1570 */
1571 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1572 {
1573 if (!test_and_set_bit(0, &tunnel->dead)) {
1574 l2tp_tunnel_inc_refcount(tunnel);
1575 queue_work(l2tp_wq, &tunnel->del_work);
1576 }
1577 }
1578 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1579
1580 /* Really kill the session.
1581 */
1582 void l2tp_session_free(struct l2tp_session *session)
1583 {
1584 struct l2tp_tunnel *tunnel = session->tunnel;
1585
1586 BUG_ON(refcount_read(&session->ref_count) != 0);
1587
1588 if (tunnel) {
1589 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1590 l2tp_tunnel_dec_refcount(tunnel);
1591 }
1592
1593 kfree(session);
1594 }
1595 EXPORT_SYMBOL_GPL(l2tp_session_free);
1596
1597 /* Remove an l2tp session from l2tp_core's hash lists.
1598 * Provides a tidyup interface for pseudowire code which can't just route all
1599 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1600 * callback.
1601 */
1602 void __l2tp_session_unhash(struct l2tp_session *session)
1603 {
1604 struct l2tp_tunnel *tunnel = session->tunnel;
1605
1606 /* Remove the session from core hashes */
1607 if (tunnel) {
1608 /* Remove from the per-tunnel hash */
1609 write_lock_bh(&tunnel->hlist_lock);
1610 hlist_del_init(&session->hlist);
1611 write_unlock_bh(&tunnel->hlist_lock);
1612
1613 /* For L2TPv3 we have a per-net hash: remove from there, too */
1614 if (tunnel->version != L2TP_HDR_VER_2) {
1615 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1616 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1617 hlist_del_init_rcu(&session->global_hlist);
1618 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1619 synchronize_rcu();
1620 }
1621 }
1622 }
1623 EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1624
1625 /* This function is used by the netlink SESSION_DELETE command and by
1626 pseudowire modules.
1627 */
1628 int l2tp_session_delete(struct l2tp_session *session)
1629 {
1630 if (test_and_set_bit(0, &session->dead))
1631 return 0;
1632
1633 __l2tp_session_unhash(session);
1634 l2tp_session_queue_purge(session);
1635 if (session->session_close != NULL)
1636 (*session->session_close)(session);
1637
1638 l2tp_session_dec_refcount(session);
1639
1640 return 0;
1641 }
1642 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1643
1644 /* We come here whenever a session's send_seq, cookie_len or
1645 * l2specific_len parameters are set.
1646 */
1647 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1648 {
1649 if (version == L2TP_HDR_VER_2) {
1650 session->hdr_len = 6;
1651 if (session->send_seq)
1652 session->hdr_len += 4;
1653 } else {
1654 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1655 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1656 session->hdr_len += 4;
1657 }
1658
1659 }
1660 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1661
1662 struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1663 {
1664 struct l2tp_session *session;
1665
1666 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1667 if (session != NULL) {
1668 session->magic = L2TP_SESSION_MAGIC;
1669 session->tunnel = tunnel;
1670
1671 session->session_id = session_id;
1672 session->peer_session_id = peer_session_id;
1673 session->nr = 0;
1674 if (tunnel->version == L2TP_HDR_VER_2)
1675 session->nr_max = 0xffff;
1676 else
1677 session->nr_max = 0xffffff;
1678 session->nr_window_size = session->nr_max / 2;
1679 session->nr_oos_count_max = 4;
1680
1681 /* Use NR of first received packet */
1682 session->reorder_skip = 1;
1683
1684 sprintf(&session->name[0], "sess %u/%u",
1685 tunnel->tunnel_id, session->session_id);
1686
1687 skb_queue_head_init(&session->reorder_q);
1688
1689 INIT_HLIST_NODE(&session->hlist);
1690 INIT_HLIST_NODE(&session->global_hlist);
1691
1692 /* Inherit debug options from tunnel */
1693 session->debug = tunnel->debug;
1694
1695 if (cfg) {
1696 session->pwtype = cfg->pw_type;
1697 session->debug = cfg->debug;
1698 session->mtu = cfg->mtu;
1699 session->mru = cfg->mru;
1700 session->send_seq = cfg->send_seq;
1701 session->recv_seq = cfg->recv_seq;
1702 session->lns_mode = cfg->lns_mode;
1703 session->reorder_timeout = cfg->reorder_timeout;
1704 session->offset = cfg->offset;
1705 session->l2specific_type = cfg->l2specific_type;
1706 session->l2specific_len = cfg->l2specific_len;
1707 session->cookie_len = cfg->cookie_len;
1708 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1709 session->peer_cookie_len = cfg->peer_cookie_len;
1710 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1711 }
1712
1713 if (tunnel->version == L2TP_HDR_VER_2)
1714 session->build_header = l2tp_build_l2tpv2_header;
1715 else
1716 session->build_header = l2tp_build_l2tpv3_header;
1717
1718 l2tp_session_set_header_len(session, tunnel->version);
1719
1720 refcount_set(&session->ref_count, 1);
1721
1722 return session;
1723 }
1724
1725 return ERR_PTR(-ENOMEM);
1726 }
1727 EXPORT_SYMBOL_GPL(l2tp_session_create);
1728
1729 /*****************************************************************************
1730 * Init and cleanup
1731 *****************************************************************************/
1732
1733 static __net_init int l2tp_init_net(struct net *net)
1734 {
1735 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1736 int hash;
1737
1738 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1739 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1740
1741 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1742 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1743
1744 spin_lock_init(&pn->l2tp_session_hlist_lock);
1745
1746 return 0;
1747 }
1748
1749 static __net_exit void l2tp_exit_net(struct net *net)
1750 {
1751 struct l2tp_net *pn = l2tp_pernet(net);
1752 struct l2tp_tunnel *tunnel = NULL;
1753 int hash;
1754
1755 rcu_read_lock_bh();
1756 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1757 l2tp_tunnel_delete(tunnel);
1758 }
1759 rcu_read_unlock_bh();
1760
1761 flush_workqueue(l2tp_wq);
1762 rcu_barrier();
1763
1764 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1765 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
1766 }
1767
1768 static struct pernet_operations l2tp_net_ops = {
1769 .init = l2tp_init_net,
1770 .exit = l2tp_exit_net,
1771 .id = &l2tp_net_id,
1772 .size = sizeof(struct l2tp_net),
1773 };
1774
1775 static int __init l2tp_init(void)
1776 {
1777 int rc = 0;
1778
1779 rc = register_pernet_device(&l2tp_net_ops);
1780 if (rc)
1781 goto out;
1782
1783 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1784 if (!l2tp_wq) {
1785 pr_err("alloc_workqueue failed\n");
1786 unregister_pernet_device(&l2tp_net_ops);
1787 rc = -ENOMEM;
1788 goto out;
1789 }
1790
1791 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1792
1793 out:
1794 return rc;
1795 }
1796
1797 static void __exit l2tp_exit(void)
1798 {
1799 unregister_pernet_device(&l2tp_net_ops);
1800 if (l2tp_wq) {
1801 destroy_workqueue(l2tp_wq);
1802 l2tp_wq = NULL;
1803 }
1804 }
1805
1806 module_init(l2tp_init);
1807 module_exit(l2tp_exit);
1808
1809 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1810 MODULE_DESCRIPTION("L2TP core");
1811 MODULE_LICENSE("GPL");
1812 MODULE_VERSION(L2TP_DRV_VERSION);
1813