Skip to contentSkip to conversation

Fix unexpected SA hard expiration after setting new date

PATCH RESEND Superseded Fan Du 2 participants 3 messages Jul 27, 2012 – Jul 30, 2012

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Fan Du

author
#0 lore raw

PATCH RESEND v4 1/1 Fix unexpected SA hard expiration after setting new date

Hi, Dave

It has been more than one month since my last post of this patch. Previous posting receives no objection from you and Herbert, so I rebase it with latest linux-3.5, then it can be easily picked up. I'm wondering could you please take a look at this patch ?

Any comments are really welcome!

Thanks

Changelog:
v1->v2
1) use xflags instead of creating new flags(suggested by Steffen Klassert)

v2->v3
1) fix email problem, and remove cc to myself(requested by David Miller)

v3->v4
1) fix typo when clearing XFRM_SOFT_EXPIRE(thanks for David Miller)
2) fix email problem, and remove cc to myself AGAIN!!!

*Background*:
Once IPsec SAs are created between two peers, kernel setup a timer to monitor two events: soft/hard expiration. However the timer handler use xtime to caculate whether it's soft or hard expiration event.

normal code flow(hard expire time:100s, soft expire time:82s)

a) When new SAs created, xfrm_timer_handler is called one second after its creation. At this point, calculate soft expire interval(81s), setup the timer;

b) soft expire occur, rearm the timer with hard expire interval(18s) then notify racoon2 about soft expire event. racoon2 will create new SAs.

c) hard expire happen, notify racoon2 about it. racoon2 will delete the old SAs.

*Scenario*:
Setting a new date before b),and after a) could result c) happens first, As a result, old SAs is deleted before new ones are created. Normally new SAs will be created by the next time networking traffic, but there is a small time being when networking connection is down, this could result in upper layer connections failed in tel comm area, thus it's better to keep it strict in sequence.

*Workaround*:
set new time could happen:
1) before a), then SAs is updated with new time.
2) before b),and after a)
2a) When new SAs created, xfrm_timer_handler is called one second after its creation. At this point, calculate soft expire interval(81s), setup the timer;(set flag to mark next time should be soft time expire)

<<---- new date comes

2b) soft expire occur, the calculation results in a hard time expire event, but flag is set, so catch ya. Sync the addtime, and rearm the timer with hard expire interval(18s), then notify racoon2 about soft expire event;

2c) hard expire happen, notify racoon2 about it;
so everything is in order.

3) after b), hard expire always happened anyway.

author
#1 lore raw

PATCH 1/1 Fix unexpected SA hard expiration after changing date

After SA is setup, one timer is armed to detect soft/hard expiration, however the timer handler uses xtime to do the math. This makes hard expiration occurs first before soft expiration after setting new date with big interval. As a result new child SA is deleted before rekeying the new one.

---
 include/net/xfrm.h    |    4 ++++
 net/xfrm/xfrm_state.c |   22 ++++++++++++++++++----
 2 files changed, 22 insertions(+), 4 deletions(-)
include/net/xfrm.h +4 -0
index d9509eb..62b619e 100644
@@ -213,6 +213,9 @@ struct xfrm_state {
 	struct xfrm_lifetime_cur curlft;
 	struct tasklet_hrtimer	mtimer;
 
+	/* used to fix curlft->add_time when changing date */
+	long		saved_tmo;
+
 	/* Last used time */
 	unsigned long		lastused;
 
@@ -238,6 +241,7 @@ static inline struct net *xs_net(struct xfrm_state *x)
 
 /* xflags - make enum if more show up */
 #define XFRM_TIME_DEFER	1
+#define XFRM_SOFT_EXPIRE 2
 
 enum {
 	XFRM_STATE_VOID,
net/xfrm/xfrm_state.c +18 -4
index 5b228f9..fb64dc6 100644
@@ -415,8 +415,18 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
 	if (x->lft.hard_add_expires_seconds) {
 		long tmo = x->lft.hard_add_expires_seconds +
 			x->curlft.add_time - now;
-		if (tmo <= 0)
-			goto expired;
+		if (tmo <= 0) {
+			if (x->xflags & XFRM_SOFT_EXPIRE) {
+				/* enter hard expire without soft expire first?!
+				 * setting a new date could trigger this.
+				 * workarbound: fix x->curflt.add_time by below:
+				 */
+				x->curlft.add_time = now - x->saved_tmo - 1;
+				tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
+			
+			} else
+				goto expired;
+		}
 		if (tmo < next)
 			next = tmo;
 	}
@@ -433,10 +443,14 @@ static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
 	if (x->lft.soft_add_expires_seconds) {
 		long tmo = x->lft.soft_add_expires_seconds +
 			x->curlft.add_time - now;
-		if (tmo <= 0)
+		if (tmo <= 0) {
 			warn = 1;
-		else if (tmo < next)
+			x->xflags &= ~XFRM_SOFT_EXPIRE;
+		} else if (tmo < next) {
 			next = tmo;
+			x->xflags |= XFRM_SOFT_EXPIRE;
+			x->saved_tmo = tmo;
+		}
 	}
 	if (x->lft.soft_use_expires_seconds) {
 		long tmo = x->lft.soft_use_expires_seconds +
Signature · 1 line
1.7.1
maintainer
#2 lore raw

Re: [PATCH] [XFRM] Fix unexpected SA hard expiration after changing date

From: Fan Du <[email protected]>
Date: Fri, 27 Jul 2012 10:39:19 +0800

net/xfrm/xfrm_state.c static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me) · line 426

Quoted 3 lines
net/xfrm/xfrm_state.c +3 -0
+				x->curlft.add_time = now - x->saved_tmo - 1;
+				tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
+			

^^^^^^^^^^^^^^^^

net/xfrm/xfrm_state.c static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me) · line 427

Quoted 1 line
net/xfrm/xfrm_state.c +1 -0
+			} else

Unnecessary empty line, please remove it.