2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1985, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)timed.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
48 #include <sys/ioctl.h>
50 #include "pathnames.h"
52 #include <sys/types.h>
53 #include <sys/times.h>
56 int sock, sock_raw = -1;
58 u_short sequence; /* sequence number */
62 int nslavenets; /* nets were I could be a slave */
63 int nmasternets; /* nets were I could be a master */
64 int nignorednets; /* ignored nets */
65 int nnets; /* nets I am connected to */
67 FILE *fd; /* trace file FD */
71 struct netinfo *nettab = 0;
72 struct netinfo *slavenet;
83 struct hosttbl hosttbl[NHOSTS+1]; /* known hosts */
85 static struct goodhost { /* hosts that we trust */
86 char name[MAXHOSTNAMELEN];
87 struct goodhost *next;
91 static char *goodgroup; /* net group of trusted hosts */
92 static void checkignorednets(void);
93 static void pickslavenet(struct netinfo *);
94 static void add_good_host(char *, int);
95 static void usage(void);
98 * The timedaemons synchronize the clocks of hosts in a local area network.
99 * One daemon runs as master, all the others as slaves. The master
100 * performs the task of computing clock differences and sends correction
101 * values to the slaves.
102 * Slaves start an election to choose a new master when the latter disappears
103 * because of a machine crash, network partition, or when killed.
104 * A resolution protocol is used to kill all but one of the masters
105 * that happen to exist in segments of a partitioned network when the
106 * network partition is fixed.
108 * Authors: Riccardo Gusella & Stefano Zatti
110 * overhauled at Silicon Graphics
120 struct timeval ntime;
121 struct servent *srvp;
122 char buf[BUFSIZ], *cp, *cplim;
124 struct ifreq ifreq, ifreqf, *ifr;
125 register struct netinfo *ntp;
126 struct netinfo *ntip;
127 struct netinfo *savefromnet;
128 struct netent *nentp;
130 struct sockaddr_in server;
144 while ((c = getopt(argc, argv, "Mtdn:i:F:G:P:")) != -1) {
156 errx(1, "-i and -n make no sense together");
165 errx(1, "-i and -n make no sense together");
173 add_good_host(optarg,1);
174 while (optind < argc && argv[optind][0] != '-')
175 add_good_host(argv[optind++], 1);
183 errx(1, "only one net group");
195 /* If we care about which machine is the master, then we must
196 * be willing to be a master
198 if (0 != goodgroup || 0 != goodhosts)
201 if (gethostname(hostname, sizeof(hostname) - 1) < 0)
202 err(1, "gethostname");
210 if (goodhosts != 0) /* trust ourself */
211 add_good_host(hostname,1);
213 srvp = getservbyname("timed", "udp");
215 errx(1, "timed/udp: unknown service");
217 bzero(&server, sizeof(struct sockaddr_in));
218 server.sin_port = srvp->s_port;
219 server.sin_family = AF_INET;
220 sock = socket(AF_INET, SOCK_DGRAM, 0);
223 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
225 err(1, "setsockopt");
226 if (bind(sock, (struct sockaddr*)&server, sizeof(server))) {
227 if (errno == EADDRINUSE)
228 warnx("time daemon already running");
234 /* choose a unique seed for random number generation */
235 (void)gettimeofday(&ntime, 0);
236 srandom(ntime.tv_sec + ntime.tv_usec);
238 sequence = random(); /* initial seq number */
240 /* rounds kernel variable time to multiple of 5 ms. */
242 ntime.tv_usec = -((ntime.tv_usec/1000) % 5) * 1000;
243 (void)adjtime(&ntime, (struct timeval *)0);
245 for (nt = nets; nt; nt = nt->next) {
246 nentp = getnetbyname(nt->name);
248 nt->net = inet_network(nt->name);
249 if (nt->net != INADDR_NONE)
250 nentp = getnetbyaddr(nt->net, AF_INET);
253 nt->net = nentp->n_net;
254 } else if (nt->net == INADDR_NONE) {
255 errx(1, "unknown net %s", nt->name);
256 } else if (nt->net == INADDR_ANY) {
257 errx(1, "bad net %s", nt->name);
259 warnx("warning: %s unknown in /etc/networks",
263 if (0 == (nt->net & 0xff000000))
265 if (0 == (nt->net & 0xff000000))
267 if (0 == (nt->net & 0xff000000))
270 ifc.ifc_len = sizeof(buf);
272 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0)
273 err(1, "get interface configuration");
275 #define size(p) max((p).sa_len, sizeof(p))
276 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
277 for (cp = buf; cp < cplim;
278 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
279 ifr = (struct ifreq *)cp;
280 if (ifr->ifr_addr.sa_family != AF_INET)
283 ntp = (struct netinfo*)malloc(sizeof(struct netinfo));
284 bzero(ntp,sizeof(*ntp));
285 ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
286 ntp->status = NOMASTER;
290 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) {
291 warn("get interface flags");
294 if ((ifreqf.ifr_flags & IFF_UP) == 0)
296 if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 &&
297 (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) {
302 if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
306 ntp->mask = ((struct sockaddr_in *)
307 &ifreq.ifr_addr)->sin_addr.s_addr;
309 if (ifreqf.ifr_flags & IFF_BROADCAST) {
310 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
311 warn("get broadaddr");
314 ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr;
315 /* What if the broadcast address is all ones?
316 * So we cannot just mask ntp->dest_addr. */
317 ntp->net = ntp->my_addr;
318 ntp->net.s_addr &= ntp->mask;
320 if (ioctl(sock, SIOCGIFDSTADDR,
321 (char *)&ifreq) < 0) {
322 warn("get destaddr");
325 ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr;
326 ntp->net = ntp->dest_addr.sin_addr;
329 ntp->dest_addr.sin_port = port;
331 for (nt = nets; nt; nt = nt->next) {
332 if (ntp->net.s_addr == htonl(nt->net))
335 if ((nflag && !nt) || (iflag && nt))
339 if (nettab == NULL) {
348 (void) free((char *)ntp);
350 errx(1, "no network usable");
352 /* microseconds to delay before responding to a broadcast */
353 delay1 = casual(1, 100*1000);
355 /* election timer delay in secs. */
356 delay2 = casual(MINTOUT, MAXTOUT);
363 openlog("timed", LOG_CONS|LOG_PID, LOG_DAEMON);
366 * keep returning here
368 ret = setjmp(jmpenv);
369 savefromnet = fromnet;
380 /* Just lost our master */
382 slavenet->status = election(slavenet);
383 if (!slavenet || slavenet->status == MASTER) {
387 makeslave(slavenet); /* prune extras */
392 /* Just been told to quit */
394 pickslavenet(savefromnet);
399 if (!(status & MASTER) && sock_raw != -1) {
400 /* sock_raw is not being used now */
401 (void)close(sock_raw);
405 if (status == MASTER)
411 if (sock_raw != -1) {
412 (void)close(sock_raw);
417 /* we just lost our master or were told to quit */
420 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
421 if (ntp->status == MASTER)
423 ntp->status = NOMASTER;
440 "usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...] [-G netgp]\n");
443 "usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...]\n");
449 * suppress an upstart, untrustworthy, self-appointed master
452 suppress(addr, name,net)
453 struct sockaddr_in *addr;
457 struct sockaddr_in tgt;
458 char tname[MAXHOSTNAMELEN];
460 static struct timeval wait;
463 fprintf(fd, "suppress: %s\n", name);
465 (void)strcpy(tname, name);
467 while (0 != readmsg(TSP_ANY, ANYADDR, &wait, net)) {
469 fprintf(fd, "suppress:\tdiscarded packet from %s\n",
473 syslog(LOG_NOTICE, "suppressing false master %s", tname);
474 msg.tsp_type = TSP_QUIT;
475 (void)strcpy(msg.tsp_name, hostname);
476 (void)acksend(&msg, &tgt, tname, TSP_ACK, 0, 1);
483 struct tsp resp, conflict, *answer;
484 struct timeval ntime;
485 char mastername[MAXHOSTNAMELEN];
486 struct sockaddr_in masteraddr;
491 /* look for master */
492 resp.tsp_type = TSP_MASTERREQ;
493 (void)strcpy(resp.tsp_name, hostname);
494 answer = acksend(&resp, &ntp->dest_addr, ANYADDR,
495 TSP_MASTERACK, ntp, 0);
496 if (answer != 0 && !good_host_name(answer->tsp_name)) {
497 suppress(&from, answer->tsp_name, ntp);
498 ntp->status = NOMASTER;
503 * Various conditions can cause conflict: races between
504 * two just started timedaemons when no master is
505 * present, or timedaemons started during an election.
506 * A conservative approach is taken. Give up and became a
507 * slave, postponing election of a master until first
510 ntime.tv_sec = ntime.tv_usec = 0;
511 answer = readmsg(TSP_MASTERREQ, ANYADDR, &ntime, ntp);
513 if (!good_host_name(answer->tsp_name)) {
514 suppress(&from, answer->tsp_name, ntp);
515 ntp->status = NOMASTER;
520 ntime.tv_sec = ntime.tv_usec = 0;
521 answer = readmsg(TSP_MASTERUP, ANYADDR, &ntime, ntp);
523 if (!good_host_name(answer->tsp_name)) {
524 suppress(&from, answer->tsp_name, ntp);
525 ntp->status = NOMASTER;
530 ntime.tv_sec = ntime.tv_usec = 0;
531 answer = readmsg(TSP_ELECTION, ANYADDR, &ntime, ntp);
533 if (!good_host_name(answer->tsp_name)) {
534 suppress(&from, answer->tsp_name, ntp);
535 ntp->status = NOMASTER;
541 ntp->status = MASTER;
543 ntp->status = NOMASTER;
548 (void)strcpy(mastername, answer->tsp_name);
552 * If network has been partitioned, there might be other
553 * masters; tell the one we have just acknowledged that
554 * it has to gain control over the others.
557 ntime.tv_usec = 300000;
558 answer = readmsg(TSP_MASTERACK, ANYADDR, &ntime, ntp);
560 * checking also not to send CONFLICT to ack'ed master
561 * due to duplicated MASTERACKs
563 if (answer != NULL &&
564 strcmp(answer->tsp_name, mastername) != 0) {
565 conflict.tsp_type = TSP_CONFLICT;
566 (void)strcpy(conflict.tsp_name, hostname);
567 if (!acksend(&conflict, &masteraddr, mastername,
570 "error on sending TSP_CONFLICT");
576 * based on the current network configuration, set the status, and count
585 nmasternets = nslavenets = nnets = nignorednets = 0;
587 fprintf(fd, "Net status:\n");
588 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
589 switch ((int)ntp->status) {
602 fprintf(fd, "\t%-16s", inet_ntoa(ntp->net));
603 switch ((int)ntp->status) {
605 fprintf(fd, "NOMASTER\n");
608 fprintf(fd, "MASTER\n");
611 fprintf(fd, "SLAVE\n");
614 fprintf(fd, "IGNORE\n");
617 fprintf(fd, "invalid state %d\n",
623 status |= ntp->status;
628 "\tnets=%d masters=%d slaves=%d ignored=%d delay2=%ld\n",
629 nnets, nmasternets, nslavenets, nignorednets, delay2);
636 register struct netinfo *ntp;
638 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
639 if (ntp->status == SLAVE && ntp != net)
640 ntp->status = IGNORE;
646 * Try to become master over ignored nets..
651 register struct netinfo *ntp;
653 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
654 if (!Mflag && ntp->status == SLAVE)
657 if (ntp->status == IGNORE || ntp->status == NOMASTER) {
659 if (!Mflag && ntp->status == SLAVE)
666 * choose a good network on which to be a slave
667 * The ignored networks must have already been checked.
668 * Take a hint about for a good network.
674 if (slavenet != 0 && slavenet->status == SLAVE) {
675 makeslave(slavenet); /* prune extras */
679 if (ntp == 0 || ntp->status != SLAVE) {
680 for (ntp = nettab; ntp != 0; ntp = ntp->next) {
681 if (ntp->status == SLAVE)
689 * returns a random number in the range [inf, sup]
697 value = ((double)(random() & 0x7fffffff)) / (0x7fffffff*1.0);
698 return(inf + (sup - inf)*value);
707 (void)gettimeofday(&tv, (struct timezone *)0);
709 return (ctime(&tv_sec));
716 register struct nets **netlist = &nets;
719 netlist = &((*netlist)->next);
720 *netlist = (struct nets *)malloc(sizeof **netlist);
722 errx(1, "malloc failed");
723 bzero((char *)*netlist, sizeof(**netlist));
724 (*netlist)->name = name;
727 /* note a host as trustworthy */
729 add_good_host(name, perm)
731 int perm; /* 1=not part of the netgroup */
733 register struct goodhost *ghp;
734 register struct hostent *hentp;
736 ghp = (struct goodhost*)malloc(sizeof(*ghp));
738 syslog(LOG_ERR, "malloc failed");
742 bzero((char*)ghp, sizeof(*ghp));
743 (void)strncpy(&ghp->name[0], name, sizeof(ghp->name));
744 ghp->next = goodhosts;
748 hentp = gethostbyname(name);
749 if (0 == hentp && perm)
750 warnx("unknown host %s", name);
754 /* update our image of the net-group of trustworthy hosts
760 # define NG_DELAY (30*60*CLK_TCK) /* 30 minutes */
761 static unsigned long last_update = -NG_DELAY;
762 unsigned long new_update;
763 struct goodhost *ghp, **ghpp;
766 char *mach, *usr, *dom;
771 /* if no netgroup, then we are finished */
772 if (goodgroup == 0 || !Mflag)
775 /* Do not chatter with the netgroup master too often.
777 new_update = times(&tm);
778 if (new_update < last_update + NG_DELAY
781 last_update = new_update;
783 /* forget the old temporary entries */
785 while (0 != (ghp = *ghpp)) {
795 /* quit now if we are not one of the trusted masters
797 if (!innetgr(goodgroup, &hostname[0], 0,0)) {
799 (void)fprintf(fd, "get_goodgroup: %s not in %s\n",
800 &hostname[0], goodgroup);
804 (void)fprintf(fd, "get_goodgroup: %s in %s\n",
805 &hostname[0], goodgroup);
807 /* mark the entire netgroup as trusted */
808 (void)setnetgrent(goodgroup);
809 while (getnetgrent(&mach,&usr,&dom)) {
811 add_good_host(mach,0);
815 /* update list of slaves */
816 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
817 htp->good = good_host_name(&htp->name[0]);
823 /* see if a machine is trustworthy
825 int /* 1=trust hp to change our date */
829 register struct goodhost *ghp = goodhosts;
832 if (!ghp || !Mflag) /* trust everyone if no one named */
837 if (c == ghp->name[0]
838 && !strcasecmp(name, ghp->name))
839 return 1; /* found him, so say so */
840 } while (0 != (ghp = ghp->next));
842 if (!strcasecmp(name,hostname)) /* trust ourself */
845 return 0; /* did not find him */