]> The Tcpdump Group git mirrors - tcpdump/blob - print-ptp.c
remove redundant ND_TCHECK, let GET_ routines handle checks
[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 #include <string.h>
28
29 /*
30 * PTP header
31 * 0 1 2 3
32 * 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
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | R | |msgtype| version | Msg Len |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | domain No | rsvd1 | flag Field |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Correction NS |
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
207 #define PTP_TRUE 1
208 #define PTP_FALSE !PTP_TRUE
209
210 #define PTP_HDR_LEN 0x22
211
212 /* mask based on the first byte */
213 #define PTP_VERS_MASK 0xFF
214 #define PTP_V1_COMPAT 0x10
215 #define PTP_MSG_TYPE_MASK 0x0F
216
217 /*mask based 2byte */
218 #define PTP_DOMAIN_MASK 0xFF00
219 #define PTP_RSVD1_MASK 0xFF
220 #define PTP_CONTROL_MASK 0xFF
221 #define PTP_LOGMSG_MASK 0xFF
222
223 /* mask based on the flags 2 bytes */
224
225 #define PTP_L161_MASK 0x1
226 #define PTP_L1_59_MASK 0x2
227 #define PTP_UTC_REASONABLE_MASK 0x4
228 #define PTP_TIMESCALE_MASK 0x8
229 #define PTP_TIME_TRACABLE_MASK 0x10
230 #define PTP_FREQUENCY_TRACABLE_MASK 0x20
231 #define PTP_ALTERNATE_MASTER_MASK 0x100
232 #define PTP_TWO_STEP_MASK 0x200
233 #define PTP_UNICAST_MASK 0x400
234 #define PTP_PROFILE_SPEC_1_MASK 0x1000
235 #define PTP_PROFILE_SPEC_2_MASK 0x2000
236 #define PTP_SECURITY_MASK 0x4000
237 #define PTP_FLAGS_UNKNOWN_MASK 0x18C0
238
239
240 static const struct tok ptp_flag_values[] = {
241 { PTP_L161_MASK ,"l1 61"},
242 { PTP_L1_59_MASK ,"l1 59"},
243 { PTP_UTC_REASONABLE_MASK ,"utc reasonable"},
244 { PTP_TIMESCALE_MASK ,"timescale"},
245 { PTP_TIME_TRACABLE_MASK ,"time tracable"},
246 { PTP_FREQUENCY_TRACABLE_MASK ,"frequency tracable"},
247 { PTP_ALTERNATE_MASTER_MASK ,"alternate master"},
248 { PTP_TWO_STEP_MASK ,"two step"},
249 { PTP_UNICAST_MASK ,"unicast"},
250 { PTP_PROFILE_SPEC_1_MASK ,"profile specific 1"},
251 { PTP_PROFILE_SPEC_2_MASK ,"profile specific 2"},
252 { PTP_SECURITY_MASK ,"security mask"},
253 { PTP_FLAGS_UNKNOWN_MASK , "unknown"},
254 {0, NULL}
255 };
256
257 #define PTP_PRINT_MSG_TYPE(e) \
258 { \
259 ND_PRINT("(%s)", tok2str(ptp_msg_type, "unknown", e)); \
260 }
261
262 static const char *p_porigin_ts = "preciseOriginTimeStamp";
263 static const char *p_origin_ts = "originTimeStamp";
264 static const char *p_recv_ts = "receiveTimeStamp";
265
266 #define PTP_VER_1 0x1
267 #define PTP_VER_2 0x2
268
269 #define PTP_UCHAR_LEN sizeof(uint8_t)
270 #define PTP_UINT16_LEN sizeof(uint16_t)
271 #define PTP_UINT32_LEN sizeof(uint32_t)
272 #define PTP_6BYTES_LEN sizeof(uint32_t)+sizeof(uint16_t)
273 #define PTP_UINT64_LEN sizeof(uint64_t)
274
275
276
277 static void ptp_print_1(netdissect_options *ndo);
278 static void ptp_print_2(netdissect_options *ndo, const u_char *bp, u_int len);
279
280 static void ptp_print_timestamp(netdissect_options *ndo, const u_char *bp, u_int *len, const char *stype);
281 static void ptp_print_timestamp_identity(netdissect_options *ndo, const u_char *bp, u_int *len, const char *ttype);
282 static void ptp_print_announce_msg(netdissect_options *ndo, const u_char *bp, u_int *len);
283 static void ptp_print_port_id(netdissect_options *ndo, const u_char *bp, u_int *len);
284 static void ptp_print_mgmt_msg(netdissect_options *ndo, const u_char *bp, u_int *len);
285
286 static void
287 print_field(netdissect_options *ndo, const char *st, uint32_t flen,
288 const u_char *bp, u_int *len, uint8_t hex)
289 {
290 uint8_t u8_val;
291 uint16_t u16_val;
292 uint32_t u32_val;
293 uint64_t u64_val;
294
295 switch(flen) {
296 case PTP_UCHAR_LEN:
297 u8_val = GET_U_1(bp);
298 ND_PRINT(", %s", st);
299 if (hex)
300 ND_PRINT(" 0x%x", u8_val);
301 else
302 ND_PRINT(" %u", u8_val);
303 *len -= 1; bp += 1;
304 break;
305 case PTP_UINT16_LEN:
306 u16_val = GET_BE_U_2(bp);
307 ND_PRINT(", %s", st);
308 if (hex)
309 ND_PRINT(" 0x%x", u16_val);
310 else
311 ND_PRINT(" %u", u16_val);
312 *len -= 2; bp += 2;
313 break;
314 case PTP_UINT32_LEN:
315 u32_val = GET_BE_U_4(bp);
316 ND_PRINT(", %s", st);
317 if (hex)
318 ND_PRINT(" 0x%x", u32_val);
319 else
320 ND_PRINT(" %u", u32_val);
321 *len -= 4; bp += 4;
322 break;
323 case PTP_UINT64_LEN:
324 u64_val = GET_BE_U_8(bp);
325 ND_PRINT(", %s", st);
326 if (hex)
327 ND_PRINT(" 0x%"PRIx64, u64_val);
328 else
329 ND_PRINT(" 0x%"PRIu64, u64_val);
330 *len -= 8; bp += 8;
331 break;
332 default:
333 break;
334 }
335 }
336
337 static void
338 ptp_print_1(netdissect_options *ndo)
339 {
340 ND_PRINT(" (not implemented)");
341 }
342
343 static void
344 ptp_print_2(netdissect_options *ndo, const u_char *bp, u_int length)
345 {
346 u_int len = length;
347 uint16_t msg_len, flags, port_id, seq_id;
348 uint8_t foct, domain_no, msg_type, v1_compat, rsvd1, lm_int, control;
349 uint32_t ns_corr, sns_corr, rsvd2;
350 uint64_t clk_id;
351
352 foct = GET_U_1(bp);
353 v1_compat = foct & PTP_V1_COMPAT;
354 ND_PRINT(", v1 compat : %s", v1_compat?"yes":"no");
355 msg_type = foct & PTP_MSG_TYPE_MASK;
356 ND_PRINT(", msg type : %s", tok2str(ptp_msg_type, "none", msg_type));
357
358 /* msg length */
359 len -= 2; bp += 2; msg_len = GET_BE_U_2(bp); ND_PRINT(", length : %u", msg_len);
360
361 /* domain */
362 len -= 2; bp += 2; domain_no = (GET_BE_U_2(bp) & PTP_DOMAIN_MASK) >> 8; ND_PRINT(", domain : %u", domain_no);
363
364 /* rsvd 1*/
365 rsvd1 = GET_BE_U_2(bp) & PTP_RSVD1_MASK;
366 ND_PRINT(", reserved1 : %u", rsvd1);
367
368 /* flags */
369 len -= 2; bp += 2; flags = GET_BE_U_2(bp); ND_PRINT(", Flags [%s]", bittok2str(ptp_flag_values, "none", flags));
370
371 /* correction NS */
372 len -= 2; bp += 2; ns_corr = GET_BE_U_4(bp); ND_PRINT(", NS correction : %u", ns_corr);
373
374 /* correction sub NS */
375 len -= 4; bp += 4; sns_corr = GET_BE_U_4(bp); ND_PRINT(", sub NS correction : %u", sns_corr);
376
377 /* Reserved 2 */
378 len -= 4; bp += 4; rsvd2 = GET_BE_U_4(bp); ND_PRINT(", reserved2 : %u", rsvd2);
379
380 /* clock identity */
381 len -= 4; bp += 4; clk_id = GET_BE_U_8(bp); ND_PRINT(", clock identity : 0x%"PRIx64, clk_id);
382
383 /* port identity */
384 len -= 8; bp += 8; port_id = GET_BE_U_2(bp); ND_PRINT(", port id : %u", port_id);
385
386 /* sequence ID */
387 len -= 2; bp += 2; seq_id = GET_BE_U_2(bp); ND_PRINT(", seq id : %u", seq_id);
388
389 /* control */
390 len -= 2; bp += 2; control = GET_U_1(bp) ;
391 ND_PRINT(", control : %u (%s)", control, tok2str(ptp_msg_type, "none", control));
392
393 /* log message interval */
394 lm_int = GET_BE_U_2(bp) & PTP_LOGMSG_MASK; ND_PRINT(", log message interval : %u", lm_int); len -= 2; bp += 2;
395
396 switch(msg_type) {
397 case M_SYNC:
398 ptp_print_timestamp(ndo, bp, &len, p_origin_ts);
399 break;
400 case M_DELAY_REQ:
401 ptp_print_timestamp(ndo, bp, &len, p_origin_ts);
402 break;
403 case M_PDELAY_REQ:
404 ptp_print_timestamp_identity(ndo, bp, &len, p_porigin_ts);
405 break;
406 case M_PDELAY_RESP:
407 ptp_print_timestamp_identity(ndo, bp, &len, p_recv_ts);
408 break;
409 case M_FOLLOW_UP:
410 ptp_print_timestamp(ndo, bp, &len, p_porigin_ts);
411 break;
412 case M_DELAY_RESP:
413 ptp_print_timestamp_identity(ndo, bp, &len, p_recv_ts);
414 break;
415 case M_PDELAY_RESP_FOLLOW_UP:
416 ptp_print_timestamp_identity(ndo, bp, &len, p_porigin_ts);
417 break;
418 case M_ANNOUNCE:
419 ptp_print_announce_msg(ndo, bp, &len);
420 break;
421 case M_SIGNALLING:
422 ptp_print_port_id(ndo, bp, &len);
423 break;
424 case M_MANAGEMENT:
425 ptp_print_mgmt_msg(ndo, bp, &len);
426 break;
427 default:
428 break;
429 }
430 }
431 /*
432 * PTP general message
433 */
434 void
435 ptp_print(netdissect_options *ndo, const u_char *bp, u_int len)
436 {
437 u_int vers;
438
439 ndo->ndo_protocol = "ptp";
440 if (len < PTP_HDR_LEN) {
441 goto trunc;
442 }
443 vers = GET_BE_U_2(bp) & PTP_VERS_MASK;
444 ND_PRINT("PTPv%u",vers);
445 switch(vers) {
446 case PTP_VER_1:
447 ptp_print_1(ndo);
448 break;
449 case PTP_VER_2:
450 ptp_print_2(ndo, bp, len);
451 break;
452 default:
453 //ND_PRINT("ERROR: unknown-version\n");
454 break;
455 }
456 return;
457
458 trunc:
459 nd_print_trunc(ndo);
460 return;
461 }
462
463 static void
464 ptp_print_timestamp(netdissect_options *ndo, const u_char *bp, u_int *len, const char *stype)
465 {
466 uint64_t secs;
467 uint32_t nsecs;
468
469 ND_PRINT(", %s :", stype);
470 /* sec time stamp 6 bytes */
471 secs = GET_BE_U_6(bp);
472 ND_PRINT(" %"PRIu64" seconds,", secs);
473 *len -= 6;
474 bp += 6;
475
476 /* NS time stamp 4 bytes */
477 nsecs = GET_BE_U_4(bp);
478 ND_PRINT(" %u nanoseconds", nsecs);
479 *len -= 4;
480 bp += 4;
481 }
482 static void
483 ptp_print_timestamp_identity(netdissect_options *ndo,
484 const u_char *bp, u_int *len, const char *ttype)
485 {
486 uint64_t secs;
487 uint32_t nsecs;
488 uint16_t port_id;
489 uint64_t port_identity;
490
491 ND_PRINT(", %s :", ttype);
492 /* sec time stamp 6 bytes */
493 secs = GET_BE_U_6(bp);
494 ND_PRINT(" %"PRIu64" seconds,", secs);
495 *len -= 6;
496 bp += 6;
497
498 /* NS time stamp 4 bytes */
499 nsecs = GET_BE_U_4(bp);
500 ND_PRINT(" %u nanoseconds", nsecs);
501 *len -= 4;
502 bp += 4;
503
504 /* port identity*/
505 port_identity = GET_BE_U_8(bp);
506 ND_PRINT(", port identity : 0x%"PRIx64, port_identity);
507 *len -= 8;
508 bp += 8;
509
510 /* port id */
511 port_id = GET_BE_U_2(bp);
512 ND_PRINT(", port id : %u", port_id);
513 *len -= 2;
514 bp += 2;
515 }
516 static void
517 ptp_print_announce_msg(netdissect_options *ndo, const u_char *bp, u_int *len)
518 {
519 uint8_t rsvd, gm_prio_1, gm_prio_2, gm_clk_cls, gm_clk_acc, time_src;
520 uint16_t origin_cur_utc, gm_clk_var, steps_removed;
521 uint64_t gm_clock_id;
522 uint64_t secs;
523 uint32_t nsecs;
524
525 ND_PRINT(", %s :", p_origin_ts);
526 /* sec time stamp 6 bytes */
527 secs = GET_BE_U_6(bp);
528 ND_PRINT(" %"PRIu64" seconds", secs);
529 *len -= 6;
530 bp += 6;
531
532 /* NS time stamp 4 bytes */
533 nsecs = GET_BE_U_4(bp);
534 ND_PRINT(" %u nanoseconds", nsecs);
535 *len -= 4;
536 bp += 4;
537
538 /* origin cur utc */
539 origin_cur_utc = GET_BE_U_2(bp);
540 ND_PRINT(", origin cur utc :%u", origin_cur_utc);
541 *len -= 2;
542 bp += 2;
543
544 /* rsvd */
545 rsvd = GET_U_1(bp);
546 ND_PRINT(", rsvd : %u", rsvd);
547 *len -= 1;
548 bp += 1;
549
550 /* gm prio */
551 gm_prio_1 = GET_U_1(bp);
552 ND_PRINT(", gm priority_1 : %u", gm_prio_1);
553 *len -= 1;
554 bp += 1;
555
556 /* GM clock class */
557 gm_clk_cls = GET_U_1(bp);
558 ND_PRINT(", gm clock class : %u", gm_clk_cls);
559 *len -= 1;
560 bp += 1;
561 /* GM clock accuracy */
562 gm_clk_acc = GET_U_1(bp);
563 ND_PRINT(", gm clock accuracy : %u", gm_clk_acc);
564 *len -= 1;
565 bp += 1;
566 /* GM clock variance */
567 gm_clk_var = GET_BE_U_2(bp);
568 ND_PRINT(", gm clock variance : %u", gm_clk_var);
569 *len -= 2;
570 bp += 2;
571 /* GM Prio 2 */
572 gm_prio_2 = GET_U_1(bp);
573 ND_PRINT(", gm priority_2 : %u", gm_prio_2);
574 *len -= 1;
575 bp += 1;
576
577 /* GM Clock Identity */
578 gm_clock_id = GET_BE_U_8(bp);
579 ND_PRINT(", gm clock id : 0x%"PRIx64, gm_clock_id);
580 *len -= 8;
581 bp += 8;
582 /* steps removed */
583 steps_removed = GET_BE_U_2(bp);
584 ND_PRINT(", steps removed : %u", steps_removed);
585 *len -= 2;
586 bp += 2;
587 /* Time source */
588 time_src = GET_U_1(bp);
589 ND_PRINT(", time source : 0x%x", time_src);
590 *len -= 1;
591 bp += 1;
592
593 }
594 static void
595 ptp_print_port_id(netdissect_options *ndo, const u_char *bp, u_int *len)
596 {
597 uint16_t port_id;
598 uint64_t port_identity;
599
600 /* port identity*/
601 port_identity = GET_BE_U_8(bp);
602 ND_PRINT(", port identity : 0x%"PRIx64, port_identity);
603 *len -= 8;
604 bp += 8;
605
606 /* port id */
607 port_id = GET_BE_U_2(bp);
608 ND_PRINT(", port id : %u", port_id);
609 *len -= 2;
610 bp += 2;
611
612 }
613
614 static void
615 ptp_print_mgmt_msg(netdissect_options *ndo, const u_char *bp, u_int *len)
616 {
617 ptp_print_port_id(ndo, bp, len);
618 print_field(ndo, ", start boundary hops ", PTP_UCHAR_LEN, bp, len, PTP_FALSE);
619 print_field(ndo, ", boundary hops ", PTP_UCHAR_LEN, bp, len, PTP_FALSE);
620 print_field(ndo, ", flags ", PTP_UCHAR_LEN, bp, len, PTP_TRUE);
621 print_field(ndo, ", reserved ", PTP_UCHAR_LEN, bp, len, PTP_TRUE);
622 }