xfrm: Flushing empty SPD generates false events
authorJamal Hadi Salim <hadi@cyberus.ca>
Fri, 19 Feb 2010 02:00:42 +0000 (02:00 +0000)
committerDavid S. Miller <davem@davemloft.net>
Fri, 19 Feb 2010 21:11:50 +0000 (13:11 -0800)
To see the effect make sure you have an empty SPD.
On window1 "ip xfrm mon" and on window2 issue "ip xfrm policy flush"
You get prompt back in window2 and you see the flush event on window1.
With this fix, you still get prompt on window1 but no event on window2.

Thanks to Alexey Dobriyan for finding a bug in earlier version
when using pfkey to do the flushing.

Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/key/af_key.c
net/xfrm/xfrm_policy.c
net/xfrm/xfrm_user.c

index c269ce6..a20d2fa 100644 (file)
@@ -2735,8 +2735,11 @@ static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg
        audit_info.secid = 0;
        err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
        err2 = unicast_flush_resp(sk, hdr);
-       if (err || err2)
-               return err ? err : err2;
+       if (err || err2) {
+               if (err == -ESRCH) /* empty table - old silent behavior */
+                       return 0;
+               return err;
+       }
 
        c.data.type = XFRM_POLICY_TYPE_MAIN;
        c.event = XFRM_MSG_FLUSHPOLICY;
index 4368e7b..d6eb16d 100644 (file)
@@ -771,7 +771,8 @@ xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audi
 
 int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
 {
-       int dir, err = 0;
+       int dir, err = 0, cnt = 0;
+       struct xfrm_policy *dp;
 
        write_lock_bh(&xfrm_policy_lock);
 
@@ -789,8 +790,10 @@ int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
                                     &net->xfrm.policy_inexact[dir], bydst) {
                        if (pol->type != type)
                                continue;
-                       __xfrm_policy_unlink(pol, dir);
+                       dp = __xfrm_policy_unlink(pol, dir);
                        write_unlock_bh(&xfrm_policy_lock);
+                       if (dp)
+                               cnt++;
 
                        xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
                                                 audit_info->sessionid,
@@ -809,8 +812,10 @@ int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
                                             bydst) {
                                if (pol->type != type)
                                        continue;
-                               __xfrm_policy_unlink(pol, dir);
+                               dp = __xfrm_policy_unlink(pol, dir);
                                write_unlock_bh(&xfrm_policy_lock);
+                               if (dp)
+                                       cnt++;
 
                                xfrm_audit_policy_delete(pol, 1,
                                                         audit_info->loginuid,
@@ -824,6 +829,8 @@ int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
                }
 
        }
+       if (!cnt)
+               err = -ESRCH;
        atomic_inc(&flow_cache_genid);
 out:
        write_unlock_bh(&xfrm_policy_lock);
index cd94a9d..ee04e6b 100644 (file)
@@ -1679,8 +1679,12 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
        audit_info.sessionid = NETLINK_CB(skb).sessionid;
        audit_info.secid = NETLINK_CB(skb).sid;
        err = xfrm_policy_flush(net, type, &audit_info);
-       if (err)
+       if (err) {
+               if (err == -ESRCH) /* empty table */
+                       return 0;
                return err;
+       }
+
        c.data.type = type;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;