The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 22 | /* \summary: BSD time daemon protocol printer */ |
| 23 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 28 | #include <netdissect-stdinc.h> |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 30 | #include "netdissect.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 31 | #include "extract.h" |
| 32 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Time Synchronization Protocol |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 35 | * |
| 36 | * http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | struct tsp_timeval { |
| 40 | uint32_t tv_sec; |
| 41 | uint32_t tv_usec; |
| 42 | }; |
| 43 | |
| 44 | struct tsp { |
| 45 | uint8_t tsp_type; |
| 46 | uint8_t tsp_vers; |
| 47 | uint16_t tsp_seq; |
| 48 | union { |
| 49 | struct tsp_timeval tspu_time; |
| 50 | int8_t tspu_hopcnt; |
| 51 | } tsp_u; |
| 52 | int8_t tsp_name[256]; |
| 53 | }; |
| 54 | |
| 55 | #define tsp_time tsp_u.tspu_time |
| 56 | #define tsp_hopcnt tsp_u.tspu_hopcnt |
| 57 | |
| 58 | /* |
| 59 | * Command types. |
| 60 | */ |
| 61 | #define TSP_ANY 0 /* match any types */ |
| 62 | #define TSP_ADJTIME 1 /* send adjtime */ |
| 63 | #define TSP_ACK 2 /* generic acknowledgement */ |
| 64 | #define TSP_MASTERREQ 3 /* ask for master's name */ |
| 65 | #define TSP_MASTERACK 4 /* acknowledge master request */ |
| 66 | #define TSP_SETTIME 5 /* send network time */ |
| 67 | #define TSP_MASTERUP 6 /* inform slaves that master is up */ |
| 68 | #define TSP_SLAVEUP 7 /* slave is up but not polled */ |
| 69 | #define TSP_ELECTION 8 /* advance candidature for master */ |
| 70 | #define TSP_ACCEPT 9 /* support candidature of master */ |
| 71 | #define TSP_REFUSE 10 /* reject candidature of master */ |
| 72 | #define TSP_CONFLICT 11 /* two or more masters present */ |
| 73 | #define TSP_RESOLVE 12 /* masters' conflict resolution */ |
| 74 | #define TSP_QUIT 13 /* reject candidature if master is up */ |
| 75 | #define TSP_DATE 14 /* reset the time (date command) */ |
| 76 | #define TSP_DATEREQ 15 /* remote request to reset the time */ |
| 77 | #define TSP_DATEACK 16 /* acknowledge time setting */ |
| 78 | #define TSP_TRACEON 17 /* turn tracing on */ |
| 79 | #define TSP_TRACEOFF 18 /* turn tracing off */ |
| 80 | #define TSP_MSITE 19 /* find out master's site */ |
| 81 | #define TSP_MSITEREQ 20 /* remote master's site request */ |
| 82 | #define TSP_TEST 21 /* for testing election algo */ |
| 83 | #define TSP_SETDATE 22 /* New from date command */ |
| 84 | #define TSP_SETDATEREQ 23 /* New remote for above */ |
| 85 | #define TSP_LOOP 24 /* loop detection packet */ |
| 86 | |
| 87 | #define TSPTYPENUMBER 25 |
| 88 | |
| 89 | static const char tstr[] = "[|timed]"; |
| 90 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 91 | static const char *tsptype[TSPTYPENUMBER] = |
| 92 | { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP", |
| 93 | "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT", |
| 94 | "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ", |
| 95 | "TEST", "SETDATE", "SETDATEREQ", "LOOP" }; |
| 96 | |
| 97 | void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 98 | timed_print(netdissect_options *ndo, |
| 99 | register const u_char *bp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 100 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 101 | const struct tsp *tsp = (const struct tsp *)bp; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 102 | long sec, usec; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 103 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 104 | ND_TCHECK(tsp->tsp_type); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 105 | if (tsp->tsp_type < TSPTYPENUMBER) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 106 | ND_PRINT((ndo, "TSP_%s", tsptype[tsp->tsp_type])); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 107 | else |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 108 | ND_PRINT((ndo, "(tsp_type %#x)", tsp->tsp_type)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 109 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 110 | ND_TCHECK(tsp->tsp_vers); |
| 111 | ND_PRINT((ndo, " vers %u", tsp->tsp_vers)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 112 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 113 | ND_TCHECK(tsp->tsp_seq); |
| 114 | ND_PRINT((ndo, " seq %u", tsp->tsp_seq)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 115 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 116 | switch (tsp->tsp_type) { |
| 117 | case TSP_LOOP: |
| 118 | ND_TCHECK(tsp->tsp_hopcnt); |
| 119 | ND_PRINT((ndo, " hopcnt %u", tsp->tsp_hopcnt)); |
| 120 | break; |
| 121 | case TSP_SETTIME: |
| 122 | case TSP_ADJTIME: |
| 123 | case TSP_SETDATE: |
| 124 | case TSP_SETDATEREQ: |
| 125 | ND_TCHECK(tsp->tsp_time); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 126 | sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec); |
| 127 | usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 128 | /* XXX The comparison below is always false? */ |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 129 | if (usec < 0) |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 130 | /* invalid, skip the rest of the packet */ |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 131 | return; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 132 | ND_PRINT((ndo, " time ")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 133 | if (sec < 0 && usec != 0) { |
| 134 | sec++; |
| 135 | if (sec == 0) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 136 | ND_PRINT((ndo, "-")); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 137 | usec = 1000000 - usec; |
| 138 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 139 | ND_PRINT((ndo, "%ld.%06ld", sec, usec)); |
| 140 | break; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 141 | } |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 142 | ND_TCHECK(tsp->tsp_name); |
| 143 | ND_PRINT((ndo, " name ")); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 144 | if (fn_print(ndo, (const u_char *)tsp->tsp_name, (const u_char *)tsp->tsp_name + sizeof(tsp->tsp_name))) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 145 | goto trunc; |
| 146 | return; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 147 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 148 | trunc: |
| 149 | ND_PRINT((ndo, " %s", tstr)); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 150 | } |