Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] l2tp: bound the reorder queue
@ 2026-11-23 16:20 Ren Wei
  2026-11-24 16:30 ` netdev-bot+sashiko
  0 siblings, 1 reply; 3+ messages in thread
From: Ren Wei @ 2026-11-23 16:20 UTC (permalink / raw)
  To: netdev, edumazet
  Cc: davem, kuba, pabeni, willemb, horms, gnault, kuniyu,
	michael.bommarito, alice.kernel, nicoyip.dev, hengyul, jchapman,
	vega, caoruide123, weir

From: Ruide Cao <caoruide123@gmail.com>

The receive-window check accepts every sequence number in the large
window. If the peer omits the expected packet, distinct future packets
can therefore fill the ordered reorder queue. Since insertion walks the
queue and expiration was checked only while receiving another packet,
this allowed unbounded memory and CPU use.

Reject duplicate sequence numbers and cap the reorder queue at 64
packets. Packets beyond the cap are discarded, except that the expected
sequence number is admitted so an in-order packet can drain the queue.
The no-reorder tail-enqueue path uses the same exception, and also
admits a sequenced recovery packet once reorder_skip is set. That
exception is limited to a single extra slot and is refused when the
queue head cannot be dequeued, so a stalled head cannot grow the
queue without bound. Advance session->nr under the reorder queue lock
when unlinking a sequenced skb, so the cap checks observe a consistent
expected sequence number. Declare the dequeued flag first in
l2tp_recv_dequeue() to follow reverse xmas tree.

The existing timeout recovery then skips a missing sequence number when
the queue limit is reached.

Use a session timer to run the existing dequeue path at the head
expiration and shut it down before purging a session. This bounds the
queue and services expiration even when the peer stops sending.

Insert into the reorder queue by walking from the tail so an in-order
packet after a gap does not scan the queue from the head. Compare
sequence numbers only for skbs with has_seq set, and use modular
comparison in the 16-bit or 24-bit space so wrap-around is handled.
Refuse enqueue once session->dead is set under the queue lock, and
purge the queue again in l2tp_session_free() so an in-flight receive
path cannot leak an skb after the deletion purge.

Fixes: 3557baabf280 ("[L2TP]: PPP over L2TP driver core")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Signed-off-by: Ruide Cao <caoruide123@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>

---
Changes in v2:
- Insert in-order-after-gap packets by walking the reorder queue from
  the tail, as suggested by Eric Dumazet, so insertion does not always
  scan from the head.
- Compare Ns only when has_seq is set, and use modular comparison in
  the L2TPv2 16-bit / L2TPv3 24-bit sequence space instead of a raw
  '>' check.
- Check session->dead under reorder_q.lock at the enqueue sites, and
  purge the queue again in l2tp_session_free(), so an in-flight receive
  path cannot leak an skb after timer_shutdown_sync() and the deletion
  purge.
- Advance session->nr under reorder_q.lock when unlinking a sequenced
  skb, so the full-queue expected-Ns exception cannot race with dequeue.
- On the no-reorder tail path, admit one sequenced expected or recovery
  packet when the queue is at the cap, but only if the head can be
  dequeued. Further packets are rejected until dequeue makes progress,
  so a stalled head cannot bypass the cap.
- Declare the dequeued flag first in l2tp_recv_dequeue() to follow
  reverse xmas tree.
- Keep dropping non-expected packets when the queue is full; that is
  the cap. After the head times out, reorder_skip lets the queue drain.
- Leave the dequeue-timer dst lifetime unchanged: teardown already
  uses timer_shutdown_sync() and a second purge, and this path does
  not dereference dst on its own.
- Leave concurrent timer and receive dequeue otherwise unchanged;
  the Nr race is fixed by updating session->nr under the queue lock.
- Leave the l2tp_recv_data_seq() return-value comment unchanged; a
  rejected skb stays with the caller and is freed on the discard path.
- Leave timer rearming unchanged if reorder_timeout is shortened at
  runtime; a tighter deadline is left for a follow-up.
- v1 link:
  https://lore.kernel.org/netdev/cover.1790136447.git.caoruide123@gmail.com/
---
 net/l2tp/l2tp_core.c | 136 +++++++++++++++++++++++++++++++++++++------
 net/l2tp/l2tp_core.h |   2 +
 2 files changed, 121 insertions(+), 17 deletions(-)

diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
index 0cd3e38bf662..42900c3cff63 100644
--- a/net/l2tp/l2tp_core.c
+++ b/net/l2tp/l2tp_core.c
@@ -82,6 +82,7 @@
 #define L2TP_SL_SEQ_MASK   0x00ffffff
 
 #define L2TP_HDR_SIZE_MAX		14
+#define L2TP_REORDER_MAX_QUEUE	64
 
 /* Default trace flags */
 #define L2TP_DEFAULT_DEBUG_FLAGS	0
@@ -166,9 +167,13 @@ static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
 	kfree_rcu(tunnel, rcu);
 }
 
+static void l2tp_session_queue_purge(struct l2tp_session *session);
+
 static void l2tp_session_free(struct l2tp_session *session)
 {
 	trace_free_session(session);
+	/* Catch a post-purge enqueue from an in-flight receive path. */
+	l2tp_session_queue_purge(session);
 	if (session->tunnel)
 		l2tp_tunnel_put(session->tunnel);
 	kfree_rcu(session, rcu);
@@ -636,30 +641,104 @@ EXPORT_SYMBOL_GPL(l2tp_session_register);
  * Receive data handling
  *****************************************************************************/
 
+/* True if sequence number a precedes b in the 16-bit (L2TPv2) or
+ * 24-bit (L2TPv3) sequence space.
+ */
+static bool l2tp_seq_before(const struct l2tp_session *session, u32 a, u32 b)
+{
+	u32 dist = (b - a) & session->nr_max;
+
+	return dist && dist <= session->nr_max / 2;
+}
+
 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
- * number.
+ * number. Walk from the tail so an in-order packet after a gap is not
+ * inserted by scanning the whole queue from the head.
  */
 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
 {
 	struct sk_buff *skbp;
-	struct sk_buff *tmp;
 	u32 ns = L2TP_SKB_CB(skb)->ns;
+	bool saw_seq = false;
 
 	spin_lock_bh(&session->reorder_q.lock);
-	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
-		if (L2TP_SKB_CB(skbp)->ns > ns) {
-			__skb_queue_before(&session->reorder_q, skbp, skb);
-			atomic_long_inc(&session->stats.rx_oos_packets);
+	if (test_bit(0, &session->dead)) {
+		kfree_skb(skb);
+		goto out;
+	}
+	if (skb_queue_len(&session->reorder_q) >= L2TP_REORDER_MAX_QUEUE &&
+	    ns != session->nr) {
+		atomic_long_inc(&session->stats.rx_seq_discards);
+		atomic_long_inc(&session->stats.rx_errors);
+		kfree_skb(skb);
+		goto out;
+	}
+
+	skb_queue_reverse_walk(&session->reorder_q, skbp) {
+		if (!L2TP_SKB_CB(skbp)->has_seq)
+			continue;
+		if (unlikely(L2TP_SKB_CB(skbp)->ns == ns)) {
+			atomic_long_inc(&session->stats.rx_seq_discards);
+			atomic_long_inc(&session->stats.rx_errors);
+			kfree_skb(skb);
 			goto out;
 		}
+		if (l2tp_seq_before(session, L2TP_SKB_CB(skbp)->ns, ns)) {
+			__skb_queue_after(&session->reorder_q, skbp, skb);
+			if (saw_seq)
+				atomic_long_inc(&session->stats.rx_oos_packets);
+			goto out;
+		}
+		saw_seq = true;
 	}
 
-	__skb_queue_tail(&session->reorder_q, skb);
+	if (saw_seq)
+		atomic_long_inc(&session->stats.rx_oos_packets);
+	__skb_queue_head(&session->reorder_q, skb);
 
 out:
 	spin_unlock_bh(&session->reorder_q.lock);
 }
 
+static bool l2tp_recv_queue_tail_skb(struct l2tp_session *session,
+				     struct sk_buff *skb)
+{
+	struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
+	struct l2tp_skb_cb *hcb;
+	struct sk_buff *head;
+	unsigned int qlen;
+
+	spin_lock_bh(&session->reorder_q.lock);
+	if (test_bit(0, &session->dead))
+		goto reject;
+	/* Admit one expected or recovery packet when the queue is at
+	 * the cap, but only if the head can be dequeued. Further
+	 * packets are rejected until dequeue makes progress.
+	 */
+	qlen = skb_queue_len(&session->reorder_q);
+	if (qlen >= L2TP_REORDER_MAX_QUEUE) {
+		if (qlen > L2TP_REORDER_MAX_QUEUE)
+			goto reject;
+		if (!(cb->has_seq &&
+		      (cb->ns == session->nr || session->reorder_skip)))
+			goto reject;
+
+		head = skb_peek(&session->reorder_q);
+		hcb = L2TP_SKB_CB(head);
+		if (!time_after(jiffies, hcb->expires) &&
+		    hcb->has_seq && !session->reorder_skip &&
+		    hcb->ns != session->nr)
+			goto reject;
+	}
+	__skb_queue_tail(&session->reorder_q, skb);
+	spin_unlock_bh(&session->reorder_q.lock);
+	return true;
+
+reject:
+	spin_unlock_bh(&session->reorder_q.lock);
+	return false;
+}
+
 /* Dequeue a single skb.
  */
 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
@@ -677,13 +756,6 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
 	atomic_long_inc(&session->stats.rx_packets);
 	atomic_long_add(length, &session->stats.rx_bytes);
 
-	if (L2TP_SKB_CB(skb)->has_seq) {
-		/* Bump our Nr */
-		session->nr++;
-		session->nr &= session->nr_max;
-		trace_session_seqnum_update(session);
-	}
-
 	/* call private receive handler */
 	if (session->recv_skb)
 		(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
@@ -696,6 +768,7 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
  */
 static void l2tp_recv_dequeue(struct l2tp_session *session)
 {
+	bool dequeued = false;
 	struct sk_buff *skb;
 	struct sk_buff *tmp;
 
@@ -715,6 +788,7 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
 			trace_session_pkt_expired(session, cb->ns);
 			session->reorder_skip = 1;
 			__skb_unlink(skb, &session->reorder_q);
+			dequeued = true;
 			kfree_skb(skb);
 			continue;
 		}
@@ -729,6 +803,16 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
 				goto out;
 		}
 		__skb_unlink(skb, &session->reorder_q);
+		dequeued = true;
+
+		/* Advance Nr under the queue lock so the enqueue cap
+		 * checks observe a consistent expected sequence number.
+		 */
+		if (cb->has_seq) {
+			session->nr++;
+			session->nr &= session->nr_max;
+			trace_session_seqnum_update(session);
+		}
 
 		/* Process the skb. We release the queue lock while we
 		 * do so to let other contexts process the queue.
@@ -739,9 +823,22 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
 	}
 
 out:
+	if (skb_queue_empty(&session->reorder_q))
+		timer_delete(&session->reorder_timer);
+	else if (dequeued || !timer_pending(&session->reorder_timer))
+		timer_reduce(&session->reorder_timer,
+			     L2TP_SKB_CB(skb_peek(&session->reorder_q))->expires);
 	spin_unlock_bh(&session->reorder_q.lock);
 }
 
+static void l2tp_recv_dequeue_timer(struct timer_list *timer)
+{
+	struct l2tp_session *session = timer_container_of(session, timer,
+							 reorder_timer);
+
+	l2tp_recv_dequeue(session);
+}
+
 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
 {
 	u32 nws;
@@ -783,7 +880,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
 	 * sequence number to re-enable packet reception.
 	 */
 	if (cb->ns == session->nr) {
-		skb_queue_tail(&session->reorder_q, skb);
+		if (!l2tp_recv_queue_tail_skb(session, skb))
+			goto discard;
 	} else {
 		u32 nr_oos = cb->ns;
 		u32 nr_next = (session->nr_oos + 1) & session->nr_max;
@@ -802,7 +900,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
 			trace_session_pkt_oos(session, cb->ns);
 			goto discard;
 		}
-		skb_queue_tail(&session->reorder_q, skb);
+		if (!l2tp_recv_queue_tail_skb(session, skb))
+			goto discard;
 	}
 
 out:
@@ -991,7 +1090,8 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
 		 * reorder queue. This ensures that it will be
 		 * delivered after all previous sequenced skbs.
 		 */
-		skb_queue_tail(&session->reorder_q, skb);
+		if (!l2tp_recv_queue_tail_skb(session, skb))
+			goto discard;
 	}
 
 	/* Try to dequeue as many skbs from reorder_q as we can. */
@@ -1760,6 +1860,7 @@ static void l2tp_session_del_work(struct work_struct *work)
 						    del_work);
 
 	l2tp_session_unhash(session);
+	timer_shutdown_sync(&session->reorder_timer);
 	l2tp_session_queue_purge(session);
 	if (session->session_close)
 		(*session->session_close)(session);
@@ -1810,6 +1911,7 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn
 			tunnel->tunnel_id, session->session_id);
 
 		skb_queue_head_init(&session->reorder_q);
+		timer_setup(&session->reorder_timer, l2tp_recv_dequeue_timer, 0);
 
 		session->hlist_key = l2tp_v3_session_hashkey(tunnel->sock, session->session_id);
 		INIT_HLIST_NODE(&session->hlist);
diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h
index 25ae4c25d3ef..bf403598fff8 100644
--- a/net/l2tp/l2tp_core.h
+++ b/net/l2tp/l2tp_core.h
@@ -4,6 +4,7 @@
  * Copyright (c) 2008,2009 Katalix Systems Ltd
  */
 #include <linux/refcount.h>
+#include <linux/timer.h>
 
 #ifndef _L2TP_CORE_H_
 #define _L2TP_CORE_H_
@@ -80,6 +81,7 @@ struct l2tp_session {
 	u32			nr;		/* session NR state (receive) */
 	u32			ns;		/* session NR state (send) */
 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
+	struct timer_list	reorder_timer;
 	u32			nr_max;		/* max NR. Depends on tunnel */
 	u32			nr_window_size;	/* NR window size */
 	u32			nr_oos;		/* NR of last OOS packet */

base-commit: ad2c7240a3981fd5f0142b61ca913b02ab476ede
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] l2tp: bound the reorder queue
@ 2026-11-23 16:22 netdev-bot+sinfo
  0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sinfo @ 2026-11-23 16:22 UTC (permalink / raw)
  To: Ren Wei
  Cc: netdev, edumazet, davem, kuba, pabeni, willemb, horms, gnault,
	kuniyu, michael.bommarito, alice.kernel, nicoyip.dev, hengyul,
	jchapman, vega, caoruide123

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] l2tp: bound the reorder queue
  2026-11-23 16:20 [PATCH net v2] l2tp: bound the reorder queue Ren Wei
@ 2026-11-24 16:30 ` netdev-bot+sashiko
  0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 16:30 UTC (permalink / raw)
  To: weir
  Cc: netdev, edumazet, davem, kuba, pabeni, willemb, horms, gnault,
	kuniyu, michael.bommarito, alice.kernel, nicoyip.dev, hengyul,
	jchapman, vega, caoruide123

Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 2 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] With reordering enabled (reorder_timeout != 0), the new fixed
  cap of 64 combined with timeout-only recovery turns a single lost…
