[IPV6] ADDRCONF: Allow longer lifetime on 64bit archs.
authorYOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Tue, 27 May 2008 08:37:49 +0000 (17:37 +0900)
committerYOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Wed, 4 Jun 2008 19:02:34 +0000 (04:02 +0900)
- Allow longer lifetimes (>= 0x7fffffff/HZ) on 64bit archs
  by using unsigned long.
- Shadow this arithmetic overflow workaround by introducing
  helper functions: addrconf_timeout_fixup() and
  addrconf_finite_timeout().

Signed-off-by: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
include/net/addrconf.h
net/ipv6/addrconf.c
net/ipv6/route.c

index 0a2f037..bbd3d58 100644 (file)
@@ -94,6 +94,28 @@ extern void                  addrconf_join_solict(struct net_device *dev,
 extern void                    addrconf_leave_solict(struct inet6_dev *idev,
                                        struct in6_addr *addr);
 
+static inline unsigned long addrconf_timeout_fixup(u32 timeout,
+                                                   unsigned unit)
+{
+       if (timeout == 0xffffffff)
+               return ~0UL;
+
+       /*
+        * Avoid arithmetic overflow.
+        * Assuming unit is constant and non-zero, this "if" statement
+        * will go away on 64bit archs.
+        */
+       if (0xfffffffe > LONG_MAX / unit && timeout > LONG_MAX / unit)
+               return LONG_MAX / unit;
+
+       return timeout;
+}
+
+static inline int addrconf_finite_timeout(unsigned long timeout)
+{
+       return ~timeout;
+}
+
 /*
  *     IPv6 Address Label subsystem (addrlabel.c)
  */
index c3b20c5..147588f 100644 (file)
@@ -731,8 +731,13 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
                                                onlink = -1;
 
                                        spin_lock(&ifa->lock);
-                                       lifetime = min_t(unsigned long,
-                                                        ifa->valid_lft, 0x7fffffffUL/HZ);
+
+                                       lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
+                                       /*
+                                        * Note: Because this address is
+                                        * not permanent, lifetime <
+                                        * LONG_MAX / HZ here.
+                                        */
                                        if (time_before(expires,
                                                        ifa->tstamp + lifetime * HZ))
                                                expires = ifa->tstamp + lifetime * HZ;
@@ -1722,7 +1727,6 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
        __u32 valid_lft;
        __u32 prefered_lft;
        int addr_type;
-       unsigned long rt_expires;
        struct inet6_dev *in6_dev;
 
        pinfo = (struct prefix_info *) opt;
@@ -1764,28 +1768,23 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
         *      2) Configure prefixes with the auto flag set
         */
 
