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 $";
25 #include <tcpdump-stdinc.h>
31 #include "interface.h"
33 #include "addrtoname.h"
42 * PGM header (RFC 3208)
56 u_int32_t pgms_trailseq;
57 u_int32_t pgms_leadseq;
58 u_int16_t pgms_nla_afi;
59 u_int16_t pgms_reserved;
60 /* ... u_int8_t pgms_nla[0]; */
66 u_int16_t pgmn_source_afi;
67 u_int16_t pgmn_reserved;
68 /* ... u_int8_t pgmn_source[0]; */
69 /* ... u_int16_t pgmn_group_afi */
70 /* ... u_int16_t pgmn_reserved2; */
71 /* ... u_int8_t pgmn_group[0]; */
78 u_int16_t pgmp_reserved;
85 u_int16_t pgmp_subtype;
86 u_int16_t pgmp_nla_afi;
87 u_int16_t pgmp_reserved;
88 /* ... u_int8_t pgmp_nla[0]; */
94 u_int32_t pgmd_trailseq;
98 typedef enum _pgm_type {
99 PGM_SPM = 0, /* source path message */
100 PGM_POLL = 1, /* POLL Request */
101 PGM_POLR = 2, /* POLL Response */
102 PGM_ODATA = 4, /* original data */
103 PGM_RDATA = 5, /* repair data */
104 PGM_NAK = 8, /* NAK */
105 PGM_NULLNAK = 9, /* Null NAK */
106 PGM_NCF = 10, /* NAK Confirmation */
107 PGM_ACK = 11, /* ACK for congestion control */
108 PGM_SPMR = 12, /* SPM request */
112 #define PGM_OPT_BIT_PRESENT 0x01
113 #define PGM_OPT_BIT_NETWORK 0x02
114 #define PGM_OPT_BIT_VAR_PKTLEN 0x40
115 #define PGM_OPT_BIT_PARITY 0x80
117 #define PGM_OPT_LENGTH 0x00
118 #define PGM_OPT_FRAGMENT 0x01
119 #define PGM_OPT_NAK_LIST 0x02
120 #define PGM_OPT_JOIN 0x03
121 #define PGM_OPT_NAK_BO_IVL 0x04
122 #define PGM_OPT_NAK_BO_RNG 0x05
124 #define PGM_OPT_REDIRECT 0x07
125 #define PGM_OPT_PARITY_PRM 0x08
126 #define PGM_OPT_PARITY_GRP 0x09
127 #define PGM_OPT_CURR_TGSIZE 0x0A
128 #define PGM_OPT_NBR_UNREACH 0x0B
129 #define PGM_OPT_PATH_NLA 0x0C
131 #define PGM_OPT_SYN 0x0D
132 #define PGM_OPT_FIN 0x0E
133 #define PGM_OPT_RST 0x0F
134 #define PGM_OPT_CR 0x10
135 #define PGM_OPT_CRQST 0x11
137 #define PGM_OPT_MASK 0x7f
139 #define PGM_OPT_END 0x80 /* end of options marker */
141 #define PGM_MIN_OPT_LEN 4
149 pgm_print(register const u_char *bp, register u_int length,
150 register const u_char *bp2)
152 register const struct pgm_header *pgm;
153 register const struct ip *ip;
155 u_int16_t sport, dport;
160 char nla_buf[INET6_ADDRSTRLEN];
161 register const struct ip6_hdr *ip6;
163 char nla_buf[INET_ADDRSTRLEN];
165 u_int8_t opt_type, opt_len, flags1, flags2;
166 u_int32_t seq, opts_len, len, offset;
168 pgm = (struct pgm_header *)bp;
169 ip = (struct ip *)bp2;
172 ip6 = (struct ip6_hdr *)bp2;
177 (void)printf("Can't handle IPv6");
182 if (!TTEST(pgm->pgm_dport)) {
185 (void)printf("%s > %s: [|pgm]",
186 ip6addr_string(&ip6->ip6_src),
187 ip6addr_string(&ip6->ip6_dst));
192 (void)printf("%s > %s: [|pgm]",
193 ipaddr_string(&ip->ip_src),
194 ipaddr_string(&ip->ip_dst));
199 sport = EXTRACT_16BITS(&pgm->pgm_sport);
200 dport = EXTRACT_16BITS(&pgm->pgm_dport);
204 if (ip6->ip6_nxt == IPPROTO_PGM) {
205 (void)printf("%s.%s > %s.%s: ",
206 ip6addr_string(&ip6->ip6_src),
207 tcpport_string(sport),
208 ip6addr_string(&ip6->ip6_dst),
209 tcpport_string(dport));
211 (void)printf("%s > %s: ",
212 tcpport_string(sport), tcpport_string(dport));
217 if (ip->ip_p == IPPROTO_PGM) {
218 (void)printf("%s.%s > %s.%s: ",
219 ipaddr_string(&ip->ip_src),
220 tcpport_string(sport),
221 ipaddr_string(&ip->ip_dst),
222 tcpport_string(dport));
224 (void)printf("%s > %s: ",
225 tcpport_string(sport), tcpport_string(dport));
231 (void)printf("PGM, length %u", pgm->pgm_length);
236 if (length > pgm->pgm_length)
237 length = pgm->pgm_length;
239 (void)printf(" 0x%02x%02x%02x%02x%02x%02x ",
246 switch (pgm->pgm_type) {
250 spm = (struct pgm_spm *)(pgm + 1);
253 switch (EXTRACT_16BITS(&spm->pgms_nla_afi)) {
255 addr_size = sizeof(struct in_addr);
260 addr_size = sizeof(struct in6_addr);
268 bp = (u_char *) (spm + 1);
269 TCHECK2(*bp, addr_size);
273 inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
274 (void)printf("SPM seq %u trail %u lead %u nla %s",
275 EXTRACT_32BITS(&spm->pgms_seq),
276 EXTRACT_32BITS(&spm->pgms_trailseq),
277 EXTRACT_32BITS(&spm->pgms_leadseq),
283 struct pgm_poll *poll;
285 poll = (struct pgm_poll *)(pgm + 1);
287 (void)printf("POLL seq %u round %u",
288 EXTRACT_32BITS(&poll->pgmp_seq),
289 EXTRACT_16BITS(&poll->pgmp_round));
290 bp = (u_char *) (poll + 1);
294 struct pgm_polr *polr;
295 u_int32_t ivl, rnd, mask;
297 polr = (struct pgm_polr *)(pgm + 1);
300 switch (EXTRACT_16BITS(&polr->pgmp_nla_afi)) {
302 addr_size = sizeof(struct in_addr);
307 addr_size = sizeof(struct in6_addr);
315 bp = (u_char *) (polr + 1);
316 TCHECK2(*bp, addr_size);
320 inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
322 TCHECK2(*bp, sizeof(u_int32_t));
323 ivl = EXTRACT_32BITS(bp);
324 bp += sizeof(u_int32_t);
326 TCHECK2(*bp, sizeof(u_int32_t));
327 rnd = EXTRACT_32BITS(bp);
328 bp += sizeof(u_int32_t);
330 TCHECK2(*bp, sizeof(u_int32_t));
331 mask = EXTRACT_32BITS(bp);
332 bp += sizeof(u_int32_t);
334 (void)printf("POLR seq %u round %u nla %s ivl %u rnd 0x%08x "
335 "mask 0x%08x", EXTRACT_32BITS(&polr->pgmp_seq),
336 EXTRACT_16BITS(&polr->pgmp_round), nla_buf, ivl, rnd, mask);
340 struct pgm_data *odata;
342 odata = (struct pgm_data *)(pgm + 1);
344 (void)printf("ODATA trail %u seq %u",
345 EXTRACT_32BITS(&odata->pgmd_trailseq),
346 EXTRACT_32BITS(&odata->pgmd_seq));
347 bp = (u_char *) (odata + 1);
352 struct pgm_data *rdata;
354 rdata = (struct pgm_data *)(pgm + 1);
356 (void)printf("RDATA trail %u seq %u",
357 EXTRACT_32BITS(&rdata->pgmd_trailseq),
358 EXTRACT_32BITS(&rdata->pgmd_seq));
359 bp = (u_char *) (rdata + 1);
367 const void *source, *group;
368 int source_af, group_af;
370 char source_buf[INET6_ADDRSTRLEN], group_buf[INET6_ADDRSTRLEN];
372 char source_buf[INET_ADDRSTRLEN], group_buf[INET_ADDRSTRLEN];
375 nak = (struct pgm_nak *)(pgm + 1);
379 * Skip past the source, saving info along the way
380 * and stopping if we don't have enough.
382 switch (EXTRACT_16BITS(&nak->pgmn_source_afi)) {
384 addr_size = sizeof(struct in_addr);
389 addr_size = sizeof(struct in6_addr);
390 source_af = AF_INET6;
397 bp = (u_char *) (nak + 1);
398 TCHECK2(*bp, addr_size);
403 * Skip past the group, saving info along the way
404 * and stopping if we don't have enough.
406 switch (EXTRACT_16BITS(bp)) {
408 addr_size = sizeof(struct in_addr);
413 addr_size = sizeof(struct in6_addr);
421 bp += (2 * sizeof(u_int16_t));
422 TCHECK2(*bp, addr_size);
427 * Options decoding can go here.
429 inet_ntop(source_af, source, source_buf, sizeof(source_buf));
430 inet_ntop(group_af, group, group_buf, sizeof(group_buf));
431 switch (pgm->pgm_type) {
433 (void)printf("NAK ");
436 (void)printf("NNAK ");
439 (void)printf("NCF ");
444 (void)printf("(%s -> %s), seq %u",
445 source_buf, group_buf, EXTRACT_32BITS(&nak->pgmn_seq));
450 (void)printf("SPMR");
454 (void)printf("UNKNOWN type %0x02x", pgm->pgm_type);
458 if (pgm->pgm_options & PGM_OPT_BIT_PRESENT) {
461 * make sure there's enough for the first option header
463 if (!TTEST2(*bp, PGM_MIN_OPT_LEN)) {
464 (void)printf("[|OPT]");
469 * That option header MUST be an OPT_LENGTH option
470 * (see the first paragraph of section 9.1 in RFC 3208).
473 if ((opt_type & PGM_OPT_MASK) != PGM_OPT_LENGTH) {
474 (void)printf("[First option bad, should be PGM_OPT_LENGTH, is %u]", opt_type & PGM_OPT_MASK);
479 (void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len);
482 opts_len = EXTRACT_16BITS(bp);
484 (void)printf("[Bad total option length %u < 4]", opts_len);
487 bp += sizeof(u_int16_t);
488 (void)printf(" OPTS LEN %d", opts_len);
492 if (opts_len < PGM_MIN_OPT_LEN) {
493 (void)printf("[Total option length leaves no room for final option]");
498 if (opt_len < PGM_MIN_OPT_LEN) {
499 (void)printf("[Bad option, length %u < %u]", opt_len,
503 if (opts_len < opt_len) {
504 (void)printf("[Total option length leaves no room for final option]");
507 if (!TTEST2(*bp, opt_len - 2)) {
508 (void)printf(" [|OPT]");
512 switch (opt_type & PGM_OPT_MASK) {
515 (void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len);
518 (void)printf(" OPTS LEN (extra?) %d", EXTRACT_16BITS(bp));
519 bp += sizeof(u_int16_t);
523 case PGM_OPT_FRAGMENT:
525 (void)printf("[Bad OPT_FRAGMENT option, length %u != 16]", opt_len);
530 seq = EXTRACT_32BITS(bp);
531 bp += sizeof(u_int32_t);
532 offset = EXTRACT_32BITS(bp);
533 bp += sizeof(u_int32_t);
534 len = EXTRACT_32BITS(bp);
535 bp += sizeof(u_int32_t);
536 (void)printf(" FRAG seq %u off %u len %u", seq, offset, len);
540 case PGM_OPT_NAK_LIST:
543 opt_len -= sizeof(u_int32_t); /* option header */
544 (void)printf(" NAK LIST");
546 if (opt_len < sizeof(u_int32_t)) {
547 (void)printf("[Option length not a multiple of 4]");
550 TCHECK2(*bp, sizeof(u_int32_t));
551 (void)printf(" %u", EXTRACT_32BITS(bp));
552 bp += sizeof(u_int32_t);
553 opt_len -= sizeof(u_int32_t);
554 opts_len -= sizeof(u_int32_t);
560 (void)printf("[Bad OPT_JOIN option, length %u != 8]", opt_len);
565 seq = EXTRACT_32BITS(bp);
566 bp += sizeof(u_int32_t);
567 (void)printf(" JOIN %u", seq);
571 case PGM_OPT_NAK_BO_IVL:
573 (void)printf("[Bad OPT_NAK_BO_IVL option, length %u != 12]", opt_len);
578 offset = EXTRACT_32BITS(bp);
579 bp += sizeof(u_int32_t);
580 seq = EXTRACT_32BITS(bp);
581 bp += sizeof(u_int32_t);
582 (void)printf(" BACKOFF ivl %u ivlseq %u", offset, seq);
586 case PGM_OPT_NAK_BO_RNG:
588 (void)printf("[Bad OPT_NAK_BO_RNG option, length %u != 12]", opt_len);
593 offset = EXTRACT_32BITS(bp);
594 bp += sizeof(u_int32_t);
595 seq = EXTRACT_32BITS(bp);
596 bp += sizeof(u_int32_t);
597 (void)printf(" BACKOFF max %u min %u", offset, seq);
601 case PGM_OPT_REDIRECT:
604 switch (EXTRACT_16BITS(bp)) {
606 addr_size = sizeof(struct in_addr);
611 addr_size = sizeof(struct in6_addr);
619 bp += (2 * sizeof(u_int16_t));
620 if (opt_len != 4 + addr_size) {
621 (void)printf("[Bad OPT_REDIRECT option, length %u != 4 + address size]", opt_len);
624 TCHECK2(*bp, addr_size);
628 inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf));
629 (void)printf(" REDIRECT %s", (char *)nla);
630 opts_len -= 4 + addr_size;
633 case PGM_OPT_PARITY_PRM:
635 (void)printf("[Bad OPT_PARITY_PRM option, length %u != 8]", opt_len);
640 len = EXTRACT_32BITS(bp);
641 bp += sizeof(u_int32_t);
642 (void)printf(" PARITY MAXTGS %u", len);
646 case PGM_OPT_PARITY_GRP:
648 (void)printf("[Bad OPT_PARITY_GRP option, length %u != 8]", opt_len);
653 seq = EXTRACT_32BITS(bp);
654 bp += sizeof(u_int32_t);
655 (void)printf(" PARITY GROUP %u", seq);
659 case PGM_OPT_CURR_TGSIZE:
661 (void)printf("[Bad OPT_CURR_TGSIZE option, length %u != 8]", opt_len);
666 len = EXTRACT_32BITS(bp);
667 bp += sizeof(u_int32_t);
668 (void)printf(" PARITY ATGS %u", len);
672 case PGM_OPT_NBR_UNREACH:
674 (void)printf("[Bad OPT_NBR_UNREACH option, length %u != 4]", opt_len);
679 (void)printf(" NBR_UNREACH");
683 case PGM_OPT_PATH_NLA:
684 (void)printf(" PATH_NLA [%d]", opt_len);
691 (void)printf("[Bad OPT_SYN option, length %u != 4]", opt_len);
696 (void)printf(" SYN");
702 (void)printf("[Bad OPT_FIN option, length %u != 4]", opt_len);
707 (void)printf(" FIN");
713 (void)printf("[Bad OPT_RST option, length %u != 4]", opt_len);
718 (void)printf(" RST");
730 (void)printf("[Bad OPT_CRQST option, length %u != 4]", opt_len);
735 (void)printf(" CRQST");
740 (void)printf(" OPT_%02X [%d] ", opt_type, opt_len);
746 if (opt_type & PGM_OPT_END)
751 (void)printf(" [%u]", EXTRACT_16BITS(&pgm->pgm_length));
756 fputs("[|pgm]", stdout);