Commit 4bed72e4 authored by YOSHIFUJI Hideaki's avatar YOSHIFUJI Hideaki

[IPV6] ADDRCONF: Allow longer lifetime on 64bit archs.

- 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: default avatarYOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
parent baa2bfb8
...@@ -94,6 +94,28 @@ extern void addrconf_join_solict(struct net_device *dev, ...@@ -94,6 +94,28 @@ extern void addrconf_join_solict(struct net_device *dev,
extern void addrconf_leave_solict(struct inet6_dev *idev, extern void addrconf_leave_solict(struct inet6_dev *idev,
struct in6_addr *addr); 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) * IPv6 Address Label subsystem (addrlabel.c)
*/ */
......
...@@ -731,8 +731,13 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp) ...@@ -731,8 +731,13 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
onlink = -1; onlink = -1;
spin_lock(&ifa->lock); 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, if (time_before(expires,
ifa->tstamp + lifetime * HZ)) ifa->tstamp + lifetime * HZ))
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) ...@@ -1722,7 +1727,6 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
__u32 valid_lft; __u32 valid_lft;
__u32 prefered_lft; __u32 prefered_lft;
int addr_type; int addr_type;
unsigned long rt_expires;
struct inet6_dev *in6_dev; struct inet6_dev *in6_dev;
pinfo = (struct prefix_info *) opt; pinfo = (struct prefix_info *) opt;
...@@ -1764,28 +1768,23 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) ...@@ -1764,28 +1768,23 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
* 2) Configure prefixes with the auto flag set * 2) Configure prefixes with the auto flag set
*/ */
if (valid_lft == INFINITY_LIFE_TIME) if (pinfo->onlink) {
rt_expires = ~0UL; struct rt6_info *rt;
else if (valid_lft >= 0x7FFFFFFF/HZ) { unsigned long rt_expires;
/* Avoid arithmetic overflow. Really, we could /* Avoid arithmetic overflow. Really, we could
* save rt_expires in seconds, likely valid_lft, * save rt_expires in seconds, likely valid_lft,
* but it would require division in fib gc, that it * but it would require division in fib gc, that it
* not good. * not good.
*/ */
rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ); if (HZ > USER_HZ)
} else rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
rt_expires = valid_lft * HZ; else
rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
/* if (addrconf_finite_timeout(rt_expires))
* We convert this (in jiffies) to clock_t later. rt_expires *= HZ;
* 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 (pinfo->onlink) {
struct rt6_info *rt;
rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL, rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
dev->ifindex, 1); dev->ifindex, 1);
...@@ -1794,7 +1793,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) ...@@ -1794,7 +1793,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
if (valid_lft == 0) { if (valid_lft == 0) {
ip6_del_rt(rt); ip6_del_rt(rt);
rt = NULL; rt = NULL;
} else if (~rt_expires) { } else if (addrconf_finite_timeout(rt_expires)) {
/* not infinity */ /* not infinity */
rt->rt6i_expires = jiffies + rt_expires; rt->rt6i_expires = jiffies + rt_expires;
rt->rt6i_flags |= RTF_EXPIRES; rt->rt6i_flags |= RTF_EXPIRES;
...@@ -1803,9 +1802,9 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) ...@@ -1803,9 +1802,9 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
rt->rt6i_expires = 0; rt->rt6i_expires = 0;
} }
} else if (valid_lft) { } else if (valid_lft) {
int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
clock_t expires = 0; clock_t expires = 0;
if (~rt_expires) { int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
if (addrconf_finite_timeout(rt_expires)) {
/* not infinity */ /* not infinity */
flags |= RTF_EXPIRES; flags |= RTF_EXPIRES;
expires = jiffies_to_clock_t(rt_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, ...@@ -2036,6 +2035,7 @@ static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
int scope; int scope;
u32 flags; u32 flags;
clock_t expires; clock_t expires;
unsigned long timeout;
ASSERT_RTNL(); ASSERT_RTNL();
...@@ -2055,22 +2055,23 @@ static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx, ...@@ -2055,22 +2055,23 @@ static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
scope = ipv6_addr_scope(pfx); scope = ipv6_addr_scope(pfx);
if (valid_lft == INFINITY_LIFE_TIME) { timeout = addrconf_timeout_fixup(valid_lft, HZ);
ifa_flags |= IFA_F_PERMANENT; if (addrconf_finite_timeout(timeout)) {
flags = 0; expires = jiffies_to_clock_t(timeout * HZ);
expires = 0; valid_lft = timeout;
} else {
if (valid_lft >= 0x7FFFFFFF/HZ)
valid_lft = 0x7FFFFFFF/HZ;
flags = RTF_EXPIRES; 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) timeout = addrconf_timeout_fixup(prefered_lft, HZ);
ifa_flags |= IFA_F_DEPRECATED; if (addrconf_finite_timeout(timeout)) {
else if ((prefered_lft >= 0x7FFFFFFF/HZ) && if (timeout == 0)
(prefered_lft != INFINITY_LIFE_TIME)) ifa_flags |= IFA_F_DEPRECATED;
prefered_lft = 0x7FFFFFFF/HZ; prefered_lft = timeout;
}
ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 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, ...@@ -3175,26 +3176,28 @@ static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
{ {
u32 flags; u32 flags;
clock_t expires; clock_t expires;
unsigned long timeout;
if (!valid_lft || (prefered_lft > valid_lft)) if (!valid_lft || (prefered_lft > valid_lft))
return -EINVAL; return -EINVAL;
if (valid_lft == INFINITY_LIFE_TIME) { timeout = addrconf_timeout_fixup(valid_lft, HZ);
ifa_flags |= IFA_F_PERMANENT; if (addrconf_finite_timeout(timeout)) {
flags = 0; expires = jiffies_to_clock_t(timeout * HZ);
expires = 0; valid_lft = timeout;
} else {
if (valid_lft >= 0x7FFFFFFF/HZ)
valid_lft = 0x7FFFFFFF/HZ;
flags = RTF_EXPIRES; 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) timeout = addrconf_timeout_fixup(prefered_lft, HZ);
ifa_flags |= IFA_F_DEPRECATED; if (addrconf_finite_timeout(timeout)) {
else if ((prefered_lft >= 0x7FFFFFFF/HZ) && if (timeout == 0)
(prefered_lft != INFINITY_LIFE_TIME)) ifa_flags |= IFA_F_DEPRECATED;
prefered_lft = 0x7FFFFFFF/HZ; prefered_lft = timeout;
}
spin_lock_bh(&ifp->lock); spin_lock_bh(&ifp->lock);
ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
......
...@@ -446,7 +446,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, ...@@ -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 route_info *rinfo = (struct route_info *) opt;
struct in6_addr prefix_buf, *prefix; struct in6_addr prefix_buf, *prefix;
unsigned int pref; unsigned int pref;
u32 lifetime; unsigned long lifetime;
struct rt6_info *rt; struct rt6_info *rt;
if (len < sizeof(struct route_info)) { if (len < sizeof(struct route_info)) {
...@@ -472,13 +472,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, ...@@ -472,13 +472,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
if (pref == ICMPV6_ROUTER_PREF_INVALID) if (pref == ICMPV6_ROUTER_PREF_INVALID)
pref = ICMPV6_ROUTER_PREF_MEDIUM; pref = ICMPV6_ROUTER_PREF_MEDIUM;
lifetime = ntohl(rinfo->lifetime); lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
if (lifetime == 0xffffffff) {
/* infinity */
} else if (lifetime > 0x7fffffff/HZ - 1) {
/* Avoid arithmetic overflow */
lifetime = 0x7fffffff/HZ - 1;
}
if (rinfo->length == 3) if (rinfo->length == 3)
prefix = (struct in6_addr *)rinfo->prefix; prefix = (struct in6_addr *)rinfo->prefix;
...@@ -506,7 +500,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, ...@@ -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); (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
if (rt) { if (rt) {
if (lifetime == 0xffffffff) { if (!addrconf_finite_timeout(lifetime)) {
rt->rt6i_flags &= ~RTF_EXPIRES; rt->rt6i_flags &= ~RTF_EXPIRES;
} else { } else {
rt->rt6i_expires = jiffies + HZ * lifetime; rt->rt6i_expires = jiffies + HZ * lifetime;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment