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