- [Medium] The two new enqueue helpers enforce the same reorder-queue cap
  but update the userspace-visible session counters differently.
- [Medium] The new reorder_timer adds a second dequeuer that runs in
  timer softirq.
- [Low] The comment above l2tp_recv_data_seq() says it will "queue it if
  acceptable.
- [Low] The deadline and the expiry check disagree by one tick.
- [Low] l2tp_recv_dequeue() now also runs from timer context and writes
  session->nr and session->reorder_skip under reorder_q.lock.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but skbs parked in
  session->reorder_q keep the non-refcounted (noref) dst they were…

--

l2tp: bound the reorder queue

This patch caps the L2TP session reorder queue at 64 packets, rejects
duplicate sequence numbers, and inserts sequenced packets by walking from
the tail with a modular sequence comparison. It adds a per-session
reorder_timer that runs l2tp_recv_dequeue() at the head expiry, and it
moves the session->nr advance under the reorder queue lock. Session
teardown shuts down the timer, and l2tp_session_free() purges the queue a
second time.

> diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
> index 0cd3e38bf662f..42900c3cff632 100644
> --- a/net/l2tp/l2tp_core.c
> +++ b/net/l2tp/l2tp_core.c

[ ... ]

> @@ -636,30 +641,104 @@ EXPORT_SYMBOL_GPL(l2tp_session_register);

