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.
13 * Original code by Andy Heffernan (ahh@juniper.net)
17 static const char rcsid[] _U_ =
18 "@(#) $Header: /tcpdump/master/tcpdump/print-pgm.c,v 1.1.2.5 2005/06/07 22:06:16 guy Exp $";
27 #include <tcpdump-stdinc.h>
33 #include "interface.h"
35 #include "addrtoname.h"
44 * PGM header (RFC 3208)
58 u_int32_t pgms_trailseq;
59 u_int32_t pgms_leadseq;
60 u_int16_t pgms_nla_afi;
61 u_int16_t pgms_reserved;
62 /* ... u_int8_t pgms_nla[0]; */
68 u_int16_t pgmn_source_afi;
69 u_int16_t pgmn_reserved;
70 /* ... u_int8_t pgmn_source[0]; */
71 /* ... u_int16_t pgmn_group_afi */
72 /* ... u_int16_t pgmn_reserved2; */
73 /* ... u_int8_t pgmn_group[0]; */
80 u_int16_t pgmp_reserved;
87 u_int16_t pgmp_subtype;
88 u_int16_t pgmp_nla_afi;
89 u_int16_t pgmp_reserved;
90 /* ... u_int8_t pgmp_nla[0]; */
96 u_int32_t pgmd_trailseq;
100 typedef enum _pgm_type {
101 PGM_SPM = 0, /* source path message */
102 PGM_POLL = 1, /* POLL Request */
103 PGM_POLR = 2, /* POLL Response */
104 PGM_ODATA = 4, /* original data */
105 PGM_RDATA = 5, /* repair data */
106 PGM_NAK = 8, /* NAK */
107 PGM_NULLNAK = 9, /* Null NAK */
108 PGM_NCF = 10, /* NAK Confirmation */
109 PGM_ACK = 11, /* ACK for congestion control */
110 PGM_SPMR = 12, /* SPM request */
114 #define PGM_OPT_BIT_PRESENT 0x01
115 #define PGM_OPT_BIT_NETWORK 0x02
116 #define PGM_OPT_BIT_VAR_PKTLEN 0x40
117 #define PGM_OPT_BIT_PARITY 0x80
119 #define PGM_OPT_LENGTH 0x00
120 #define PGM_OPT_FRAGMENT 0x01
121 #define PGM_OPT_NAK_LIST 0x02
122 #define PGM_OPT_JOIN 0x03
123 #define PGM_OPT_NAK_BO_IVL 0x04
124 #define PGM_OPT_NAK_BO_RNG 0x05
126 #define PGM_OPT_REDIRECT 0x07
127 #define PGM_OPT_PARITY_PRM 0x08
128 #define PGM_OPT_PARITY_GRP 0x09
129 #define PGM_OPT_CURR_TGSIZE 0x0A
130 #define PGM_OPT_NBR_UNREACH 0x0B
131 #define PGM_OPT_PATH_NLA 0x0C
133 #define PGM_OPT_SYN 0x0D
134 #define PGM_OPT_FIN 0x0E
135 #define PGM_OPT_RST 0x0F
136 #define PGM_OPT_CR 0x10
137 #define PGM_OPT_CRQST 0x11
139 #define PGM_OPT_MASK 0x7f
141 #define PGM_OPT_END 0x80 /* end of options marker */
143 #define PGM_MIN_OPT_LEN 4
151 pgm_print(register const u_char *bp, register u_int length,
152 register const u_char *bp2)
154 register const struct pgm_header *pgm;
155 register const struct ip *ip;
157 u_int16_t sport, dport;
162 char nla_buf[INET6_ADDRSTRLEN];
163 register const struct ip6_hdr *ip6;
165 char nla_buf[INET_ADDRSTRLEN];
167 u_int8_t opt_type, opt_len, flags1, flags2;
168 u_int32_t seq, opts_len, len, offset;
170 pgm = (struct pgm_header *)bp;
171 ip = (struct ip *)bp2;
174 ip6 = (struct ip6_hdr *)bp2;
179 (void)printf("Can't handle IPv6");
184 if (!TTEST(pgm->pgm_dport)) {
187 (void)printf("%s > %s: [|pgm]",
188 ip6addr_string(&ip6->ip6_src),
189 ip6addr_string(&ip6->ip6_dst));
194 (void)printf("%s > %s: [|pgm]",
195 ipaddr_string(&ip->ip_src),
196 ipaddr_string(&ip->ip_dst));
201 sport = EXTRACT_16BITS(&pgm->pgm_sport);
202 dport = EXTRACT_16BITS(&pgm->pgm_dport);
206 if (ip6->ip6_nxt == IPPROTO_PGM) {
207 (void)printf("%s.%s > %s.%s: ",
208 ip6addr_string(&ip6->ip6_src),
209 tcpport_string(sport),
210 ip6addr_string(&ip6->ip6_dst),
211 tcpport_string(dport));
213 (void)printf("%s > %s: ",
214 tcpport_string(sport), tcpport_string(dport));
219 if (ip->ip_p == IPPROTO_PGM) {
220 (void)printf("%s.%s > %s.%s: ",
221 ipaddr_string(&ip->ip_src),
222 tcpport_string(sport),
223 ipaddr_string(&ip->ip_dst),
224 tcpport_string(dport));
226 (void)printf("%s > %s: ",
227 tcpport_string(sport), tcpport_string(dport));
233 (void)printf("PGM, length %u", pgm->pgm_length);
238 if (length > pgm->pgm_length)
239 length = pgm->pgm_length;
241 (void)printf(" 0x%02x%02x%02x%02x%02x%02x ",
248 switch (pgm->pgm_type) {
252 spm = (struct pgm_spm *)(pgm + 1);
255 switch (EXTRACT_16BITS(&spm->pgms_nla_afi)) {
257 addr_size = sizeof(struct in_addr);
262 addr_size = sizeof(struct in6_addr);
270 bp = (u_char *) (spm + 1);
271 TCHECK2(*bp, addr_size);
275 inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
276 (void)printf("SPM seq %u trail %u lead %u nla %s",
277 EXTRACT_32BITS(&spm->pgms_seq),
278 EXTRACT_32BITS(&spm->pgms_trailseq),
279 EXTRACT_32BITS(&spm->pgms_leadseq),
285 struct pgm_poll *poll;
287 poll = (struct pgm_poll *)(pgm + 1);
289 (void)printf("POLL seq %u round %u",
290 EXTRACT_32BITS(&poll->pgmp_seq),
291 EXTRACT_16BITS(&poll->pgmp_round));
292 bp = (u_char *) (poll + 1);
296 struct pgm_polr *polr;
297 u_int32_t ivl, rnd, mask;
299 polr = (struct pgm_polr *)(pgm + 1);
302 switch (EXTRACT_16BITS(&polr->pgmp_nla_afi)) {
304 addr_size = sizeof(struct in_addr);
309 addr_size = sizeof(struct in6_addr);
317 bp = (u_char *) (polr + 1);
318 TCHECK2(*bp, addr_size);
322 inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
324 TCHECK2(*bp, sizeof(u_int32_t));
325 ivl = EXTRACT_32BITS(bp);
326 bp += sizeof(u_int32_t);
328 TCHECK2(*bp, sizeof(u_int32_t));
329 rnd = EXTRACT_32BITS(bp);
330 bp += sizeof(u_int32_t);
332 TCHECK2(*bp, sizeof(u_int32_t));
333 mask = EXTRACT_32BITS(bp);
334 bp += sizeof(u_int32_t);
336 (void)printf("POLR seq %u round %u nla %s ivl %u rnd 0x%08x "
337 "mask 0x%08x", EXTRACT_32BITS(&polr->pgmp_seq),
338 EXTRACT_16BITS(&polr->pgmp_round), nla_buf, ivl, rnd, mask);
342 struct pgm_data *odata;
344 odata = (struct pgm_data *)(pgm + 1);
346 (void)printf("ODATA trail %u seq %u",
347 EXTRACT_32BITS(&odata->pgmd_trailseq),
348 EXTRACT_32BITS(&odata->pgmd_seq));
349 bp = (u_char *) (odata + 1);
354 struct pgm_data *rdata;
356 rdata = (struct pgm_data *)(pgm + 1);
358 (void)printf("RDATA trail %u seq %u",
359 EXTRACT_32BITS(&rdata->pgmd_trailseq),
360 EXTRACT_32BITS(&rdata->pgmd_seq));
361 bp = (u_char *) (rdata + 1);
369 const void *source, *group;
370 int source_af, group_af;
372 char source_buf[INET6_ADDRSTRLEN], group_buf[INET6_ADDRSTRLEN];
374 char source_buf[INET_ADDRSTRLEN], group_buf[INET_ADDRSTRLEN];
377 nak = (struct pgm_nak *)(pgm + 1);
381 * Skip past the source, saving info along the way
382 * and stopping if we don't have enough.
384 switch (EXTRACT_16BITS(&nak->pgmn_source_afi)) {
386 addr_size = sizeof(struct in_addr);
391 addr_size = sizeof(struct in6_addr);
392 source_af = AF_INET6;
399 bp = (u_char *) (nak + 1);
400 TCHECK2(*bp, addr_size);
405 * Skip past the group, saving info along the way
406 * and stopping if we don't have enough.
408 switch (EXTRACT_16BITS(bp)) {
410 addr_size = sizeof(struct in_addr);
415 addr_size = sizeof(struct in6_addr);
423 bp += (2 * sizeof(u_int16_t));
424 TCHECK2(*bp, addr_size);
429 * Options decoding can go here.
431 inet_ntop(source_af, source, source_buf, sizeof(source_buf));
432 inet_ntop(group_af, group, group_buf, sizeof(group_buf));
433 switch (pgm->pgm_type) {
435 (void)printf("NAK ");
438 (void)printf("NNAK ");
441 (void)printf("NCF ");
446 (void)printf("(%s -> %s), seq %u",
447 source_buf, group_buf, EXTRACT_32BITS(&nak->pgmn_seq));
452 (void)printf("SPMR");
456 (void)printf("UNKNOWN type %0x02x", pgm->pgm_type);
460 if (pgm->pgm_options & PGM_OPT_BIT_PRESENT) {
463 * make sure there's enough for the first option header
465 if (!TTEST2(*bp, PGM_MIN_OPT_LEN)) {
466 (void)printf("[|OPT]");
471 * That option header MUST be an OPT_LENGTH option
472 * (see the first paragraph of section 9.1 in RFC 3208).
475 if ((opt_type & PGM_OPT_MASK) != PGM_OPT_LENGTH) {
476 (void)printf("[First option bad, should be PGM_OPT_LENGTH, is %u]", opt_type & PGM_OPT_MASK);
481 (void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len);
484 opts_len = EXTRACT_16BITS(bp);
486 (void)printf("[Bad total option length %u < 4]", opts_len);
489 bp += sizeof(u_int16_t);
490 (void)printf(" OPTS LEN %d", opts_len);
494 if (opts_len < PGM_MIN_OPT_LEN) {
495 (void)printf("[Total option length leaves no room for final option]");
500 if (opt_len < PGM_MIN_OPT_LEN) {
501 (void)printf("[Bad option, length %u < %u]", opt_len,
505 if (opts_len < opt_len) {
506 (void)printf("[Total option length leaves no room for final option]");
509 if (!TTEST2(*bp, opt_len - 2)) {
510 (void)printf(" [|OPT]");
514 switch (opt_type & PGM_OPT_MASK) {
517 (void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len);
520 (void)printf(" OPTS LEN (extra?) %d", EXTRACT_16BITS(bp));
521 bp += sizeof(u_int16_t);
525 case PGM_OPT_FRAGMENT:
527 (void)printf("[Bad OPT_FRAGMENT option, length %u != 16]", opt_len);
532 seq = EXTRACT_32BITS(bp);
533 bp += sizeof(u_int32_t);
534 offset = EXTRACT_32BITS(bp);
535 bp += sizeof(u_int32_t);
536 len = EXTRACT_32BITS(bp);
537 bp += sizeof(u_int32_t);
538 (void)printf(" FRAG seq %u off %u len %u", seq, offset, len);
542 case PGM_OPT_NAK_LIST:
545 opt_len -= sizeof(u_int32_t); /* option header */
546 (void)printf(" NAK LIST");
548 if (opt_len < sizeof(u_int32_t)) {
549 (void)printf("[Option length not a multiple of 4]");
552 TCHECK2(*bp, sizeof(u_int32_t));
553 (void)printf(" %u", EXTRACT_32BITS(bp));
554 bp += sizeof(u_int32_t);
555 opt_len -= sizeof(u_int32_t);
556 opts_len -= sizeof(u_int32_t);
562 (void)printf("[Bad OPT_JOIN option, length %u != 8]", opt_len);
567 seq = EXTRACT_32BITS(bp);
568 bp += sizeof(u_int32_t);
569 (void)printf(" JOIN %u", seq);
573 case PGM_OPT_NAK_BO_IVL:
575 (void)printf("[Bad OPT_NAK_BO_IVL option, length %u != 12]", opt_len);
580 offset = EXTRACT_32BITS(bp);
581 bp += sizeof(u_int32_t);
582 seq = EXTRACT_32BITS(bp);
583 bp += sizeof(u_int32_t);
584 (void)printf(" BACKOFF ivl %u ivlseq %u", offset, seq);
588 case PGM_OPT_NAK_BO_RNG:
590 (void)printf("[Bad OPT_NAK_BO_RNG option, length %u != 12]", opt_len);
595 offset = EXTRACT_32BITS(bp);
596 bp += sizeof(u_int32_t);
597 seq = EXTRACT_32BITS(bp);
598 bp += sizeof(u_int32_t);
599 (void)printf(" BACKOFF max %u min %u", offset, seq);
603 case PGM_OPT_REDIRECT:
606 switch (EXTRACT_16BITS(bp)) {
608 addr_size = sizeof(struct in_addr);
613 addr_size = sizeof(struct in6_addr);
621 bp += (2 * sizeof(u_int16_t));
622 if (opt_len != 4 + addr_size) {
623 (void)printf("[Bad OPT_REDIRECT option, length %u != 4 + address size]", opt_len);
626 TCHECK2(*bp, addr_size);
630 inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
631 (void)printf(" REDIRECT %s", (char *)nla);
632 opts_len -= 4 + addr_size;
635 case PGM_OPT_PARITY_PRM:
637 (void)printf("[Bad OPT_PARITY_PRM option, length %u != 8]", opt_len);
642 len = EXTRACT_32BITS(bp);
643 bp += sizeof(u_int32_t);
644 (void)printf(" PARITY MAXTGS %u", len);
648 case PGM_OPT_PARITY_GRP:
650 (void)printf("[Bad OPT_PARITY_GRP option, length %u != 8]", opt_len);
655 seq = EXTRACT_32BITS(bp);
656 bp += sizeof(u_int32_t);
657 (void)printf(" PARITY GROUP %u", seq);
661 case PGM_OPT_CURR_TGSIZE:
663 (void)printf("[Bad OPT_CURR_TGSIZE option, length %u != 8]", opt_len);
668 len = EXTRACT_32BITS(bp);
669 bp += sizeof(u_int32_t);
670 (void)printf(" PARITY ATGS %u", len);
674 case PGM_OPT_NBR_UNREACH:
676 (void)printf("[Bad OPT_NBR_UNREACH option, length %u != 4]", opt_len);
681 (void)printf(" NBR_UNREACH");
685 case PGM_OPT_PATH_NLA:
686 (void)printf(" PATH_NLA [%d]", opt_len);
693 (void)printf("[Bad OPT_SYN option, length %u != 4]", opt_len);
698 (void)printf(" SYN");
704 (void)printf("[Bad OPT_FIN option, length %u != 4]", opt_len);
709 (void)printf(" FIN");
715 (void)printf("[Bad OPT_RST option, length %u != 4]", opt_len);
720 (void)printf(" RST");
732 (void)printf("[Bad OPT_CRQST option, length %u != 4]", opt_len);
737 (void)printf(" CRQST");
742 (void)printf(" OPT_%02X [%d] ", opt_type, opt_len);
748 if (opt_type & PGM_OPT_END)
753 (void)printf(" [%u]", EXTRACT_16BITS(&pgm->pgm_length));
758 fputs("[|pgm]", stdout);