]> The Tcpdump Group git mirrors - tcpdump/blob - print-ptp.c
32f466488c62bd5c411ae0973a275adce8b4d599
[tcpdump] / print-ptp.c
1 /*
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that: (1) source code
4 * distributions retain the above copyright notice and this paragraph
5 * in its entirety, and (2) distributions including binary code include
6 * the above copyright notice and this paragraph in its entirety in
7 * the documentation or other materials provided with the distribution.
8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 * FOR A PARTICULAR PURPOSE.
12 *
13 * Original code by Partha S. Ghosh (psglinux dot gmail dot com)
14 */
15
16 /* \summary: Precision Time Protocol (PTP) printer */
17
18 /* specification: https://standards.ieee.org/findstds/standard/1588-2008.html*/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "netdissect-stdinc.h"
25 #include "netdissect.h"
26 #include "extract.h"
27
28 /*
29 * PTP header
30 * 0 1 2 3
31 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | R | |msgtype| version | Msg Len |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | domain No | rsvd1 | flag Field |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | Correction NS |
38 * + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | | Correction Sub NS |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Reserved2 |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Clock Identity |
44 * | |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Port Identity | Sequence ID |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | control | log msg int |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51 * 0 1 2 3
52 *
53 * Announce Message (msg type=0xB)
54 * 0 1 2 3
55 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
59 * | Seconds |
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * | Nano Seconds |
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | Origin Cur UTC Offset | Reserved | GM Prio 1 |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * |GM Clock Class | GM Clock Accu | GM Clock Variance |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * | GM Prio 2 | |
68 * +-+-+-+-+-+-+-+-+ +
69 * | GM Clock Identity |
70 * + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * | | Steps Removed | Time Source |
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
74 * 0 1 2 3
75 *
76 * Sync Message (msg type=0x0)
77 * 0 1 2 3
78 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
79 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 * | |
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
82 * | Seconds |
83 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84 * | Nano Seconds |
85 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 *
87 * Delay Request Message (msg type=0x1)
88 * 0 1 2 3
89 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | |
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
93 * | Origin Time Stamp Seconds |
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 * | Nano Seconds |
96 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 *
98 * Followup Message (msg type=0x8)
99 * 0 1 2 3
100 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 * | |
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
104 * | Precise Origin Time Stamp Seconds |
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * | Nano Seconds |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 *
109 * Delay Resp Message (msg type=0x9)
110 * 0 1 2 3
111 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 * | |
114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
115 * | Seconds |
116 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 * | Nano Seconds |
118 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 * | Port Identity |
120 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 *
122 * PDelay Request Message (msg type=0x2)
123 * 0 1 2 3
124 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
125 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 * | |
127 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
128 * | Origin Time Stamp Seconds |
129 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 * | Origin Time Stamp Nano Seconds |
131 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * | Port Identity |
133 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 *
135 * PDelay Response Message (msg type=0x3)
136 * 0 1 2 3
137 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
141 * | Request receipt Time Stamp Seconds |
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * | Nano Seconds |
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * | Requesting Port Identity |
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 *
148 * PDelay Resp Follow up Message (msg type=0xA)
149 * 0 1 2 3
150 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
151 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152 * | |
153 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
154 * | Response Origin Time Stamp Seconds |
155 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 * | Nano Seconds |
157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 * | Requesting Port Identity |
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160 *
161 * Signalling Message (msg type=0xC)
162 * 0 1 2 3
163 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
164 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165 * | Requesting Port Identity |
166 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 *
168 * Management Message (msg type=0xD)
169 * 0 1 2 3
170 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172 * | Requesting Port Identity |
173 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
174 * |Start Bndry Hps| Boundary Hops | flags | Reserved |
175 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
176 *
177 */
178
179 #define M_SYNC 0x0
180 #define M_DELAY_REQ 0x1
181 #define M_PDELAY_REQ 0x2
182 #define M_PDELAY_RESP 0x3
183 #define M_OTHER 0x5
184 #define M_FOLLOW_UP 0x8
185 #define M_DELAY_RESP 0x9
186 #define M_PDELAY_RESP_FOLLOW_UP 0xA
187 #define M_ANNOUNCE 0xB
188 #define M_SIGNALLING 0xC
189 #define M_MANAGEMENT 0xD
190
191 static const struct tok ptp_msg_type[] = {
192 { M_SYNC ,"sync msg"},
193 { M_DELAY_REQ ,"delay req msg"},
194 { M_PDELAY_REQ ,"peer delay req msg"},
195 { M_PDELAY_RESP ,"peer delay resp msg"},
196 { M_OTHER, "Other"},
197 { M_FOLLOW_UP ,"follow up msg"},
198 { M_DELAY_RESP ,"delay resp msg"},
199 { M_PDELAY_RESP_FOLLOW_UP ,"pdelay resp fup msg"},
200 { M_ANNOUNCE ,"announce msg"},
201 { M_SIGNALLING ,"signalling msg"},
202 { M_MANAGEMENT ,"management msg"},
203 { 0, NULL}
204 };
205
206 #define PTP_TRUE 1
207 #define PTP_FALSE !PTP_TRUE
208
209 #define PTP_HDR_LEN 0x22
210
211 /* mask based on the first byte */
212 #define PTP_VERS_MASK 0xFF
213 #define PTP_V1_COMPAT 0x10
214 #define PTP_MSG_TYPE_MASK 0x0F
215
216 /*mask based 2byte */
217 #define PTP_DOMAIN_MASK 0xFF00
218 #define PTP_RSVD1_MASK 0xFF
219 #define PTP_CONTROL_MASK 0xFF
220 #define PTP_LOGMSG_MASK 0xFF
221
222 /* mask based on the flags 2 bytes */
223
224 #define PTP_L161_MASK 0x1
225 #define PTP_L1_59_MASK 0x2
226 #define PTP_UTC_REASONABLE_MASK 0x4
227 #define PTP_TIMESCALE_MASK 0x8
228 #define PTP_TIME_TRACABLE_MASK 0x10
229 #define PTP_FREQUENCY_TRACABLE_MASK 0x20
230 #define PTP_ALTERNATE_MASTER_MASK 0x100
231 #define PTP_TWO_STEP_MASK 0x200
232 #define PTP_UNICAST_MASK 0x400
233 #define PTP_PROFILE_SPEC_1_MASK 0x1000
234 #define PTP_PROFILE_SPEC_2_MASK 0x2000
235 #define PTP_SECURITY_MASK 0x4000
236 #define PTP_FLAGS_UNKNOWN_MASK 0x18C0
237
238 static const struct tok ptp_flag_values[] = {
239 { PTP_L161_MASK ,"l1 61"},
240 { PTP_L1_59_MASK ,"l1 59"},
241 { PTP_UTC_REASONABLE_MASK ,"utc reasonable"},
242 { PTP_TIMESCALE_MASK ,"timescale"},
243 { PTP_TIME_TRACABLE_MASK ,"time tracable"},
244 { PTP_FREQUENCY_TRACABLE_MASK ,"frequency tracable"},
245 { PTP_ALTERNATE_MASTER_MASK ,"alternate master"},
246 { PTP_TWO_STEP_MASK ,"two step"},
247 { PTP_UNICAST_MASK ,"unicast"},
248 { PTP_PROFILE_SPEC_1_MASK ,"profile specific 1"},
249 { PTP_PROFILE_SPEC_2_MASK ,"profile specific 2"},
250 { PTP_SECURITY_MASK ,"security mask"},
251 { PTP_FLAGS_UNKNOWN_MASK , "unknown"},
252 {0, NULL}
253 };
254
255 static const char *p_porigin_ts = "preciseOriginTimeStamp";
256 static const char *p_origin_ts = "originTimeStamp";
257 static const char *p_recv_ts = "receiveTimeStamp";
258
259 #define PTP_VER_1 0x1
260 #define PTP_VER_2 0x2
261
262 #define PTP_UCHAR_LEN sizeof(uint8_t)
263 #define PTP_UINT16_LEN sizeof(uint16_t)
264 #define PTP_UINT32_LEN sizeof(uint32_t)
265 #define PTP_6BYTES_LEN sizeof(uint32_t)+sizeof(uint16_t)
266 #define PTP_UINT64_LEN sizeof(uint64_t)
267
268 static void ptp_print_1(netdissect_options *ndo);
269 static void ptp_print_2(netdissect_options *ndo, const u_char *bp, u_int len);
270
271 static void ptp_print_timestamp(netdissect_options *ndo, const u_char *bp, u_int *len, const char *stype);
272 static void ptp_print_timestamp_identity(netdissect_options *ndo, const u_char *bp, u_int *len, const char *ttype);
273 static void ptp_print_announce_msg(netdissect_options *ndo, const u_char *bp, u_int *len);
274 static void ptp_print_port_id(netdissect_options *ndo, const u_char *bp, u_int *len);
275 static void ptp_print_mgmt_msg(netdissect_options *ndo, const u_char *bp, u_int *len);
276
277 static void
278 print_field(netdissect_options *ndo, const char *st, uint32_t flen,
279 const u_char *bp, u_int *len, uint8_t hex)
280 {
281 uint8_t u8_val;
282 uint16_t u16_val;
283 uint32_t u32_val;
284 uint64_t u64_val;
285
286 switch(flen) {
287 case PTP_UCHAR_LEN:
288 u8_val = GET_U_1(bp);
289 ND_PRINT(", %s", st);
290 if (hex)
291 ND_PRINT(" 0x%x", u8_val);
292 else
293 ND_PRINT(" %u", u8_val);
294 *len -= 1; bp += 1;
295 break;
296 case PTP_UINT16_LEN:
297 u16_val = GET_BE_U_2(bp);
298 ND_PRINT(", %s", st);
299 if (hex)
300 ND_PRINT(" 0x%x", u16_val);
301 else
302 ND_PRINT(" %u", u16_val);
303 *len -= 2; bp += 2;
304 break;
305 case PTP_UINT32_LEN:
306 u32_val = GET_BE_U_4(bp);
307 ND_PRINT(", %s", st);
308 if (hex)
309 ND_PRINT(" 0x%x", u32_val);
310 else
311 ND_PRINT(" %u", u32_val);
312 *len -= 4; bp += 4;
313 break;
314 case PTP_UINT64_LEN:
315 u64_val = GET_BE_U_8(bp);
316 ND_PRINT(", %s", st);
317 if (hex)
318 ND_PRINT(" 0x%"PRIx64, u64_val);
319 else
320 ND_PRINT(" 0x%"PRIu64, u64_val);
321 *len -= 8; bp += 8;
322 break;
323 default:
324 break;
325 }
326 }
327
328 static void
329 ptp_print_1(netdissect_options *ndo)
330 {
331 ND_PRINT(" (not implemented)");
332 }
333
334 static void
335 ptp_print_2(netdissect_options *ndo, const u_char *bp, u_int length)
336 {
337 u_int len = length;
338 uint16_t msg_len, flags, port_id, seq_id;
339 uint8_t foct, domain_no, msg_type, v1_compat, rsvd1, lm_int, control;
340 uint64_t ns_corr;
341 uint16_t sns_corr;
342 uint32_t rsvd2;
343 uint64_t clk_id;
344
345 foct = GET_U_1(bp);
346 v1_compat = foct & PTP_V1_COMPAT;
347 ND_PRINT(", v1 compat : %s", v1_compat?"yes":"no");
348 msg_type = foct & PTP_MSG_TYPE_MASK;
349 ND_PRINT(", msg type : %s", tok2str(ptp_msg_type, "none", msg_type));
350
351 /* msg length */
352 len -= 2; bp += 2; msg_len = GET_BE_U_2(bp); ND_PRINT(", length : %u", msg_len);
353
354 /* domain */
355 len -= 2; bp += 2; domain_no = (GET_BE_U_2(bp) & PTP_DOMAIN_MASK) >> 8; ND_PRINT(", domain : %u", domain_no);
356
357 /* rsvd 1*/
358 rsvd1 = GET_BE_U_2(bp) & PTP_RSVD1_MASK;
359 ND_PRINT(", reserved1 : %u", rsvd1);
360
361 /* flags */
362 len -= 2; bp += 2; flags = GET_BE_U_2(bp); ND_PRINT(", Flags [%s]", bittok2str(ptp_flag_values, "none", flags));
363
364 /* correction NS (48 bits) */
365 len -= 2; bp += 2; ns_corr = GET_BE_U_6(bp); ND_PRINT(", NS correction : %"PRIu64, ns_corr);
366
367 /* correction sub NS (16 bits) */
368 len -= 6; bp += 6; sns_corr = GET_BE_U_2(bp); ND_PRINT(", sub NS correction : %u", sns_corr);
369
370 /* Reserved 2 */
371 len -= 2; bp += 2; rsvd2 = GET_BE_U_4(bp); ND_PRINT(", reserved2 : %u", rsvd2);
372
373 /* clock identity */
374 len -= 4; bp += 4; clk_id = GET_BE_U_8(bp); ND_PRINT(", clock identity : 0x%"PRIx64, clk_id);
375
376 /* port identity */
377 len -= 8; bp += 8; port_id = GET_BE_U_2(bp); ND_PRINT(", port id : %u", port_id);
378
379 /* sequence ID */
380 len -= 2; bp += 2; seq_id = GET_BE_U_2(bp); ND_PRINT(", seq id : %u", seq_id);
381
382 /* control */
383 len -= 2; bp += 2; control = GET_U_1(bp) ;
384 ND_PRINT(", control : %u (%s)", control, tok2str(ptp_msg_type, "none", control));
385
386 /* log message interval */
387 lm_int = GET_BE_U_2(bp) & PTP_LOGMSG_MASK; ND_PRINT(", log message interval : %u", lm_int); len -= 2; bp += 2;
388
389 switch(msg_type) {
390 case M_SYNC:
391 ptp_print_timestamp(ndo, bp, &len, p_origin_ts);
392 break;
393 case M_DELAY_REQ:
394 ptp_print_timestamp(ndo, bp, &len, p_origin_ts);
395 break;
396 case M_PDELAY_REQ:
397 ptp_print_timestamp_identity(ndo, bp, &len, p_porigin_ts);
398 break;
399 case M_PDELAY_RESP:
400 ptp_print_timestamp_identity(ndo, bp, &len, p_recv_ts);
401 break;
402 case M_FOLLOW_UP:
403 ptp_print_timestamp(ndo, bp, &len, p_porigin_ts);
404 break;
405 case M_DELAY_RESP:
406 ptp_print_timestamp_identity(ndo, bp, &len, p_recv_ts);
407 break;
408 case M_PDELAY_RESP_FOLLOW_UP:
409 ptp_print_timestamp_identity(ndo, bp, &len, p_porigin_ts);
410 break;
411 case M_ANNOUNCE:
412 ptp_print_announce_msg(ndo, bp, &len);
413 break;
414 case M_SIGNALLING:
415 ptp_print_port_id(ndo, bp, &len);
416 break;
417 case M_MANAGEMENT:
418 ptp_print_mgmt_msg(ndo, bp, &len);
419 break;
420 default:
421 break;
422 }
423 }
424 /*
425 * PTP general message
426 */
427 void
428 ptp_print(netdissect_options *ndo, const u_char *bp, u_int length)
429 {
430 u_int vers;
431
432 ndo->ndo_protocol = "ptp";
433 ND_ICHECK_U(length, <, PTP_HDR_LEN);
434 vers = GET_BE_U_2(bp) & PTP_VERS_MASK;
435 ND_PRINT("PTPv%u",vers);
436 switch(vers) {
437 case PTP_VER_1:
438 ptp_print_1(ndo);
439 break;
440 case PTP_VER_2:
441 ptp_print_2(ndo, bp, length);
442 break;
443 default:
444 //ND_PRINT("ERROR: unknown-version\n");
445 break;
446 }
447 return;
448
449 invalid:
450 nd_print_invalid(ndo);
451 }
452
453 static void
454 ptp_print_timestamp(netdissect_options *ndo, const u_char *bp, u_int *len, const char *stype)
455 {
456 uint64_t secs;
457 uint32_t nsecs;
458
459 ND_PRINT(", %s :", stype);
460 /* sec time stamp 6 bytes */
461 secs = GET_BE_U_6(bp);
462 ND_PRINT(" %"PRIu64" seconds,", secs);
463 *len -= 6;
464 bp += 6;
465
466 /* NS time stamp 4 bytes */
467 nsecs = GET_BE_U_4(bp);
468 ND_PRINT(" %u nanoseconds", nsecs);
469 *len -= 4;
470 bp += 4;
471 }
472 static void
473 ptp_print_timestamp_identity(netdissect_options *ndo,
474 const u_char *bp, u_int *len, const char *ttype)
475 {
476 uint64_t secs;
477 uint32_t nsecs;
478 uint16_t port_id;
479 uint64_t port_identity;
480
481 ND_PRINT(", %s :", ttype);
482 /* sec time stamp 6 bytes */
483 secs = GET_BE_U_6(bp);
484 ND_PRINT(" %"PRIu64" seconds,", secs);
485 *len -= 6;
486 bp += 6;
487
488 /* NS time stamp 4 bytes */
489 nsecs = GET_BE_U_4(bp);
490 ND_PRINT(" %u nanoseconds", nsecs);
491 *len -= 4;
492 bp += 4;
493
494 /* port identity*/
495 port_identity = GET_BE_U_8(bp);
496 ND_PRINT(", port identity : 0x%"PRIx64, port_identity);
497 *len -= 8;
498 bp += 8;
499
500 /* port id */
501 port_id = GET_BE_U_2(bp);
502 ND_PRINT(", port id : %u", port_id);
503 *len -= 2;
504 bp += 2;
505 }
506 static void
507 ptp_print_announce_msg(netdissect_options *ndo, const u_char *bp, u_int *len)
508 {
509 uint8_t rsvd, gm_prio_1, gm_prio_2, gm_clk_cls, gm_clk_acc, time_src;
510 uint16_t origin_cur_utc, gm_clk_var, steps_removed;
511 uint64_t gm_clock_id;
512 uint64_t secs;
513 uint32_t nsecs;
514
515 ND_PRINT(", %s :", p_origin_ts);
516 /* sec time stamp 6 bytes */
517 secs = GET_BE_U_6(bp);
518 ND_PRINT(" %"PRIu64" seconds", secs);
519 *len -= 6;
520 bp += 6;
521
522 /* NS time stamp 4 bytes */
523 nsecs = GET_BE_U_4(bp);
524 ND_PRINT(" %u nanoseconds", nsecs);
525 *len -= 4;
526 bp += 4;
527
528 /* origin cur utc */
529 origin_cur_utc = GET_BE_U_2(bp);
530 ND_PRINT(", origin cur utc :%u", origin_cur_utc);
531 *len -= 2;
532 bp += 2;
533
534 /* rsvd */
535 rsvd = GET_U_1(bp);
536 ND_PRINT(", rsvd : %u", rsvd);
537 *len -= 1;
538 bp += 1;
539
540 /* gm prio */
541 gm_prio_1 = GET_U_1(bp);
542 ND_PRINT(", gm priority_1 : %u", gm_prio_1);
543 *len -= 1;
544 bp += 1;
545
546 /* GM clock class */
547 gm_clk_cls = GET_U_1(bp);
548 ND_PRINT(", gm clock class : %u", gm_clk_cls);
549 *len -= 1;
550 bp += 1;
551 /* GM clock accuracy */
552 gm_clk_acc = GET_U_1(bp);
553 ND_PRINT(", gm clock accuracy : %u", gm_clk_acc);
554 *len -= 1;
555 bp += 1;
556 /* GM clock variance */
557 gm_clk_var = GET_BE_U_2(bp);
558 ND_PRINT(", gm clock variance : %u", gm_clk_var);
559 *len -= 2;
560 bp += 2;
561 /* GM Prio 2 */
562 gm_prio_2 = GET_U_1(bp);
563 ND_PRINT(", gm priority_2 : %u", gm_prio_2);
564 *len -= 1;
565 bp += 1;
566
567 /* GM Clock Identity */
568 gm_clock_id = GET_BE_U_8(bp);
569 ND_PRINT(", gm clock id : 0x%"PRIx64, gm_clock_id);
570 *len -= 8;
571 bp += 8;
572 /* steps removed */
573 steps_removed = GET_BE_U_2(bp);
574 ND_PRINT(", steps removed : %u", steps_removed);
575 *len -= 2;
576 bp += 2;
577 /* Time source */
578 time_src = GET_U_1(bp);
579 ND_PRINT(", time source : 0x%x", time_src);
580 *len -= 1;
581 bp += 1;
582
583 }
584 static void
585 ptp_print_port_id(netdissect_options *ndo, const u_char *bp, u_int *len)
586 {
587 uint16_t port_id;
588 uint64_t port_identity;
589
590 /* port identity*/
591 port_identity = GET_BE_U_8(bp);
592 ND_PRINT(", port identity : 0x%"PRIx64, port_identity);
593 *len -= 8;
594 bp += 8;
595
596 /* port id */
597 port_id = GET_BE_U_2(bp);
598 ND_PRINT(", port id : %u", port_id);
599 *len -= 2;
600 bp += 2;
601
602 }
603
604 static void
605 ptp_print_mgmt_msg(netdissect_options *ndo, const u_char *bp, u_int *len)
606 {
607 ptp_print_port_id(ndo, bp, len);
608 print_field(ndo, ", start boundary hops ", PTP_UCHAR_LEN, bp, len, PTP_FALSE);
609 print_field(ndo, ", boundary hops ", PTP_UCHAR_LEN, bp, len, PTP_FALSE);
610 print_field(ndo, ", flags ", PTP_UCHAR_LEN, bp, len, PTP_TRUE);
611 print_field(ndo, ", reserved ", PTP_UCHAR_LEN, bp, len, PTP_TRUE);
612 }