[ ... ]

>  static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
>  {
>  	struct sk_buff *skbp;
> -	struct sk_buff *tmp;
>  	u32 ns = L2TP_SKB_CB(skb)->ns;
> +	bool saw_seq = false;
>  
>  	spin_lock_bh(&session->reorder_q.lock);
> -	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
> -		if (L2TP_SKB_CB(skbp)->ns > ns) {
> -			__skb_queue_before(&session->reorder_q, skbp, skb);
> -			atomic_long_inc(&session->stats.rx_oos_packets);
> +	if (test_bit(0, &session->dead)) {
> +		kfree_skb(skb);
> +		goto out;
> +	}
> +	if (skb_queue_len(&session->reorder_q) >= L2TP_REORDER_MAX_QUEUE &&
> +	    ns != session->nr) {
> +		atomic_long_inc(&session->stats.rx_seq_discards);
> +		atomic_long_inc(&session->stats.rx_errors);
> +		kfree_skb(skb);
> +		goto out;
> +	}

[Severity: High]
With reordering enabled, can one lost packet cause a stall that repeats
every reorder_timeout?

Suppose session->nr == g and packet g is lost. Packets g+1..g+64 fill
reorder_q, and the head blocks because cb->ns != session->nr. This cap
check then frees every later arrival from g+65 on. The expected-Ns
exception never fires, because the L2TP data channel does not retransmit
g.

When the head expires, l2tp_recv_dequeue() drops it, sets reorder_skip,
resyncs nr to the next queued Ns and drains the queue. That leaves
session->nr == g+65:

l2tp_recv_dequeue() {
    ...
		if (time_after(jiffies, cb->expires)) {
			...
			session->reorder_skip = 1;
			__skb_unlink(skb, &session->reorder_q);
    ...
}

This check already dropped g+65. The next arrival is inside the nr_max/2
receive window, so it gets queued and blocks again.

The nr_oos recovery exists only in the reorder_timeout == 0 branch of
l2tp_recv_data_seq(). The reorder branch comes straight here:

	if (session->reorder_timeout != 0) {
		...
		l2tp_recv_queue_skb(session, skb);
		goto out;
	}

So the queue refills to 64, further packets are dropped, and the cycle
repeats. The session would deliver about 63 packets per reorder_timeout
for as long as traffic stays above about 64 packets per reorder_timeout.
The only exception is when all 64 queued packets expire in the same tick.
Then reorder_skip stays set and the next packet resyncs nr.

Before this patch, the whole backlog was queued and drained after one
timeout. nr then matched the next arrival and the session recovered.

The commit message says:

    The existing timeout recovery then skips a missing sequence number when
    the queue limit is reached.

Is that accurate? Nothing seems to skip when the limit is reached. Only
head expiry does, and it reopens a gap every cycle.

Would a full queue need to force a skip or a dequeue here?

[ ... ]

> +static bool l2tp_recv_queue_tail_skb(struct l2tp_session *session,
> +				     struct sk_buff *skb)
> +{

[ ... ]

> +	spin_lock_bh(&session->reorder_q.lock);
> +	if (test_bit(0, &session->dead))
> +		goto reject;

[ ... ]

> +	qlen = skb_queue_len(&session->reorder_q);
> +	if (qlen >= L2TP_REORDER_MAX_QUEUE) {
> +		if (qlen > L2TP_REORDER_MAX_QUEUE)
> +			goto reject;
> +		if (!(cb->has_seq &&
> +		      (cb->ns == session->nr || session->reorder_skip)))
> +			goto reject;

[ ... ]

> +	__skb_queue_tail(&session->reorder_q, skb);
> +	spin_unlock_bh(&session->reorder_q.lock);
> +	return true;
> +
> +reject:
> +	spin_unlock_bh(&session->reorder_q.lock);
> +	return false;
> +}

[Severity: Medium]
Should the two enqueue helpers count drops the same way?

In l2tp_recv_queue_skb(), a cap drop or a duplicate drop increments both
rx_seq_discards and rx_errors. The session->dead drop increments neither.

In l2tp_recv_queue_tail_skb(), every rejection returns false: dead,
qlen above the cap, not expected/recovery at the cap, or head not
dequeuable. The callers end up at the discard label in
l2tp_recv_common(), which only increments rx_errors:

discard:
	atomic_long_inc(&session->stats.rx_errors);
	kfree_skb(skb);

So a cap drop is a sequence discard in reorder mode but not in
non-reorder mode. A dead-session drop is an rx error on one path and is
not counted at all on the other.

These counters are exported via netlink (L2TP_ATTR_RX_SEQ_DISCARDS and
L2TP_ATTR_RX_ERRORS), the pppol2tp stats ioctl and debugfs.

[ ... ]

> @@ -729,6 +803,16 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
>  				goto out;
>  		}
>  		__skb_unlink(skb, &session->reorder_q);
> +		dequeued = true;
> +
> +		/* Advance Nr under the queue lock so the enqueue cap
> +		 * checks observe a consistent expected sequence number.
> +		 */
> +		if (cb->has_seq) {
> +			session->nr++;
> +			session->nr &= session->nr_max;
> +			trace_session_seqnum_update(session);
> +		}
>  
>  		/* Process the skb. We release the queue lock while we
>  		 * do so to let other contexts process the queue.

[Severity: Medium]
Can the new reorder_timer deliver packets to the pseudowire out of order?

The timer is not pinned. l2tp_recv_dequeue_timer() can run on one CPU
while the receive path runs l2tp_recv_dequeue() on another. Both advance
nr under the lock, then drop the lock before calling recv_skb:

CPU A (timer)
  drops expired head, resyncs to N+2
  unlinks N+2, nr = N+3
  spin_unlock_bh()

CPU B (rx softirq)
  spin_lock_bh()
  head N+3 == nr, unlinks it, nr = N+4
  spin_unlock_bh()
  recv_skb(N+3)

CPU A
  recv_skb(N+2)

Nothing orders recv_skb(N+2) on CPU A before recv_skb(N+3) on CPU B. So
ppp_input(), sock_queue_rcv_skb() or l2tp_eth could get them in reverse
order.

The changelog says concurrent timer and receive dequeue is left
unchanged. Before this patch, though, dequeue only ran from the session's
receive path. Is this second, concurrent dequeuer intended?

> @@ -739,9 +823,22 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
>  	}
>  
>  out:
> +	if (skb_queue_empty(&session->reorder_q))
> +		timer_delete(&session->reorder_timer);
> +	else if (dequeued || !timer_pending(&session->reorder_timer))
> +		timer_reduce(&session->reorder_timer,
> +			     L2TP_SKB_CB(skb_peek(&session->reorder_q))->expires);

[Severity: Low]
Do the timer deadline and the expiry check disagree by one tick?

The timer is armed for exactly the head's expires value, but the walk
uses a strict comparison:

		if (time_after(jiffies, cb->expires)) {

When the timer fires at jiffies == expires, the head is not treated as
expired. A blocked sequenced head takes goto out. dequeued is false, and
the timer is not pending because its callback is running. timer_reduce()
then re-arms it for the same deadline, which has already passed, so it
fires again on the next tick.

That costs an extra callback and a locked queue walk on each expiry.
Would arming at expires + 1, or using time_after_eq() in the check, make
the two agree?

>  	spin_unlock_bh(&session->reorder_q.lock);
>  }
>  
> +static void l2tp_recv_dequeue_timer(struct timer_list *timer)
> +{
> +	struct l2tp_session *session = timer_container_of(session, timer,
> +							 reorder_timer);
> +
> +	l2tp_recv_dequeue(session);
> +}

[Severity: High]
This is a pre-existing issue and was not introduced by this patch.

skbs parked in session->reorder_q keep the noref dst they were received
with. They are delivered after the RCU section that kept that dst alive
has ended.

For connected UDP tunnel sockets, udp_v4_early_demux() attaches
sk->sk_rx_dst with skb_dst_set_noref(), and ip_route_input_noref()
attaches cached input routes as noref. udp_queue_rcv_one_skb() then calls
encap_rcv() without forcing or dropping the dst. Neither
l2tp_udp_recv_core() nor l2tp_recv_common() drops it before queueing.

Once the softirq ends, the route can be replaced and freed after a grace
period, for example an rt_genid bump followed by udp_sk_rx_dst_set()
releasing the old dst. Delivery happens later, either from another
packet's receive or, with this patch, from l2tp_recv_dequeue_timer().

For a PPPoL2TP socket that is connected but not PPPOX_BOUND:

l2tp_recv_dequeue_timer()
  l2tp_recv_dequeue()
    l2tp_recv_dequeue_skb()
      pppol2tp_recv()
        sock_queue_rcv_skb()
          __sock_queue_rcv_skb()
            skb_dst_force()
              dst_hold_safe() on the stale dst

Can that rcuref_get() run on a freed dst_entry?

The rcu_read_lock() in pppol2tp_recv() hides the WARN_ON in
skb_dst_force(). It does not protect a dst that came from an earlier RCU
section. l2tp_eth calls skb_dst_drop() first, and the ppp_input() path
only drops the dst without dereferencing it.

The v2 changelog says the timer path "does not dereference dst on its
own". That is true for l2tp_core, but the recv_skb callback it calls
does dereference it.

Would calling skb_dst_drop() before queueing the skb in
l2tp_recv_common() fix this? Calling skb_dst_force() while still inside
the original RCU section would also work.

[ ... ]

> @@ -783,7 +880,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
>  	 * sequence number to re-enable packet reception.
>  	 */
>  	if (cb->ns == session->nr) {
> -		skb_queue_tail(&session->reorder_q, skb);
> +		if (!l2tp_recv_queue_tail_skb(session, skb))
> +			goto discard;

[Severity: Low]
l2tp_recv_dequeue() now also runs from timer context and writes
session->nr and session->reorder_skip under reorder_q.lock. Is this
unlocked read of session->nr a data race?

l2tp_recv_data_seq() still uses plain, unlocked accesses on the same
state. It reads session->nr in l2tp_seq_check_rx_window() and in the
comparison above. Further down it also accesses reorder_skip, nr_oos and
nr_oos_count, including this write:

		if (session->nr_oos_count > session->nr_oos_count_max) {
			session->reorder_skip = 1;
		}

On the timer side:

			if (session->reorder_skip) {
				session->reorder_skip = 0;
				session->nr = cb->ns;

KCSAN would likely report this once the timer runs on another CPU.

l2tp_recv_queue_tail_skb() also does not re-check Ns against nr when qlen
is below the cap. In non-reorder mode, a packet accepted on a stale nr
could be appended after the timer has resynced nr. It would then block
the head until reorder_skip is set again or the head expires.

The commit message says nr is advanced under the lock "so the cap checks
observe a consistent expected sequence number". Does that also hold for
this enqueue decision, which is made outside the lock?

>  	} else {

[ ... ]

> @@ -802,7 +900,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
>  			trace_session_pkt_oos(session, cb->ns);
>  			goto discard;
>  		}
> -		skb_queue_tail(&session->reorder_q, skb);
> +		if (!l2tp_recv_queue_tail_skb(session, skb))
> +			goto discard;
>  	}

[Severity: Low]
This isn't a bug, but the comment above l2tp_recv_data_seq() no longer
matches what it does:

/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
 * acceptable, else non-zero.
 */

The reorder branch calls l2tp_recv_queue_skb(), which can now reject and
free the skb itself: when session->dead is set, when the cap is reached
with ns != nr, or for a duplicate Ns. l2tp_recv_data_seq() still does
goto out and returns 0 in those cases. This branch, in contrast, returns
1 when l2tp_recv_queue_tail_skb() rejects.

The v2 changelog says the comment was left unchanged because a rejected
skb stays with the caller. That reasoning only covers the return-1 path.

Could the comment be updated to describe who owns the skb on each return?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/ccbcb9ac392bc81663a7a14691058b3f03eac3c0.1795437306.git.caoruide123%40gmail.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 16:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 16:20 [PATCH net v2] l2tp: bound the reorder queue Ren Wei
2026-11-24 16:30 ` netdev-bot+sashiko
  -- strict thread matches above, loose matches on Subject: below --
2026-11-23 16:22 netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox