aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/Makefile2
-rw-r--r--net/bootp.c19
-rw-r--r--net/net.c38
-rw-r--r--net/sntp.c93
-rw-r--r--net/sntp.h61
5 files changed, 211 insertions, 2 deletions
diff --git a/net/Makefile b/net/Makefile
index cf25a0f..7a70489 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -27,7 +27,7 @@ include $(TOPDIR)/config.mk
LIB = libnet.a
-OBJS = net.o tftp.o bootp.o rarp.o eth.o nfs.o
+OBJS = net.o tftp.o bootp.o rarp.o eth.o nfs.o sntp.o
all: $(LIB)
$(LIB): $(START) $(OBJS)
diff --git a/net/bootp.c b/net/bootp.c
index 4bca50d..ee0982e 100644
--- a/net/bootp.c
+++ b/net/bootp.c
@@ -448,6 +448,10 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
*e++ = 1; /* Subnet Mask */
*cnt += 1;
#endif
+#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET)
+ *e++ = 2;
+ *cnt += 1;
+#endif
#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY)
*e++ = 3; /* Router Option */
*cnt += 1;
@@ -472,6 +476,10 @@ static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t R
*e++ = 40; /* NIS Domain name request */
*cnt += 1;
#endif
+#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER)
+ *e++ = 42;
+ *cnt += 1;
+#endif
*e++ = 255; /* End of the list */
/* Pad to minimal length */
@@ -718,6 +726,12 @@ static void DhcpOptionsProcess (uchar * popt)
case 1:
NetCopyIP (&NetOurSubnetMask, (popt + 2));
break;
+#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET)
+ case 2: /* Time offset */
+ NetCopyLong (&NetTimeOffset, (ulong *) (popt + 2));
+ NetTimeOffset = ntohl (NetTimeOffset);
+ break;
+#endif
case 3:
NetCopyIP (&NetOurGatewayIP, (popt + 2));
break;
@@ -741,6 +755,11 @@ static void DhcpOptionsProcess (uchar * popt)
memcpy (&NetOurRootPath, popt + 2, size);
NetOurRootPath[size] = 0;
break;
+#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER)
+ case 42: /* NTP server IP */
+ NetCopyIP (&NetNtpServerIP, (popt + 2));
+ break;
+#endif
case 51:
NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2));
break;
diff --git a/net/net.c b/net/net.c
index d7523fd..11a286b 100644
--- a/net/net.c
+++ b/net/net.c
@@ -64,6 +64,13 @@
* derived from our own IP address)
* We want: - load the boot file
* Next step: none
+ *
+ * SNTP:
+ *
+ * Prerequisites: - own ethernet address
+ * - own IP address
+ * We want: - network time
+ * Next step: none
*/
@@ -79,6 +86,9 @@
#include <status_led.h>
#include <miiphy.h>
#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#include "sntp.h"
+#endif
#if (CONFIG_COMMANDS & CFG_CMD_NET)
@@ -148,6 +158,11 @@ static void PingStart(void);
static void CDPStart(void);
#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+IPaddr_t NetNtpServerIP; /* NTP server IP address */
+int NetTimeOffset=0; /* offset time from UTC */
+#endif
+
#ifdef CONFIG_NETCONSOLE
void NcStart(void);
int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
@@ -313,6 +328,9 @@ restart:
#if (CONFIG_COMMANDS & CFG_CMD_PING)
case PING:
#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+ case SNTP:
+#endif
case NETCONS:
case TFTP:
NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
@@ -334,6 +352,11 @@ restart:
/* nothing */
break;
#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+ case SNTP:
+ /* nothing */
+ break;
+#endif
default:
break;
}
@@ -417,6 +440,11 @@ restart:
NcStart();
break;
#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+ case SNTP:
+ SntpStart();
+ break;
+#endif
default:
break;
}
@@ -1426,6 +1454,14 @@ static int net_check_prereq (proto_t protocol)
}
goto common;
#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+ case SNTP:
+ if (NetNtpServerIP == 0) {
+ puts ("*** ERROR: NTP server address not given\n");
+ return (1);
+ }
+ goto common;
+#endif
#if (CONFIG_COMMANDS & CFG_CMD_NFS)
case NFS:
#endif
@@ -1435,7 +1471,7 @@ static int net_check_prereq (proto_t protocol)
puts ("*** ERROR: `serverip' not set\n");
return (1);
}
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
common:
#endif
diff --git a/net/sntp.c b/net/sntp.c
new file mode 100644
index 0000000..9e11eb4
--- /dev/null
+++ b/net/sntp.c
@@ -0,0 +1,93 @@
+/*
+ * SNTP support driver
+ *
+ * Masami Komiya <mkomiya@sonare.it> 2005
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <net.h>
+#include <rtc.h>
+
+#include "sntp.h"
+
+#if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_SNTP))
+
+#define SNTP_TIMEOUT 10
+
+static int SntpOurPort;
+
+static void
+SntpSend (void)
+{
+ struct sntp_pkt_t pkt;
+ int pktlen = SNTP_PACKET_LEN;
+ int sport;
+
+ debug ("%s\n", __FUNCTION__);
+
+ memset (&pkt, 0, sizeof(pkt));
+
+ pkt.li = NTP_LI_NOLEAP;
+ pkt.vn = NTP_VERSION;
+ pkt.mode = NTP_MODE_CLIENT;
+
+ memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
+
+ SntpOurPort = 10000 + (get_timer(0) % 4096);
+ sport = NTP_SERVICE_PORT;
+
+ NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen);
+}
+
+static void
+SntpTimeout (void)
+{
+ puts ("Timeout\n");
+ NetState = NETLOOP_FAIL;
+ return;
+}
+
+static void
+SntpHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
+{
+ struct sntp_pkt_t rpkt;
+ struct rtc_time tm;
+
+ debug ("%s\n", __FUNCTION__);
+
+ if (dest != SntpOurPort) return;
+
+ memcpy ((unsigned char *)&rpkt, pkt, len);
+
+#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
+to_tm(ntohl(rpkt.transmit_timestamp), &tm);
+printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
+tm.tm_year, tm.tm_mon, tm.tm_mday,
+tm.tm_hour, tm.tm_min, tm.tm_sec);
+ to_tm(ntohl(rpkt.transmit_timestamp) - 2208988800u + NetTimeOffset, &tm);
+#if (CONFIG_COMMANDS & CFG_CMD_DATE)
+ rtc_set (&tm);
+#endif
+ printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
+ tm.tm_year, tm.tm_mon, tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec);
+#endif
+
+ NetState = NETLOOP_SUCCESS;
+}
+
+void
+SntpStart (void)
+{
+ debug ("%s\n", __FUNCTION__);
+
+ NetSetTimeout (SNTP_TIMEOUT * CFG_HZ, SntpTimeout);
+ NetSetHandler(SntpHandler);
+ memset (NetServerEther, 0, 6);
+
+ SntpSend ();
+}
+
+#endif /* CONFIG_COMMANDS & CFG_CMD_SNTP */
diff --git a/net/sntp.h b/net/sntp.h
new file mode 100644
index 0000000..8a097bf
--- /dev/null
+++ b/net/sntp.h
@@ -0,0 +1,61 @@
+/*
+ * (C) Masami Komiya <mkomiya@sonare.it> 2005
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2, or (at
+ * your option) any later version.
+ */
+
+#ifndef __SNTP_H__
+#define __SNTP_H__
+
+#define NTP_SERVICE_PORT 123
+#define SNTP_PACKET_LEN 48
+
+
+/* Leap Indicator */
+#define NTP_LI_NOLEAP 0x0
+#define NTP_LI_61SECS 0x1
+#define NTP_LI_59SECS 0x2
+#define NTP_LI_ALARM 0x3
+
+/* Version */
+
+#define NTP_VERSION 4
+
+/* Mode */
+#define NTP_MODE_RESERVED 0
+#define NTP_MODE_SYMACTIVE 1 /* Symmetric Active */
+#define NTP_MODE_SYMPASSIVE 2 /* Symmetric Passive */
+#define NTP_MODE_CLIENT 3
+#define NTP_MODE_SERVER 4
+#define NTP_MODE_BROADCAST 5
+#define NTP_MODE_NTPCTRL 6 /* Reserved for NTP control message */
+#define NTP_MODE_PRIVATE 7 /* Reserved for private use */
+
+struct sntp_pkt_t {
+#if __LITTLE_ENDIAN
+ uchar mode:3;
+ uchar vn:3;
+ uchar li:2;
+#else
+ uchar li:2;
+ uchar vn:3;
+ uchar mode:3;
+#endif
+ uchar stratum;
+ uchar poll;
+ uchar precision;
+ uint root_delay;
+ uint root_dispersion;
+ uint reference_id;
+ unsigned long long reference_timestamp;
+ unsigned long long originate_timestamp;
+ unsigned long long receive_timestamp;
+ unsigned long long transmit_timestamp;
+};
+
+extern void SntpStart (void); /* Begin SNTP */
+
+#endif /* __SNTP_H__ */