blob: 5830bc7be07781c3e83fc603395a8e6df64d850e [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 Hughes892a68b2015-10-19 14:43:53 -070022#define NETDISSECT_REWORKED
The Android Open Source Project2949f582009-03-03 19:30:46 -080023#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <tcpdump-stdinc.h>
28
The Android Open Source Project2949f582009-03-03 19:30:46 -080029#include "interface.h"
30#include "extract.h"
31
Elliott Hughes892a68b2015-10-19 14:43:53 -070032/*
33 * Time Synchronization Protocol
34 */
35
36struct tsp_timeval {
37 uint32_t tv_sec;
38 uint32_t tv_usec;
39};
40
41struct tsp {
42 uint8_t tsp_type;
43 uint8_t tsp_vers;
44 uint16_t tsp_seq;
45 union {
46 struct tsp_timeval tspu_time;
47 int8_t tspu_hopcnt;
48 } tsp_u;
49 int8_t tsp_name[256];
50};
51
52#define tsp_time tsp_u.tspu_time
53#define tsp_hopcnt tsp_u.tspu_hopcnt
54
55/*
56 * Command types.
57 */
58#define TSP_ANY 0 /* match any types */
59#define TSP_ADJTIME 1 /* send adjtime */
60#define TSP_ACK 2 /* generic acknowledgement */
61#define TSP_MASTERREQ 3 /* ask for master's name */
62#define TSP_MASTERACK 4 /* acknowledge master request */
63#define TSP_SETTIME 5 /* send network time */
64#define TSP_MASTERUP 6 /* inform slaves that master is up */
65#define TSP_SLAVEUP 7 /* slave is up but not polled */
66#define TSP_ELECTION 8 /* advance candidature for master */
67#define TSP_ACCEPT 9 /* support candidature of master */
68#define TSP_REFUSE 10 /* reject candidature of master */
69#define TSP_CONFLICT 11 /* two or more masters present */
70#define TSP_RESOLVE 12 /* masters' conflict resolution */
71#define TSP_QUIT 13 /* reject candidature if master is up */
72#define TSP_DATE 14 /* reset the time (date command) */
73#define TSP_DATEREQ 15 /* remote request to reset the time */
74#define TSP_DATEACK 16 /* acknowledge time setting */
75#define TSP_TRACEON 17 /* turn tracing on */
76#define TSP_TRACEOFF 18 /* turn tracing off */
77#define TSP_MSITE 19 /* find out master's site */
78#define TSP_MSITEREQ 20 /* remote master's site request */
79#define TSP_TEST 21 /* for testing election algo */
80#define TSP_SETDATE 22 /* New from date command */
81#define TSP_SETDATEREQ 23 /* New remote for above */
82#define TSP_LOOP 24 /* loop detection packet */
83
84#define TSPTYPENUMBER 25
85
86static const char tstr[] = "[|timed]";
87
The Android Open Source Project2949f582009-03-03 19:30:46 -080088static const char *tsptype[TSPTYPENUMBER] =
89 { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
90 "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
91 "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
92 "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
93
94void
Elliott Hughes892a68b2015-10-19 14:43:53 -070095timed_print(netdissect_options *ndo,
96 register const u_char *bp)
The Android Open Source Project2949f582009-03-03 19:30:46 -080097{
The Android Open Source Project2949f582009-03-03 19:30:46 -080098 struct tsp *tsp = (struct tsp *)bp;
99 long sec, usec;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800100
Elliott Hughes892a68b2015-10-19 14:43:53 -0700101 ND_TCHECK(tsp->tsp_type);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800102 if (tsp->tsp_type < TSPTYPENUMBER)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700103 ND_PRINT((ndo, "TSP_%s", tsptype[tsp->tsp_type]));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800104 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700105 ND_PRINT((ndo, "(tsp_type %#x)", tsp->tsp_type));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800106
Elliott Hughes892a68b2015-10-19 14:43:53 -0700107 ND_TCHECK(tsp->tsp_vers);
108 ND_PRINT((ndo, " vers %u", tsp->tsp_vers));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800109
Elliott Hughes892a68b2015-10-19 14:43:53 -0700110 ND_TCHECK(tsp->tsp_seq);
111 ND_PRINT((ndo, " seq %u", tsp->tsp_seq));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800112
Elliott Hughes892a68b2015-10-19 14:43:53 -0700113 switch (tsp->tsp_type) {
114 case TSP_LOOP:
115 ND_TCHECK(tsp->tsp_hopcnt);
116 ND_PRINT((ndo, " hopcnt %u", tsp->tsp_hopcnt));
117 break;
118 case TSP_SETTIME:
119 case TSP_ADJTIME:
120 case TSP_SETDATE:
121 case TSP_SETDATEREQ:
122 ND_TCHECK(tsp->tsp_time);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800123 sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec);
124 usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700125 /* XXX The comparison below is always false? */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126 if (usec < 0)
127 /* corrupt, skip the rest of the packet */
128 return;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700129 ND_PRINT((ndo, " time "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800130 if (sec < 0 && usec != 0) {
131 sec++;
132 if (sec == 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700133 ND_PRINT((ndo, "-"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800134 usec = 1000000 - usec;
135 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700136 ND_PRINT((ndo, "%ld.%06ld", sec, usec));
137 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800138 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700139 ND_TCHECK(tsp->tsp_name);
140 ND_PRINT((ndo, " name "));
141 if (fn_print(ndo, (u_char *)tsp->tsp_name, (u_char *)tsp->tsp_name + sizeof(tsp->tsp_name)))
142 goto trunc;
143 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800144
Elliott Hughes892a68b2015-10-19 14:43:53 -0700145trunc:
146 ND_PRINT((ndo, " %s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800147}