net: ipv4: Convert IP network timestamps to be y2038 safe
authorDeepa Dinamani <deepa.kernel@gmail.com>
Sat, 27 Feb 2016 08:32:15 +0000 (00:32 -0800)
committerDavid S. Miller <davem@davemloft.net>
Tue, 1 Mar 2016 22:18:44 +0000 (17:18 -0500)
ICMP timestamp messages and IP source route options require
timestamps to be in milliseconds modulo 24 hours from
midnight UT format.

Add inet_current_timestamp() function to support this. The function
returns the required timestamp in network byte order.

Timestamp calculation is also changed to call ktime_get_real_ts64()
which uses struct timespec64. struct timespec64 is y2038 safe.
Previously it called getnstimeofday() which uses struct timespec.
struct timespec is not y2038 safe.

Signed-off-by: Deepa Dinamani <deepa.kernel@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Cc: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
Cc: James Morris <jmorris@namei.org>
Cc: Patrick McHardy <kaber@trash.net>
Acked-by: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/ip.h
net/ipv4/af_inet.c
net/ipv4/icmp.c
net/ipv4/ip_options.c

index cbb134b2f0e4ff2ae81aa6e45a351a2246605786..fad74d323bd6b324ef24ff7ff0ce03140c58e1dd 100644 (file)
@@ -240,6 +240,8 @@ static inline int inet_is_local_reserved_port(struct net *net, int port)
 }
 #endif
 
+__be32 inet_current_timestamp(void);
+
 /* From inetpeer.c */
 extern int inet_peer_threshold;
 extern int inet_peer_minttl;
index 209d1ed2895444bfd90d2eb12f6942d984be40d5..0cc923f83e107b429521ef809a5a308c06680e7e 100644 (file)
@@ -1380,6 +1380,32 @@ out:
        return pp;
 }
 
+#define SECONDS_PER_DAY        86400
+
+/* inet_current_timestamp - Return IP network timestamp
+ *
+ * Return milliseconds since midnight in network byte order.
+ */
+__be32 inet_current_timestamp(void)
+{
+       u32 secs;
+       u32 msecs;
+       struct timespec64 ts;
+
+       ktime_get_real_ts64(&ts);
+
+       /* Get secs since midnight. */
+       (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
+       /* Convert to msecs. */
+       msecs = secs * MSEC_PER_SEC;
+       /* Convert nsec to msec. */
+       msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
+
+       /* Convert to network byte order. */
+       return htons(msecs);
+}
+EXPORT_SYMBOL(inet_current_timestamp);
+
 int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
 {
        if (sk->sk_family == AF_INET)
index 36e26977c9088c1dbd09cd13e9a5e2c43369fe31..6333489771ed07bb2509500a2e48fed05643281a 100644 (file)
@@ -931,7 +931,6 @@ static bool icmp_echo(struct sk_buff *skb)
  */
 static bool icmp_timestamp(struct sk_buff *skb)
 {
-       struct timespec tv;
        struct icmp_bxm icmp_param;
        /*
         *      Too short.
@@ -942,9 +941,7 @@ static bool icmp_timestamp(struct sk_buff *skb)
        /*
         *      Fill in the current time as ms since midnight UT:
         */
-       getnstimeofday(&tv);
-       icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
-                                        tv.tv_nsec / NSEC_PER_MSEC);
+       icmp_param.data.times[1] = inet_current_timestamp();
        icmp_param.data.times[2] = icmp_param.data.times[1];
        if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
                BUG();
index bd246792360b4b8dcda2c13328ea5f01bb603e06..4d158ff1def1a8fae218b268c7e66224eed95adf 100644 (file)
@@ -58,10 +58,9 @@ void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
                if (opt->ts_needaddr)
                        ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, skb, rt);
                if (opt->ts_needtime) {
-                       struct timespec tv;
                        __be32 midtime;
-                       getnstimeofday(&tv);
-                       midtime = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC);
+
+                       midtime = inet_current_timestamp();
                        memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
                }
                return;
@@ -415,11 +414,10 @@ int ip_options_compile(struct net *net,
                                        break;
                                }
                                if (timeptr) {
-                                       struct timespec tv;
-                                       u32  midtime;
-                                       getnstimeofday(&tv);
-                                       midtime = (tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC;
-                                       put_unaligned_be32(midtime, timeptr);
+                                       __be32 midtime;
+
+                                       midtime = inet_current_timestamp();
+                                       memcpy(timeptr, &midtime, 4);
                                        opt->is_changed = 1;
                                }
                        } else if ((optptr[3]&0xF) != IPOPT_TS_PRESPEC) {