hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/net/l2tp/l2tp_core.c
....@@ -1,5 +1,5 @@
1
-/*
2
- * L2TP core.
1
+// SPDX-License-Identifier: GPL-2.0-only
2
+/* L2TP core.
33 *
44 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
55 *
....@@ -12,10 +12,6 @@
1212 * Michal Ostrowski <mostrows@speakeasy.net>
1313 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
1414 * 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.
1915 */
2016
2117 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -65,6 +61,10 @@
6561 #include <linux/atomic.h>
6662
6763 #include "l2tp_core.h"
64
+#include "trace.h"
65
+
66
+#define CREATE_TRACE_POINTS
67
+#include "trace.h"
6868
6969 #define L2TP_DRV_VERSION "V2.0"
7070
....@@ -97,16 +97,18 @@
9797 unsigned long expires;
9898 };
9999
100
-#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
100
+#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *)&(skb)->cb[sizeof(struct inet_skb_parm)])
101101
102102 static struct workqueue_struct *l2tp_wq;
103103
104104 /* per-net private data for this module */
105105 static unsigned int l2tp_net_id;
106106 struct l2tp_net {
107
- struct list_head l2tp_tunnel_list;
108
- spinlock_t l2tp_tunnel_list_lock;
107
+ /* Lock for write access to l2tp_tunnel_idr */
108
+ spinlock_t l2tp_tunnel_idr_lock;
109
+ struct idr l2tp_tunnel_idr;
109110 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
111
+ /* Lock for write access to l2tp_session_hlist */
110112 spinlock_t l2tp_session_hlist_lock;
111113 };
112114
....@@ -118,15 +120,8 @@
118120 }
119121 #endif
120122
121
-static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
122
-{
123
- return sk->sk_user_data;
124
-}
125
-
126123 static inline struct l2tp_net *l2tp_pernet(const struct net *net)
127124 {
128
- BUG_ON(!net);
129
-
130125 return net_generic(net, l2tp_net_id);
131126 }
132127
....@@ -139,7 +134,6 @@
139134 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
140135 {
141136 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
142
-
143137 }
144138
145139 /* Session hash list.
....@@ -154,12 +148,58 @@
154148 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
155149 }
156150
157
-void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
151
+static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
158152 {
153
+ trace_free_tunnel(tunnel);
159154 sock_put(tunnel->sock);
160155 /* the tunnel is freed in the socket destructor */
161156 }
162
-EXPORT_SYMBOL(l2tp_tunnel_free);
157
+
158
+static void l2tp_session_free(struct l2tp_session *session)
159
+{
160
+ trace_free_session(session);
161
+ if (session->tunnel)
162
+ l2tp_tunnel_dec_refcount(session->tunnel);
163
+ kfree(session);
164
+}
165
+
166
+struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk)
167
+{
168
+ struct l2tp_tunnel *tunnel = sk->sk_user_data;
169
+
170
+ if (tunnel)
171
+ if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
172
+ return NULL;
173
+
174
+ return tunnel;
175
+}
176
+EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
177
+
178
+void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
179
+{
180
+ refcount_inc(&tunnel->ref_count);
181
+}
182
+EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount);
183
+
184
+void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
185
+{
186
+ if (refcount_dec_and_test(&tunnel->ref_count))
187
+ l2tp_tunnel_free(tunnel);
188
+}
189
+EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount);
190
+
191
+void l2tp_session_inc_refcount(struct l2tp_session *session)
192
+{
193
+ refcount_inc(&session->ref_count);
194
+}
195
+EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount);
196
+
197
+void l2tp_session_dec_refcount(struct l2tp_session *session)
198
+{
199
+ if (refcount_dec_and_test(&session->ref_count))
200
+ l2tp_session_free(session);
201
+}
202
+EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount);
163203
164204 /* Lookup a tunnel. A new reference is held on the returned tunnel. */
165205 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
....@@ -168,13 +208,10 @@
168208 struct l2tp_tunnel *tunnel;
169209
170210 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
- }
211
+ tunnel = idr_find(&pn->l2tp_tunnel_idr, tunnel_id);
212
+ if (tunnel && refcount_inc_not_zero(&tunnel->ref_count)) {
213
+ rcu_read_unlock_bh();
214
+ return tunnel;
178215 }
179216 rcu_read_unlock_bh();
180217
....@@ -184,13 +221,14 @@
184221
185222 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
186223 {
187
- const struct l2tp_net *pn = l2tp_pernet(net);
224
+ struct l2tp_net *pn = l2tp_pernet(net);
225
+ unsigned long tunnel_id, tmp;
188226 struct l2tp_tunnel *tunnel;
189227 int count = 0;
190228
191229 rcu_read_lock_bh();
192
- list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
193
- if (++count > nth &&
230
+ idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
231
+ if (tunnel && ++count > nth &&
194232 refcount_inc_not_zero(&tunnel->ref_count)) {
195233 rcu_read_unlock_bh();
196234 return tunnel;
....@@ -347,6 +385,8 @@
347385 hlist_add_head(&session->hlist, head);
348386 write_unlock_bh(&tunnel->hlist_lock);
349387
388
+ trace_register_session(session);
389
+
350390 return 0;
351391
352392 err_tlock_pnlock:
....@@ -375,10 +415,6 @@
375415 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
376416 if (L2TP_SKB_CB(skbp)->ns > ns) {
377417 __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));
382418 atomic_long_inc(&session->stats.rx_oos_packets);
383419 goto out;
384420 }
....@@ -411,13 +447,11 @@
411447 /* Bump our Nr */
412448 session->nr++;
413449 session->nr &= session->nr_max;
414
-
415
- l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
416
- session->name, session->nr);
450
+ trace_session_seqnum_update(session);
417451 }
418452
419453 /* call private receive handler */
420
- if (session->recv_skb != NULL)
454
+ if (session->recv_skb)
421455 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
422456 else
423457 kfree_skb(skb);
....@@ -438,37 +472,27 @@
438472 start:
439473 spin_lock_bh(&session->reorder_q.lock);
440474 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
441
- if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
475
+ struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
476
+
477
+ /* If the packet has been pending on the queue for too long, discard it */
478
+ if (time_after(jiffies, cb->expires)) {
442479 atomic_long_inc(&session->stats.rx_seq_discards);
443480 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));
481
+ trace_session_pkt_expired(session, cb->ns);
449482 session->reorder_skip = 1;
450483 __skb_unlink(skb, &session->reorder_q);
451484 kfree_skb(skb);
452485 continue;
453486 }
454487
455
- if (L2TP_SKB_CB(skb)->has_seq) {
488
+ if (cb->has_seq) {
456489 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);
461490 session->reorder_skip = 0;
462
- session->nr = L2TP_SKB_CB(skb)->ns;
491
+ session->nr = cb->ns;
492
+ trace_session_seqnum_reset(session);
463493 }
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));
494
+ if (cb->ns != session->nr)
470495 goto out;
471
- }
472496 }
473497 __skb_unlink(skb, &session->reorder_q);
474498
....@@ -501,14 +525,13 @@
501525 */
502526 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
503527 {
504
- if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
528
+ struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
529
+
530
+ if (!l2tp_seq_check_rx_window(session, cb->ns)) {
505531 /* Packet sequence number is outside allowed window.
506532 * Discard it.
507533 */
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);
534
+ trace_session_pkt_outside_rx_window(session, cb->ns);
512535 goto discard;
513536 }
514537
....@@ -525,10 +548,10 @@
525548 * is seen. After nr_oos_count_max in-sequence packets, reset the
526549 * sequence number to re-enable packet reception.
527550 */
528
- if (L2TP_SKB_CB(skb)->ns == session->nr) {
551
+ if (cb->ns == session->nr) {
529552 skb_queue_tail(&session->reorder_q, skb);
530553 } else {
531
- u32 nr_oos = L2TP_SKB_CB(skb)->ns;
554
+ u32 nr_oos = cb->ns;
532555 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
533556
534557 if (nr_oos == nr_next)
....@@ -539,17 +562,10 @@
539562 session->nr_oos = nr_oos;
540563 if (session->nr_oos_count > session->nr_oos_count_max) {
541564 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);
545565 }
546566 if (!session->reorder_skip) {
547567 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));
568
+ trace_session_pkt_oos(session, cb->ns);
553569 goto discard;
554570 }
555571 skb_queue_tail(&session->reorder_q, skb);
....@@ -627,15 +643,13 @@
627643 {
628644 struct l2tp_tunnel *tunnel = session->tunnel;
629645 int offset;
630
- u32 ns, nr;
631646
632647 /* Parse and check optional cookie */
633648 if (session->peer_cookie_len > 0) {
634649 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);
650
+ pr_debug_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n",
651
+ tunnel->name, tunnel->tunnel_id,
652
+ session->session_id);
639653 atomic_long_inc(&session->stats.rx_cookie_discards);
640654 goto discard;
641655 }
....@@ -649,49 +663,35 @@
649663 * the control of the LNS. If no sequence numbers present but
650664 * we were expecting them, discard frame.
651665 */
652
- ns = nr = 0;
653666 L2TP_SKB_CB(skb)->has_seq = 0;
654667 if (tunnel->version == L2TP_HDR_VER_2) {
655668 if (hdrflags & L2TP_HDRFLAG_S) {
656
- ns = ntohs(*(__be16 *) ptr);
657
- ptr += 2;
658
- nr = ntohs(*(__be16 *) ptr);
659
- ptr += 2;
660
-
661669 /* Store L2TP info in the skb */
662
- L2TP_SKB_CB(skb)->ns = ns;
670
+ L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr);
663671 L2TP_SKB_CB(skb)->has_seq = 1;
672
+ ptr += 2;
673
+ /* Skip past nr in the header */
674
+ ptr += 2;
664675
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);
668676 }
669677 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
670
- u32 l2h = ntohl(*(__be32 *) ptr);
678
+ u32 l2h = ntohl(*(__be32 *)ptr);
671679
672680 if (l2h & 0x40000000) {
673
- ns = l2h & 0x00ffffff;
674
-
675681 /* Store L2TP info in the skb */
676
- L2TP_SKB_CB(skb)->ns = ns;
682
+ L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff;
677683 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);
682684 }
683685 ptr += 4;
684686 }
685687
686688 if (L2TP_SKB_CB(skb)->has_seq) {
687
- /* Received a packet with sequence numbers. If we're the LNS,
689
+ /* Received a packet with sequence numbers. If we're the LAC,
688690 * check if we sre sending sequence numbers and if not,
689691 * configure it so.
690692 */
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);
693
+ if (!session->lns_mode && !session->send_seq) {
694
+ trace_session_seqnum_lns_enable(session);
695695 session->send_seq = 1;
696696 l2tp_session_set_header_len(session, tunnel->version);
697697 }
....@@ -700,9 +700,8 @@
700700 * If user has configured mandatory sequence numbers, discard.
701701 */
702702 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);
703
+ pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
704
+ session->name);
706705 atomic_long_inc(&session->stats.rx_seq_discards);
707706 goto discard;
708707 }
....@@ -712,16 +711,13 @@
712711 * If we're the LNS and we're sending sequence numbers, the
713712 * LAC is broken. Discard the frame.
714713 */
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);
714
+ if (!session->lns_mode && session->send_seq) {
715
+ trace_session_seqnum_lns_disable(session);
719716 session->send_seq = 0;
720717 l2tp_session_set_header_len(session, tunnel->version);
721718 } 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);
719
+ pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
720
+ session->name);
725721 atomic_long_inc(&session->stats.rx_seq_discards);
726722 goto discard;
727723 }
....@@ -775,20 +771,18 @@
775771 atomic_long_inc(&session->stats.rx_errors);
776772 kfree_skb(skb);
777773 }
778
-EXPORT_SYMBOL(l2tp_recv_common);
774
+EXPORT_SYMBOL_GPL(l2tp_recv_common);
779775
780776 /* Drop skbs from the session's reorder_q
781777 */
782
-static int l2tp_session_queue_purge(struct l2tp_session *session)
778
+static void l2tp_session_queue_purge(struct l2tp_session *session)
783779 {
784780 struct sk_buff *skb = NULL;
785
- BUG_ON(!session);
786
- BUG_ON(session->magic != L2TP_SESSION_MAGIC);
781
+
787782 while ((skb = skb_dequeue(&session->reorder_q))) {
788783 atomic_long_inc(&session->stats.rx_errors);
789784 kfree_skb(skb);
790785 }
791
- return 0;
792786 }
793787
794788 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
....@@ -813,47 +807,32 @@
813807
814808 /* Short packet? */
815809 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);
810
+ pr_debug_ratelimited("%s: recv short packet (len=%d)\n",
811
+ tunnel->name, skb->len);
812
+ goto invalid;
830813 }
831814
832815 /* Point to L2TP header */
833
- optr = ptr = skb->data;
816
+ optr = skb->data;
817
+ ptr = skb->data;
834818
835819 /* Get L2TP header flags */
836
- hdrflags = ntohs(*(__be16 *) ptr);
820
+ hdrflags = ntohs(*(__be16 *)ptr);
837821
838822 /* Check protocol version */
839823 version = hdrflags & L2TP_HDR_VER_MASK;
840824 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;
825
+ pr_debug_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n",
826
+ tunnel->name, version, tunnel->version);
827
+ goto invalid;
845828 }
846829
847830 /* Get length of L2TP packet */
848831 length = skb->len;
849832
850833 /* 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
- }
834
+ if (hdrflags & L2TP_HDRFLAG_T)
835
+ goto pass;
857836
858837 /* Skip flags */
859838 ptr += 2;
....@@ -864,14 +843,14 @@
864843 ptr += 2;
865844
866845 /* Extract tunnel and session ID */
867
- tunnel_id = ntohs(*(__be16 *) ptr);
846
+ tunnel_id = ntohs(*(__be16 *)ptr);
868847 ptr += 2;
869
- session_id = ntohs(*(__be16 *) ptr);
848
+ session_id = ntohs(*(__be16 *)ptr);
870849 ptr += 2;
871850 } else {
872851 ptr += 2; /* skip reserved bits */
873852 tunnel_id = tunnel->tunnel_id;
874
- session_id = ntohl(*(__be32 *) ptr);
853
+ session_id = ntohl(*(__be32 *)ptr);
875854 ptr += 4;
876855 }
877856
....@@ -882,16 +861,15 @@
882861 l2tp_session_dec_refcount(session);
883862
884863 /* 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;
864
+ pr_debug_ratelimited("%s: no session found (%u/%u). Passing up.\n",
865
+ tunnel->name, tunnel_id, session_id);
866
+ goto pass;
889867 }
890868
891869 if (tunnel->version == L2TP_HDR_VER_3 &&
892870 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
893871 l2tp_session_dec_refcount(session);
894
- goto error;
872
+ goto invalid;
895873 }
896874
897875 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
....@@ -899,7 +877,10 @@
899877
900878 return 0;
901879
902
-error:
880
+invalid:
881
+ atomic_long_inc(&tunnel->stats.rx_invalid);
882
+
883
+pass:
903884 /* Put UDP header back */
904885 __skb_push(skb, sizeof(struct udphdr));
905886
....@@ -916,12 +897,17 @@
916897 {
917898 struct l2tp_tunnel *tunnel;
918899
900
+ /* Note that this is called from the encap_rcv hook inside an
901
+ * RCU-protected region, but without the socket being locked.
902
+ * Hence we use rcu_dereference_sk_user_data to access the
903
+ * tunnel data structure rather the usual l2tp_sk_to_tunnel
904
+ * accessor function.
905
+ */
919906 tunnel = rcu_dereference_sk_user_data(sk);
920
- if (tunnel == NULL)
907
+ if (!tunnel)
921908 goto pass_up;
922
-
923
- l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
924
- tunnel->name, skb->len);
909
+ if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
910
+ goto pass_up;
925911
926912 if (l2tp_udp_recv_core(tunnel, skb))
927913 goto pass_up;
....@@ -960,8 +946,7 @@
960946 *bufp++ = 0;
961947 session->ns++;
962948 session->ns &= 0xffff;
963
- l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
964
- session->name, session->ns);
949
+ trace_session_seqnum_update(session);
965950 }
966951
967952 return bufp - optr;
....@@ -978,13 +963,13 @@
978963 */
979964 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
980965 u16 flags = L2TP_HDR_VER_3;
981
- *((__be16 *) bufp) = htons(flags);
966
+ *((__be16 *)bufp) = htons(flags);
982967 bufp += 2;
983
- *((__be16 *) bufp) = 0;
968
+ *((__be16 *)bufp) = 0;
984969 bufp += 2;
985970 }
986971
987
- *((__be32 *) bufp) = htonl(session->peer_session_id);
972
+ *((__be32 *)bufp) = htonl(session->peer_session_id);
988973 bufp += 4;
989974 if (session->cookie_len) {
990975 memcpy(bufp, &session->cookie[0], session->cookie_len);
....@@ -997,9 +982,7 @@
997982 l2h = 0x40000000 | session->ns;
998983 session->ns++;
999984 session->ns &= 0xffffff;
1000
- l2tp_dbg(session, L2TP_MSG_SEQ,
1001
- "%s: updated ns to %u\n",
1002
- session->name, session->ns);
985
+ trace_session_seqnum_update(session);
1003986 }
1004987
1005988 *((__be32 *)bufp) = htonl(l2h);
....@@ -1009,89 +992,56 @@
1009992 return bufp - optr;
1010993 }
1011994
1012
-static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1013
- struct flowi *fl, size_t data_len)
995
+/* Queue the packet to IP for output: tunnel socket lock must be held */
996
+static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
1014997 {
1015
- struct l2tp_tunnel *tunnel = session->tunnel;
1016
- unsigned int len = skb->len;
1017
- int error;
998
+ int err;
1018999
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 */
10371000 skb->ignore_df = 1;
10381001 skb_dst_drop(skb);
10391002 #if IS_ENABLED(CONFIG_IPV6)
10401003 if (l2tp_sk_is_v6(tunnel->sock))
1041
- error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1004
+ err = inet6_csk_xmit(tunnel->sock, skb, NULL);
10421005 else
10431006 #endif
1044
- error = ip_queue_xmit(tunnel->sock, skb, fl);
1007
+ err = ip_queue_xmit(tunnel->sock, skb, fl);
10451008
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
- }
1009
+ return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
10561010 }
10571011
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)
1012
+static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
10621013 {
1063
- int data_len = skb->len;
10641014 struct l2tp_tunnel *tunnel = session->tunnel;
1015
+ unsigned int data_len = skb->len;
10651016 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;
1017
+ int headroom, uhlen, udp_len;
10721018 int ret = NET_XMIT_SUCCESS;
1019
+ struct inet_sock *inet;
1020
+ struct udphdr *uh;
10731021
10741022 /* Check that there's enough headroom in the skb to insert IP,
10751023 * UDP and L2TP headers. If not enough, expand it to
10761024 * make room. Adjust truesize.
10771025 */
1078
- headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1079
- uhlen + hdr_len;
1026
+ uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
1027
+ headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
10801028 if (skb_cow_head(skb, headroom)) {
10811029 kfree_skb(skb);
10821030 return NET_XMIT_DROP;
10831031 }
10841032
10851033 /* Setup L2TP header */
1086
- session->build_header(session, __skb_push(skb, hdr_len));
1034
+ if (tunnel->version == L2TP_HDR_VER_2)
1035
+ l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
1036
+ else
1037
+ l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
10871038
10881039 /* Reset skb netfilter state */
10891040 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);
1041
+ IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
1042
+ nf_reset_ct(skb);
10931043
1094
- bh_lock_sock(sk);
1044
+ bh_lock_sock_nested(sk);
10951045 if (sock_owned_by_user(sk)) {
10961046 kfree_skb(skb);
10971047 ret = NET_XMIT_DROP;
....@@ -1107,8 +1057,12 @@
11071057 goto out_unlock;
11081058 }
11091059
1060
+ /* Report transmitted length before we add encap header, which keeps
1061
+ * statistics consistent for both UDP and IP encap tx/rx paths.
1062
+ */
1063
+ *len = skb->len;
1064
+
11101065 inet = inet_sk(sk);
1111
- fl = &inet->cork.fl;
11121066 switch (tunnel->encap) {
11131067 case L2TP_ENCAPTYPE_UDP:
11141068 /* Setup UDP header */
....@@ -1117,7 +1071,7 @@
11171071 uh = udp_hdr(skb);
11181072 uh->source = inet->inet_sport;
11191073 uh->dest = inet->inet_dport;
1120
- udp_len = uhlen + hdr_len + data_len;
1074
+ udp_len = uhlen + session->hdr_len + data_len;
11211075 uh->len = htons(udp_len);
11221076
11231077 /* Calculate UDP checksum if configured to do so */
....@@ -1128,18 +1082,40 @@
11281082 &sk->sk_v6_daddr, udp_len);
11291083 else
11301084 #endif
1131
- udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1132
- inet->inet_daddr, udp_len);
1085
+ udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1086
+ inet->inet_daddr, udp_len);
11331087 break;
11341088
11351089 case L2TP_ENCAPTYPE_IP:
11361090 break;
11371091 }
11381092
1139
- l2tp_xmit_core(session, skb, fl, data_len);
1093
+ ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
1094
+
11401095 out_unlock:
11411096 bh_unlock_sock(sk);
11421097
1098
+ return ret;
1099
+}
1100
+
1101
+/* If caller requires the skb to have a ppp header, the header must be
1102
+ * inserted in the skb data before calling this function.
1103
+ */
1104
+int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
1105
+{
1106
+ unsigned int len = 0;
1107
+ int ret;
1108
+
1109
+ ret = l2tp_xmit_core(session, skb, &len);
1110
+ if (ret == NET_XMIT_SUCCESS) {
1111
+ atomic_long_inc(&session->tunnel->stats.tx_packets);
1112
+ atomic_long_add(len, &session->tunnel->stats.tx_bytes);
1113
+ atomic_long_inc(&session->stats.tx_packets);
1114
+ atomic_long_add(len, &session->stats.tx_bytes);
1115
+ } else {
1116
+ atomic_long_inc(&session->tunnel->stats.tx_errors);
1117
+ atomic_long_inc(&session->stats.tx_errors);
1118
+ }
11431119 return ret;
11441120 }
11451121 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
....@@ -1154,12 +1130,10 @@
11541130 */
11551131 static void l2tp_tunnel_destruct(struct sock *sk)
11561132 {
1157
- struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1133
+ struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
11581134
1159
- if (tunnel == NULL)
1135
+ if (!tunnel)
11601136 goto end;
1161
-
1162
- l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
11631137
11641138 /* Disable udp encapsulation */
11651139 switch (tunnel->encap) {
....@@ -1174,8 +1148,10 @@
11741148 }
11751149
11761150 /* Remove hooks into tunnel socket */
1151
+ write_lock_bh(&sk->sk_callback_lock);
11771152 sk->sk_destruct = tunnel->old_sk_destruct;
11781153 sk->sk_user_data = NULL;
1154
+ write_unlock_bh(&sk->sk_callback_lock);
11791155
11801156 /* Call the original destructor */
11811157 if (sk->sk_destruct)
....@@ -1184,6 +1160,30 @@
11841160 kfree_rcu(tunnel, rcu);
11851161 end:
11861162 return;
1163
+}
1164
+
1165
+/* Remove an l2tp session from l2tp_core's hash lists. */
1166
+static void l2tp_session_unhash(struct l2tp_session *session)
1167
+{
1168
+ struct l2tp_tunnel *tunnel = session->tunnel;
1169
+
1170
+ /* Remove the session from core hashes */
1171
+ if (tunnel) {
1172
+ /* Remove from the per-tunnel hash */
1173
+ write_lock_bh(&tunnel->hlist_lock);
1174
+ hlist_del_init(&session->hlist);
1175
+ write_unlock_bh(&tunnel->hlist_lock);
1176
+
1177
+ /* For L2TPv3 we have a per-net hash: remove from there, too */
1178
+ if (tunnel->version != L2TP_HDR_VER_2) {
1179
+ struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1180
+
1181
+ spin_lock_bh(&pn->l2tp_session_hlist_lock);
1182
+ hlist_del_init_rcu(&session->global_hlist);
1183
+ spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1184
+ synchronize_rcu();
1185
+ }
1186
+ }
11871187 }
11881188
11891189 /* When the tunnel is closed, all the attached sessions need to go too.
....@@ -1195,36 +1195,16 @@
11951195 struct hlist_node *tmp;
11961196 struct l2tp_session *session;
11971197
1198
- BUG_ON(tunnel == NULL);
1199
-
1200
- l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1201
- tunnel->name);
1202
-
12031198 write_lock_bh(&tunnel->hlist_lock);
12041199 tunnel->acpt_newsess = false;
12051200 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
12061201 again:
12071202 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
12081203 session = hlist_entry(walk, struct l2tp_session, hlist);
1209
-
1210
- l2tp_info(session, L2TP_MSG_CONTROL,
1211
- "%s: closing session\n", session->name);
1212
-
12131204 hlist_del_init(&session->hlist);
12141205
1215
- if (test_and_set_bit(0, &session->dead))
1216
- goto again;
1217
-
12181206 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
-
1207
+ l2tp_session_delete(session);
12281208 write_lock_bh(&tunnel->hlist_lock);
12291209
12301210 /* Now restart from the beginning of this hash
....@@ -1241,10 +1221,19 @@
12411221 /* Tunnel socket destroy hook for UDP encapsulation */
12421222 static void l2tp_udp_encap_destroy(struct sock *sk)
12431223 {
1244
- struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1224
+ struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
12451225
12461226 if (tunnel)
12471227 l2tp_tunnel_delete(tunnel);
1228
+}
1229
+
1230
+static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
1231
+{
1232
+ struct l2tp_net *pn = l2tp_pernet(net);
1233
+
1234
+ spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1235
+ idr_remove(&pn->l2tp_tunnel_idr, tunnel->tunnel_id);
1236
+ spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
12481237 }
12491238
12501239 /* Workqueue tunnel deletion function */
....@@ -1254,7 +1243,6 @@
12541243 del_work);
12551244 struct sock *sk = tunnel->sock;
12561245 struct socket *sock = sk->sk_socket;
1257
- struct l2tp_net *pn;
12581246
12591247 l2tp_tunnel_closeall(tunnel);
12601248
....@@ -1268,12 +1256,7 @@
12681256 }
12691257 }
12701258
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
-
1259
+ l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
12771260 /* drop initial ref */
12781261 l2tp_tunnel_dec_refcount(tunnel);
12791262
....@@ -1291,10 +1274,10 @@
12911274 * exit hook.
12921275 */
12931276 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)
1277
+ u32 tunnel_id,
1278
+ u32 peer_tunnel_id,
1279
+ struct l2tp_tunnel_cfg *cfg,
1280
+ struct socket **sockp)
12981281 {
12991282 int err = -EINVAL;
13001283 struct socket *sock = NULL;
....@@ -1312,9 +1295,9 @@
13121295 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
13131296 sizeof(udp_conf.peer_ip6));
13141297 udp_conf.use_udp6_tx_checksums =
1315
- ! cfg->udp6_zero_tx_checksums;
1298
+ !cfg->udp6_zero_tx_checksums;
13161299 udp_conf.use_udp6_rx_checksums =
1317
- ! cfg->udp6_zero_rx_checksums;
1300
+ !cfg->udp6_zero_rx_checksums;
13181301 } else
13191302 #endif
13201303 {
....@@ -1339,7 +1322,7 @@
13391322 struct sockaddr_l2tpip6 ip6_addr = {0};
13401323
13411324 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1342
- IPPROTO_L2TP, &sock);
1325
+ IPPROTO_L2TP, &sock);
13431326 if (err < 0)
13441327 goto out;
13451328
....@@ -1347,7 +1330,7 @@
13471330 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
13481331 sizeof(ip6_addr.l2tp_addr));
13491332 ip6_addr.l2tp_conn_id = tunnel_id;
1350
- err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1333
+ err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
13511334 sizeof(ip6_addr));
13521335 if (err < 0)
13531336 goto out;
....@@ -1357,7 +1340,7 @@
13571340 sizeof(ip6_addr.l2tp_addr));
13581341 ip6_addr.l2tp_conn_id = peer_tunnel_id;
13591342 err = kernel_connect(sock,
1360
- (struct sockaddr *) &ip6_addr,
1343
+ (struct sockaddr *)&ip6_addr,
13611344 sizeof(ip6_addr), 0);
13621345 if (err < 0)
13631346 goto out;
....@@ -1367,14 +1350,14 @@
13671350 struct sockaddr_l2tpip ip_addr = {0};
13681351
13691352 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1370
- IPPROTO_L2TP, &sock);
1353
+ IPPROTO_L2TP, &sock);
13711354 if (err < 0)
13721355 goto out;
13731356
13741357 ip_addr.l2tp_family = AF_INET;
13751358 ip_addr.l2tp_addr = cfg->local_ip;
13761359 ip_addr.l2tp_conn_id = tunnel_id;
1377
- err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1360
+ err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
13781361 sizeof(ip_addr));
13791362 if (err < 0)
13801363 goto out;
....@@ -1382,7 +1365,7 @@
13821365 ip_addr.l2tp_family = AF_INET;
13831366 ip_addr.l2tp_addr = cfg->peer_ip;
13841367 ip_addr.l2tp_conn_id = peer_tunnel_id;
1385
- err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1368
+ err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
13861369 sizeof(ip_addr), 0);
13871370 if (err < 0)
13881371 goto out;
....@@ -1395,7 +1378,7 @@
13951378
13961379 out:
13971380 *sockp = sock;
1398
- if ((err < 0) && sock) {
1381
+ if (err < 0 && sock) {
13991382 kernel_sock_shutdown(sock, SHUT_RDWR);
14001383 sock_release(sock);
14011384 *sockp = NULL;
....@@ -1404,19 +1387,18 @@
14041387 return err;
14051388 }
14061389
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)
1390
+int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
1391
+ struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
14101392 {
14111393 struct l2tp_tunnel *tunnel = NULL;
14121394 int err;
14131395 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
14141396
1415
- if (cfg != NULL)
1397
+ if (cfg)
14161398 encap = cfg->encap;
14171399
1418
- tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1419
- if (tunnel == NULL) {
1400
+ tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
1401
+ if (!tunnel) {
14201402 err = -ENOMEM;
14211403 goto err;
14221404 }
....@@ -1424,15 +1406,11 @@
14241406 tunnel->version = version;
14251407 tunnel->tunnel_id = tunnel_id;
14261408 tunnel->peer_tunnel_id = peer_tunnel_id;
1427
- tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
14281409
14291410 tunnel->magic = L2TP_TUNNEL_MAGIC;
14301411 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
14311412 rwlock_init(&tunnel->hlist_lock);
14321413 tunnel->acpt_newsess = true;
1433
-
1434
- if (cfg != NULL)
1435
- tunnel->debug = cfg->debug;
14361414
14371415 tunnel->encap = encap;
14381416
....@@ -1478,11 +1456,18 @@
14781456 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
14791457 struct l2tp_tunnel_cfg *cfg)
14801458 {
1481
- struct l2tp_tunnel *tunnel_walk;
1482
- struct l2tp_net *pn;
1459
+ struct l2tp_net *pn = l2tp_pernet(net);
1460
+ u32 tunnel_id = tunnel->tunnel_id;
14831461 struct socket *sock;
14841462 struct sock *sk;
14851463 int ret;
1464
+
1465
+ spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1466
+ ret = idr_alloc_u32(&pn->l2tp_tunnel_idr, NULL, &tunnel_id, tunnel_id,
1467
+ GFP_ATOMIC);
1468
+ spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1469
+ if (ret)
1470
+ return ret == -ENOSPC ? -EEXIST : ret;
14861471
14871472 if (tunnel->fd < 0) {
14881473 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
....@@ -1494,30 +1479,16 @@
14941479 sock = sockfd_lookup(tunnel->fd, &ret);
14951480 if (!sock)
14961481 goto err;
1497
-
1498
- ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1499
- if (ret < 0)
1500
- goto err_sock;
15011482 }
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);
15171483
15181484 sk = sock->sk;
1519
- sock_hold(sk);
1520
- tunnel->sock = sk;
1485
+ lock_sock(sk);
1486
+ write_lock_bh(&sk->sk_callback_lock);
1487
+ ret = l2tp_validate_socket(sk, net, tunnel->encap);
1488
+ if (ret < 0)
1489
+ goto err_inval_sock;
1490
+ rcu_assign_sk_user_data(sk, tunnel);
1491
+ write_unlock_bh(&sk->sk_callback_lock);
15211492
15221493 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
15231494 struct udp_tunnel_sock_cfg udp_cfg = {
....@@ -1528,27 +1499,38 @@
15281499 };
15291500
15301501 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1531
- } else {
1532
- sk->sk_user_data = tunnel;
15331502 }
15341503
15351504 tunnel->old_sk_destruct = sk->sk_destruct;
15361505 sk->sk_destruct = &l2tp_tunnel_destruct;
1537
- lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1538
- "l2tp_sock");
15391506 sk->sk_allocation = GFP_ATOMIC;
1507
+ release_sock(sk);
1508
+
1509
+ sock_hold(sk);
1510
+ tunnel->sock = sk;
1511
+ tunnel->l2tp_net = net;
1512
+
1513
+ spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1514
+ idr_replace(&pn->l2tp_tunnel_idr, tunnel, tunnel->tunnel_id);
1515
+ spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1516
+
1517
+ trace_register_tunnel(tunnel);
15401518
15411519 if (tunnel->fd >= 0)
15421520 sockfd_put(sock);
15431521
15441522 return 0;
15451523
1546
-err_sock:
1524
+err_inval_sock:
1525
+ write_unlock_bh(&sk->sk_callback_lock);
1526
+ release_sock(sk);
1527
+
15471528 if (tunnel->fd < 0)
15481529 sock_release(sock);
15491530 else
15501531 sockfd_put(sock);
15511532 err:
1533
+ l2tp_tunnel_remove(net, tunnel);
15521534 return ret;
15531535 }
15541536 EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
....@@ -1558,73 +1540,25 @@
15581540 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
15591541 {
15601542 if (!test_and_set_bit(0, &tunnel->dead)) {
1543
+ trace_delete_tunnel(tunnel);
15611544 l2tp_tunnel_inc_refcount(tunnel);
15621545 queue_work(l2tp_wq, &tunnel->del_work);
15631546 }
15641547 }
15651548 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
15661549
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)
1550
+void l2tp_session_delete(struct l2tp_session *session)
16161551 {
16171552 if (test_and_set_bit(0, &session->dead))
1618
- return 0;
1553
+ return;
16191554
1620
- __l2tp_session_unhash(session);
1555
+ trace_delete_session(session);
1556
+ l2tp_session_unhash(session);
16211557 l2tp_session_queue_purge(session);
1622
- if (session->session_close != NULL)
1558
+ if (session->session_close)
16231559 (*session->session_close)(session);
16241560
16251561 l2tp_session_dec_refcount(session);
1626
-
1627
- return 0;
16281562 }
16291563 EXPORT_SYMBOL_GPL(l2tp_session_delete);
16301564
....@@ -1643,16 +1577,16 @@
16431577 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
16441578 session->hdr_len += 4;
16451579 }
1646
-
16471580 }
16481581 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
16491582
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)
1583
+struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
1584
+ u32 peer_session_id, struct l2tp_session_cfg *cfg)
16511585 {
16521586 struct l2tp_session *session;
16531587
1654
- session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1655
- if (session != NULL) {
1588
+ session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
1589
+ if (session) {
16561590 session->magic = L2TP_SESSION_MAGIC;
16571591 session->tunnel = tunnel;
16581592
....@@ -1677,12 +1611,8 @@
16771611 INIT_HLIST_NODE(&session->hlist);
16781612 INIT_HLIST_NODE(&session->global_hlist);
16791613
1680
- /* Inherit debug options from tunnel */
1681
- session->debug = tunnel->debug;
1682
-
16831614 if (cfg) {
16841615 session->pwtype = cfg->pw_type;
1685
- session->debug = cfg->debug;
16861616 session->send_seq = cfg->send_seq;
16871617 session->recv_seq = cfg->recv_seq;
16881618 session->lns_mode = cfg->lns_mode;
....@@ -1693,11 +1623,6 @@
16931623 session->peer_cookie_len = cfg->peer_cookie_len;
16941624 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
16951625 }
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;
17011626
17021627 l2tp_session_set_header_len(session, tunnel->version);
17031628
....@@ -1719,8 +1644,8 @@
17191644 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
17201645 int hash;
17211646
1722
- INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1723
- spin_lock_init(&pn->l2tp_tunnel_list_lock);
1647
+ idr_init(&pn->l2tp_tunnel_idr);
1648
+ spin_lock_init(&pn->l2tp_tunnel_idr_lock);
17241649
17251650 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
17261651 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
....@@ -1734,11 +1659,13 @@
17341659 {
17351660 struct l2tp_net *pn = l2tp_pernet(net);
17361661 struct l2tp_tunnel *tunnel = NULL;
1662
+ unsigned long tunnel_id, tmp;
17371663 int hash;
17381664
17391665 rcu_read_lock_bh();
1740
- list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1741
- l2tp_tunnel_delete(tunnel);
1666
+ idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
1667
+ if (tunnel)
1668
+ l2tp_tunnel_delete(tunnel);
17421669 }
17431670 rcu_read_unlock_bh();
17441671
....@@ -1748,6 +1675,7 @@
17481675
17491676 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
17501677 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
1678
+ idr_destroy(&pn->l2tp_tunnel_idr);
17511679 }
17521680
17531681 static struct pernet_operations l2tp_net_ops = {