blob: ebd0ac8adca69bc247cf385e4d1e41d1bf144e00 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
Elliott Hughes820eced2021-08-20 18:00:50 -070022/* \summary: Berkeley UNIX Time Synchronization Protocol */
23
24/* specification: https://docs.freebsd.org/44doc/smm/12.timed/paper.pdf */
Elliott Hughese2e3bd12017-05-15 10:59:29 -070025
The Android Open Source Project2949f582009-03-03 19:30:46 -080026#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070027#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080028#endif
29
Elliott Hughes820eced2021-08-20 18:00:50 -070030#include "netdissect-stdinc.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080031
Elliott Hughese2e3bd12017-05-15 10:59:29 -070032#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080033#include "extract.h"
34
Elliott Hughes892a68b2015-10-19 14:43:53 -070035struct tsp_timeval {
Elliott Hughes820eced2021-08-20 18:00:50 -070036 nd_int32_t tv_sec;
37 nd_int32_t tv_usec;
Elliott Hughes892a68b2015-10-19 14:43:53 -070038};
39
40struct tsp {
Elliott Hughes820eced2021-08-20 18:00:50 -070041 nd_uint8_t tsp_type;
42 nd_uint8_t tsp_vers;
43 nd_uint16_t tsp_seq;
Elliott Hughes892a68b2015-10-19 14:43:53 -070044 union {
45 struct tsp_timeval tspu_time;
Elliott Hughes820eced2021-08-20 18:00:50 -070046 nd_int8_t tspu_hopcnt;
Elliott Hughes892a68b2015-10-19 14:43:53 -070047 } tsp_u;
Elliott Hughes820eced2021-08-20 18:00:50 -070048 nd_byte tsp_name[256]; /* null-terminated string up to 256 */
Elliott Hughes892a68b2015-10-19 14:43:53 -070049};
50
51#define tsp_time tsp_u.tspu_time
52#define tsp_hopcnt tsp_u.tspu_hopcnt
53
54/*
55 * Command types.
56 */
57#define TSP_ANY 0 /* match any types */
58#define TSP_ADJTIME 1 /* send adjtime */
59#define TSP_ACK 2 /* generic acknowledgement */
60#define TSP_MASTERREQ 3 /* ask for master's name */
61#define TSP_MASTERACK 4 /* acknowledge master request */
62#define TSP_SETTIME 5 /* send network time */
63#define TSP_MASTERUP 6 /* inform slaves that master is up */
64#define TSP_SLAVEUP 7 /* slave is up but not polled */
65#define TSP_ELECTION 8 /* advance candidature for master */
66#define TSP_ACCEPT 9 /* support candidature of master */
67#define TSP_REFUSE 10 /* reject candidature of master */
68#define TSP_CONFLICT 11 /* two or more masters present */
69#define TSP_RESOLVE 12 /* masters' conflict resolution */
70#define TSP_QUIT 13 /* reject candidature if master is up */
71#define TSP_DATE 14 /* reset the time (date command) */
72#define TSP_DATEREQ 15 /* remote request to reset the time */
73#define TSP_DATEACK 16 /* acknowledge time setting */
74#define TSP_TRACEON 17 /* turn tracing on */
75#define TSP_TRACEOFF 18 /* turn tracing off */
76#define TSP_MSITE 19 /* find out master's site */
77#define TSP_MSITEREQ 20 /* remote master's site request */
78#define TSP_TEST 21 /* for testing election algo */
79#define TSP_SETDATE 22 /* New from date command */
80#define TSP_SETDATEREQ 23 /* New remote for above */
81#define TSP_LOOP 24 /* loop detection packet */
Elliott Hughes820eced2021-08-20 18:00:50 -070082static const struct tok tsptype_str[] = {
83 { TSP_ANY, "TSP_ANY" },
84 { TSP_ADJTIME, "TSP_ADJTIME" },
85 { TSP_ACK, "TSP_ACK" },
86 { TSP_MASTERREQ, "TSP_MASTERREQ" },
87 { TSP_MASTERACK, "TSP_MASTERACK" },
88 { TSP_SETTIME, "TSP_SETTIME" },
89 { TSP_MASTERUP, "TSP_MASTERUP" },
90 { TSP_SLAVEUP, "TSP_SLAVEUP" },
91 { TSP_ELECTION, "TSP_ELECTION" },
92 { TSP_ACCEPT, "TSP_ACCEPT" },
93 { TSP_REFUSE, "TSP_REFUSE" },
94 { TSP_CONFLICT, "TSP_CONFLICT" },
95 { TSP_RESOLVE, "TSP_RESOLVE" },
96 { TSP_QUIT, "TSP_QUIT" },
97 { TSP_DATE, "TSP_DATE" },
98 { TSP_DATEREQ, "TSP_DATEREQ" },
99 { TSP_DATEACK, "TSP_DATEACK" },
100 { TSP_TRACEON, "TSP_TRACEON" },
101 { TSP_TRACEOFF, "TSP_TRACEOFF" },
102 { TSP_MSITE, "TSP_MSITE" },
103 { TSP_MSITEREQ, "TSP_MSITEREQ" },
104 { TSP_TEST, "TSP_TEST" },
105 { TSP_SETDATE, "TSP_SETDATE" },
106 { TSP_SETDATEREQ, "TSP_SETDATEREQ" },
107 { TSP_LOOP, "TSP_LOOP" },
108 { 0, NULL }
109};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800110
111void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700112timed_print(netdissect_options *ndo,
Elliott Hughes820eced2021-08-20 18:00:50 -0700113 const u_char *bp)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800114{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700115 const struct tsp *tsp = (const struct tsp *)bp;
Elliott Hughes820eced2021-08-20 18:00:50 -0700116 uint8_t tsp_type;
117 int sec, usec;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800118
Elliott Hughes820eced2021-08-20 18:00:50 -0700119 ndo->ndo_protocol = "timed";
120 tsp_type = GET_U_1(tsp->tsp_type);
121 ND_PRINT("%s", tok2str(tsptype_str, "(tsp_type %#x)", tsp_type));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800122
Elliott Hughes820eced2021-08-20 18:00:50 -0700123 ND_PRINT(" vers %u", GET_U_1(tsp->tsp_vers));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800124
Elliott Hughes820eced2021-08-20 18:00:50 -0700125 ND_PRINT(" seq %u", GET_BE_U_2(tsp->tsp_seq));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126
Elliott Hughes820eced2021-08-20 18:00:50 -0700127 switch (tsp_type) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700128 case TSP_LOOP:
Elliott Hughes820eced2021-08-20 18:00:50 -0700129 ND_PRINT(" hopcnt %u", GET_U_1(tsp->tsp_hopcnt));
Elliott Hughes892a68b2015-10-19 14:43:53 -0700130 break;
131 case TSP_SETTIME:
132 case TSP_ADJTIME:
133 case TSP_SETDATE:
134 case TSP_SETDATEREQ:
Elliott Hughes820eced2021-08-20 18:00:50 -0700135 sec = GET_BE_S_4(tsp->tsp_time.tv_sec);
136 usec = GET_BE_S_4(tsp->tsp_time.tv_usec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700137 /* XXX The comparison below is always false? */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800138 if (usec < 0)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700139 /* invalid, skip the rest of the packet */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800140 return;
Elliott Hughes820eced2021-08-20 18:00:50 -0700141 ND_PRINT(" time ");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800142 if (sec < 0 && usec != 0) {
143 sec++;
144 if (sec == 0)
Elliott Hughes820eced2021-08-20 18:00:50 -0700145 ND_PRINT("-");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800146 usec = 1000000 - usec;
147 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700148 ND_PRINT("%d.%06d", sec, usec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700149 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800150 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700151 ND_PRINT(" name ");
152 nd_printjnp(ndo, tsp->tsp_name, sizeof(tsp->tsp_name));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800153}