xref: /netbsd-src/external/bsd/tcpdump/dist/print-timed.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*
2  * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-timed.c,v 1.7 2020/02/24 18:39:47 kamil Exp $");
25 #endif
26 
27 /* \summary: BSD time daemon protocol printer */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <netdissect-stdinc.h>
34 
35 #include "netdissect.h"
36 #include "extract.h"
37 
38 /*
39  * Time Synchronization Protocol
40  *
41  * http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf
42  */
43 
44 struct tsp_timeval {
45 	uint32_t	tv_sec;
46 	uint32_t	tv_usec;
47 };
48 
49 struct tsp {
50 	uint8_t		tsp_type;
51 	uint8_t		tsp_vers;
52 	uint16_t	tsp_seq;
53 	union {
54 		struct tsp_timeval tspu_time;
55 		int8_t tspu_hopcnt;
56 	} tsp_u;
57 	int8_t tsp_name[256];
58 };
59 
60 #define	tsp_time	tsp_u.tspu_time
61 #define	tsp_hopcnt	tsp_u.tspu_hopcnt
62 
63 /*
64  * Command types.
65  */
66 #define	TSP_ANY			0	/* match any types */
67 #define	TSP_ADJTIME		1	/* send adjtime */
68 #define	TSP_ACK			2	/* generic acknowledgement */
69 #define	TSP_MASTERREQ		3	/* ask for master's name */
70 #define	TSP_MASTERACK		4	/* acknowledge master request */
71 #define	TSP_SETTIME		5	/* send network time */
72 #define	TSP_MASTERUP		6	/* inform slaves that master is up */
73 #define	TSP_SLAVEUP		7	/* slave is up but not polled */
74 #define	TSP_ELECTION		8	/* advance candidature for master */
75 #define	TSP_ACCEPT		9	/* support candidature of master */
76 #define	TSP_REFUSE		10	/* reject candidature of master */
77 #define	TSP_CONFLICT		11	/* two or more masters present */
78 #define	TSP_RESOLVE		12	/* masters' conflict resolution */
79 #define	TSP_QUIT		13	/* reject candidature if master is up */
80 #define	TSP_DATE		14	/* reset the time (date command) */
81 #define	TSP_DATEREQ		15	/* remote request to reset the time */
82 #define	TSP_DATEACK		16	/* acknowledge time setting  */
83 #define	TSP_TRACEON		17	/* turn tracing on */
84 #define	TSP_TRACEOFF		18	/* turn tracing off */
85 #define	TSP_MSITE		19	/* find out master's site */
86 #define	TSP_MSITEREQ		20	/* remote master's site request */
87 #define	TSP_TEST		21	/* for testing election algo */
88 #define	TSP_SETDATE		22	/* New from date command */
89 #define	TSP_SETDATEREQ		23	/* New remote for above */
90 #define	TSP_LOOP		24	/* loop detection packet */
91 
92 #define	TSPTYPENUMBER		25
93 
94 static const char tstr[] = "[|timed]";
95 
96 static const char *tsptype[TSPTYPENUMBER] =
97   { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
98   "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
99   "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
100   "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
101 
102 UNALIGNED_OK
103 void
104 timed_print(netdissect_options *ndo,
105             register const u_char *bp)
106 {
107 	const struct tsp *tsp = (const struct tsp *)bp;
108 	long sec, usec;
109 
110 	ND_TCHECK(tsp->tsp_type);
111 	if (tsp->tsp_type < TSPTYPENUMBER)
112 		ND_PRINT((ndo, "TSP_%s", tsptype[tsp->tsp_type]));
113 	else
114 		ND_PRINT((ndo, "(tsp_type %#x)", tsp->tsp_type));
115 
116 	ND_TCHECK(tsp->tsp_vers);
117 	ND_PRINT((ndo, " vers %u", tsp->tsp_vers));
118 
119 	ND_TCHECK(tsp->tsp_seq);
120 	ND_PRINT((ndo, " seq %u", tsp->tsp_seq));
121 
122 	switch (tsp->tsp_type) {
123 	case TSP_LOOP:
124 		ND_TCHECK(tsp->tsp_hopcnt);
125 		ND_PRINT((ndo, " hopcnt %u", tsp->tsp_hopcnt));
126 		break;
127 	case TSP_SETTIME:
128 	case TSP_ADJTIME:
129 	case TSP_SETDATE:
130 	case TSP_SETDATEREQ:
131 		ND_TCHECK(tsp->tsp_time);
132 		sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec);
133 		usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec);
134 		/* XXX The comparison below is always false? */
135 		if (usec < 0)
136 			/* invalid, skip the rest of the packet */
137 			return;
138 		ND_PRINT((ndo, " time "));
139 		if (sec < 0 && usec != 0) {
140 			sec++;
141 			if (sec == 0)
142 				ND_PRINT((ndo, "-"));
143 			usec = 1000000 - usec;
144 		}
145 		ND_PRINT((ndo, "%ld.%06ld", sec, usec));
146 		break;
147 	}
148 	ND_TCHECK(tsp->tsp_name);
149 	ND_PRINT((ndo, " name "));
150 	if (fn_print(ndo, (const u_char *)tsp->tsp_name, (const u_char *)tsp->tsp_name + sizeof(tsp->tsp_name)))
151 		goto trunc;
152 	return;
153 
154 trunc:
155 	ND_PRINT((ndo, " %s", tstr));
156 }
157