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