-       if (valid_lft == INFINITY_LIFE_TIME)
-               rt_expires = ~0UL;
-       else if (valid_lft >= 0x7FFFFFFF/HZ) {
+       if (pinfo->onlink) {
+               struct rt6_info *rt;
+               unsigned long rt_expires;
+
                /* Avoid arithmetic overflow. Really, we could
                 * save rt_expires in seconds, likely valid_lft,
                 * but it would require division in fib gc, that it
                 * not good.
                 */
-               rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
-       } else
-               rt_expires = valid_lft * HZ;
+               if (HZ > USER_HZ)
+                       rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
+               else
+                       rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
 
-       /*
-        * We convert this (in jiffies) to clock_t later.
-        * Avoid arithmetic overflow there as well.
-        * Overflow can happen only if HZ < USER_HZ.
-        */
-       if (HZ < USER_HZ && ~rt_expires && rt_expires > 0x7FFFFFFF / USER_HZ)
-               rt_expires = 0x7FFFFFFF / USER_HZ;
+               if (addrconf_finite_timeout(rt_expires))
+                       rt_expires *= HZ;
 
-       if (pinfo->onlink) {
-               struct rt6_info *rt;
                rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
                                dev->ifindex, 1);
 
@@ -1794,7 +1793,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
                        if (valid_lft == 0) {
                                ip6_del_rt(rt);
                                rt = NULL;
-                       } else if (~rt_expires) {
+                       } else if (addrconf_finite_timeout(rt_expires)) {
                                /* not infinity */
                                rt->rt6i_expires = jiffies + rt_expires;
                                rt->rt6i_flags |= RTF_EXPIRES;
@@ -1803,9 +1802,9 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
                                rt->rt6i_expires = 0;
                        }
                } else if (valid_lft) {
-                       int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
                        clock_t expires = 0;
-                       if (~rt_expires) {
+                       int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
+                       if (addrconf_finite_timeout(rt_expires)) {
                                /* not infinity */
                                flags |= RTF_EXPIRES;
                                expires = jiffies_to_clock_t(rt_expires);
@@ -2036,6 +2035,7 @@ static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
        int scope;
        u32 flags;
        clock_t expires;
+       unsigned long timeout;
 
        ASSERT_RTNL();
 
@@ -2055,22 +2055,23 @@ static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
 
        scope = ipv6_addr_scope(pfx);
 
-       if (valid_lft == INFINITY_LIFE_TIME) {
-               ifa_flags |= IFA_F_PERMANENT;
-               flags = 0;
-               expires = 0;
-       } else {
-               if (valid_lft >= 0x7FFFFFFF/HZ)
-                       valid_lft = 0x7FFFFFFF/HZ;
+       timeout = addrconf_timeout_fixup(valid_lft, HZ);
+       if (addrconf_finite_timeout(timeout)) {
+               expires = jiffies_to_clock_t(timeout * HZ);
+               valid_lft = timeout;
                flags = RTF_EXPIRES;
-               expires = jiffies_to_clock_t(valid_lft * HZ);
+       } else {
+               expires = 0;
+               flags = 0;
+               ifa_flags |= IFA_F_PERMANENT;
        }
 
-       if (prefered_lft == 0)
-               ifa_flags |= IFA_F_DEPRECATED;
-       else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
-                (prefered_lft != INFINITY_LIFE_TIME))
-               prefered_lft = 0x7FFFFFFF/HZ;
+       timeout = addrconf_timeout_fixup(prefered_lft, HZ);
+       if (addrconf_finite_timeout(timeout)) {
+               if (timeout == 0)
+                       ifa_flags |= IFA_F_DEPRECATED;
+               prefered_lft = timeout;
+       }
 
        ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
 
@@ -3175,26 +3176,28 @@ static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
 {
        u32 flags;
        clock_t expires;
+       unsigned long timeout;
 
        if (!valid_lft || (prefered_lft > valid_lft))
                return -EINVAL;
 
-       if (valid_lft == INFINITY_LIFE_TIME) {
-               ifa_flags |= IFA_F_PERMANENT;
-               flags = 0;
-               expires = 0;
-       } else {
-               if (valid_lft >= 0x7FFFFFFF/HZ)
-                       valid_lft = 0x7FFFFFFF/HZ;
+       timeout = addrconf_timeout_fixup(valid_lft, HZ);
+       if (addrconf_finite_timeout(timeout)) {
+               expires = jiffies_to_clock_t(timeout * HZ);
+               valid_lft = timeout;
                flags = RTF_EXPIRES;
-               expires = jiffies_to_clock_t(valid_lft * HZ);
+       } else {
+               expires = 0;
+               flags = 0;
+               ifa_flags |= IFA_F_PERMANENT;
        }
 
-       if (prefered_lft == 0)
-               ifa_flags |= IFA_F_DEPRECATED;
-       else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
-                (prefered_lft != INFINITY_LIFE_TIME))
-               prefered_lft = 0x7FFFFFFF/HZ;
+       timeout = addrconf_timeout_fixup(prefered_lft, HZ);
+       if (addrconf_finite_timeout(timeout)) {
+               if (timeout == 0)
+                       ifa_flags |= IFA_F_DEPRECATED;
+               prefered_lft = timeout;
+       }
 
        spin_lock_bh(&ifp->lock);
        ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
index 48534c6..220cffe 100644 (file)
@@ -446,7 +446,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
        struct route_info *rinfo = (struct route_info *) opt;
        struct in6_addr prefix_buf, *prefix;
        unsigned int pref;
-       u32 lifetime;
+       unsigned long lifetime;
        struct rt6_info *rt;
 
        if (len < sizeof(struct route_info)) {
@@ -472,13 +472,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
        if (pref == ICMPV6_ROUTER_PREF_INVALID)
                pref = ICMPV6_ROUTER_PREF_MEDIUM;
 
-       lifetime = ntohl(rinfo->lifetime);
-       if (lifetime == 0xffffffff) {
-               /* infinity */
-       } else if (lifetime > 0x7fffffff/HZ - 1) {
-               /* Avoid arithmetic overflow */
-               lifetime = 0x7fffffff/HZ - 1;
-       }
+       lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
 
        if (rinfo->length == 3)
                prefix = (struct in6_addr *)rinfo->prefix;
@@ -506,7 +500,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
                                 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
 
        if (rt) {
-               if (lifetime == 0xffffffff) {
+               if (!addrconf_finite_timeout(lifetime)) {
                        rt->rt6i_flags &= ~RTF_EXPIRES;
                } else {
                        rt->rt6i_expires = jiffies + HZ * lifetime;