X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/9ebb84d5b7d3ba344584ae2ead7eed413305909b..refs/heads/master:/print-pgm.c diff --git a/print-pgm.c b/print-pgm.c index 73a42914..d9c1e2c2 100644 --- a/print-pgm.c +++ b/print-pgm.c @@ -13,100 +13,130 @@ * Original code by Andy Heffernan (ahh@juniper.net) */ -#ifndef lint -static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-pgm.c,v 1.5 2005-06-07 22:05:58 guy Exp $"; -#endif +/* \summary: Pragmatic General Multicast (PGM) printer */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +/* specification: RFC 3208 -#include + Plus https://dl.acm.org/doi/pdf/10.1145/347057.347390 for PGMCC, + whence the ACK packet type comes; there are some I-Ds for PGMCC, + draft-ietf-rmt-bb-pgmcc-00 through draft-ietf-rmt-bb-pgmcc-03, + but none of them give any description of the packet-level + changes to PGM, unlike the paper in question, which merely gives + an *insufficient* description of said changes. In particular, + it doesn't indicate what the packet type code for ACK is. -#include -#include -#include + Luigi Rizzo's PGMCC code for FreeBSD, at -#include "interface.h" + https://web.archive.org/web/20020302084503/http://info.iet.unipi.it/~luigi/pgm-code/ + + uses 0x0b (11) for ACK. + + A capture file attached to + + https://gitlab.com/wireshark/wireshark/-/issues/4798 + + has packets that use 0x0d for ACK, as does the Wireshark dissector + for PGM, and as does OpenPGM at https://github.com/steve-o/openpgm. + It may be that some proprietary PGMCC implementations, such as + SmartPGM, do so as well. + + We use *both*, treating *either one* as a PGMCC ACK, pending + more information, such as an answer to + + https://github.com/steve-o/openpgm/issues/75. + + */ + +#include + +#include "netdissect-stdinc.h" + +#define ND_LONGJMP_FROM_TCHECK +#include "netdissect.h" #include "extract.h" #include "addrtoname.h" +#include "addrtostr.h" #include "ip.h" -#ifdef INET6 #include "ip6.h" -#endif #include "ipproto.h" +#include "af.h" /* * PGM header (RFC 3208) */ struct pgm_header { - u_int16_t pgm_sport; - u_int16_t pgm_dport; - u_int8_t pgm_type; - u_int8_t pgm_options; - u_int16_t pgm_sum; - u_int8_t pgm_gsid[6]; - u_int16_t pgm_length; + nd_uint16_t pgm_sport; + nd_uint16_t pgm_dport; + nd_uint8_t pgm_type; + nd_uint8_t pgm_options; + nd_uint16_t pgm_sum; + nd_byte pgm_gsid[6]; + nd_uint16_t pgm_length; }; struct pgm_spm { - u_int32_t pgms_seq; - u_int32_t pgms_trailseq; - u_int32_t pgms_leadseq; - u_int16_t pgms_nla_afi; - u_int16_t pgms_reserved; - /* ... u_int8_t pgms_nla[0]; */ + nd_uint32_t pgms_seq; + nd_uint32_t pgms_trailseq; + nd_uint32_t pgms_leadseq; + nd_uint16_t pgms_nla_afi; + nd_uint16_t pgms_reserved; + /* ... uint8_t pgms_nla[0]; */ /* ... options */ }; struct pgm_nak { - u_int32_t pgmn_seq; - u_int16_t pgmn_source_afi; - u_int16_t pgmn_reserved; - /* ... u_int8_t pgmn_source[0]; */ - /* ... u_int16_t pgmn_group_afi */ - /* ... u_int16_t pgmn_reserved2; */ - /* ... u_int8_t pgmn_group[0]; */ + nd_uint32_t pgmn_seq; + nd_uint16_t pgmn_source_afi; + nd_uint16_t pgmn_reserved; + /* ... uint8_t pgmn_source[0]; */ + /* ... uint16_t pgmn_group_afi */ + /* ... uint16_t pgmn_reserved2; */ + /* ... uint8_t pgmn_group[0]; */ + /* ... options */ +}; + +struct pgm_ack { + nd_uint32_t pgma_rx_max_seq; + nd_uint32_t pgma_bitmap; /* ... options */ }; struct pgm_poll { - u_int32_t pgmp_seq; - u_int16_t pgmp_round; - u_int16_t pgmp_reserved; + nd_uint32_t pgmp_seq; + nd_uint16_t pgmp_round; + nd_uint16_t pgmp_subtype; + nd_uint16_t pgmp_nla_afi; + nd_uint16_t pgmp_reserved; + /* ... uint8_t pgmp_nla[0]; */ /* ... options */ }; struct pgm_polr { - u_int32_t pgmp_seq; - u_int16_t pgmp_round; - u_int16_t pgmp_subtype; - u_int16_t pgmp_nla_afi; - u_int16_t pgmp_reserved; - /* ... u_int8_t pgmp_nla[0]; */ + nd_uint32_t pgmp_seq; + nd_uint16_t pgmp_round; + nd_uint16_t pgmp_reserved; /* ... options */ }; struct pgm_data { - u_int32_t pgmd_seq; - u_int32_t pgmd_trailseq; + nd_uint32_t pgmd_seq; + nd_uint32_t pgmd_trailseq; /* ... options */ }; typedef enum _pgm_type { - PGM_SPM = 0, /* source path message */ - PGM_POLL = 1, /* POLL Request */ - PGM_POLR = 2, /* POLL Response */ - PGM_ODATA = 4, /* original data */ - PGM_RDATA = 5, /* repair data */ - PGM_NAK = 8, /* NAK */ - PGM_NULLNAK = 9, /* Null NAK */ - PGM_NCF = 10, /* NAK Confirmation */ - PGM_ACK = 11, /* ACK for congestion control */ - PGM_SPMR = 12, /* SPM request */ - PGM_MAX = 255 + PGM_SPM = 0x00, /* source path message */ + PGM_POLL = 0x01, /* POLL Request */ + PGM_POLR = 0x02, /* POLL Response */ + PGM_ODATA = 0x04, /* original data */ + PGM_RDATA = 0x05, /* repair data */ + PGM_NAK = 0x08, /* NAK */ + PGM_NULLNAK = 0x09, /* Null NAK */ + PGM_NCF = 0x0a, /* NAK Confirmation */ + PGM_ACK = 0x0b, /* ACK for congestion control? */ + PGM_SPMR = 0x0c, /* SPM request */ + PGM_ACK2 = 0x0d, /* Also ACK for congestion control? */ } pgm_type; #define PGM_OPT_BIT_PRESENT 0x01 @@ -133,612 +163,673 @@ typedef enum _pgm_type { #define PGM_OPT_RST 0x0F #define PGM_OPT_CR 0x10 #define PGM_OPT_CRQST 0x11 - + +#define PGM_OPT_PGMCC_DATA 0x12 +#define PGM_OPT_PGMCC_FEEDBACK 0x13 + #define PGM_OPT_MASK 0x7f #define PGM_OPT_END 0x80 /* end of options marker */ #define PGM_MIN_OPT_LEN 4 -#ifndef AFI_IP -#define AFI_IP 1 -#define AFI_IP6 2 -#endif - void -pgm_print(register const u_char *bp, register u_int length, - register const u_char *bp2) +pgm_print(netdissect_options *ndo, + const u_char *bp, u_int length, + const u_char *bp2) { - register const struct pgm_header *pgm; - register const struct ip *ip; - register char ch; - u_int16_t sport, dport; - int addr_size; - const void *nla; - int nla_af; -#ifdef INET6 + const struct pgm_header *pgm; + const struct ip *ip; + uint8_t pgm_type_val; + uint16_t sport, dport; + u_int nla_afnum; char nla_buf[INET6_ADDRSTRLEN]; - register const struct ip6_hdr *ip6; -#else - char nla_buf[INET_ADDRSTRLEN]; -#endif - u_int8_t opt_type, opt_len, flags1, flags2; - u_int32_t seq, opts_len, len, offset; - - pgm = (struct pgm_header *)bp; - ip = (struct ip *)bp2; -#ifdef INET6 + const struct ip6_hdr *ip6; + uint8_t opt_type, opt_len; + uint32_t seq, opts_len, len, offset; + + ndo->ndo_protocol = "pgm"; + pgm = (const struct pgm_header *)bp; + ip = (const struct ip *)bp2; if (IP_V(ip) == 6) - ip6 = (struct ip6_hdr *)bp2; + ip6 = (const struct ip6_hdr *)bp2; else ip6 = NULL; -#else /* INET6 */ - if (IP_V(ip) == 6) { - (void)printf("Can't handle IPv6"); - return; - } -#endif /* INET6 */ - ch = '\0'; - if (!TTEST(pgm->pgm_dport)) { -#ifdef INET6 + if (!ND_TTEST_2(pgm->pgm_dport)) { if (ip6) { - (void)printf("%s > %s: [|pgm]", - ip6addr_string(&ip6->ip6_src), - ip6addr_string(&ip6->ip6_dst)); - return; - } else -#endif /* INET6 */ - { - (void)printf("%s > %s: [|pgm]", - ipaddr_string(&ip->ip_src), - ipaddr_string(&ip->ip_dst)); - return; + ND_PRINT("%s > %s:", + GET_IP6ADDR_STRING(ip6->ip6_src), + GET_IP6ADDR_STRING(ip6->ip6_dst)); + } else { + ND_PRINT("%s > %s:", + GET_IPADDR_STRING(ip->ip_src), + GET_IPADDR_STRING(ip->ip_dst)); } + nd_trunc_longjmp(ndo); } - sport = EXTRACT_16BITS(&pgm->pgm_sport); - dport = EXTRACT_16BITS(&pgm->pgm_dport); + sport = GET_BE_U_2(pgm->pgm_sport); + dport = GET_BE_U_2(pgm->pgm_dport); -#ifdef INET6 if (ip6) { - if (ip6->ip6_nxt == IPPROTO_PGM) { - (void)printf("%s.%s > %s.%s: ", - ip6addr_string(&ip6->ip6_src), - tcpport_string(sport), - ip6addr_string(&ip6->ip6_dst), - tcpport_string(dport)); + if (GET_U_1(ip6->ip6_nxt) == IPPROTO_PGM) { + ND_PRINT("%s.%s > %s.%s: ", + GET_IP6ADDR_STRING(ip6->ip6_src), + tcpport_string(ndo, sport), + GET_IP6ADDR_STRING(ip6->ip6_dst), + tcpport_string(ndo, dport)); } else { - (void)printf("%s > %s: ", - tcpport_string(sport), tcpport_string(dport)); + ND_PRINT("%s > %s: ", + tcpport_string(ndo, sport), tcpport_string(ndo, dport)); } - } else -#endif /*INET6*/ - { - if (ip->ip_p == IPPROTO_PGM) { - (void)printf("%s.%s > %s.%s: ", - ipaddr_string(&ip->ip_src), - tcpport_string(sport), - ipaddr_string(&ip->ip_dst), - tcpport_string(dport)); + } else { + if (GET_U_1(ip->ip_p) == IPPROTO_PGM) { + ND_PRINT("%s.%s > %s.%s: ", + GET_IPADDR_STRING(ip->ip_src), + tcpport_string(ndo, sport), + GET_IPADDR_STRING(ip->ip_dst), + tcpport_string(ndo, dport)); } else { - (void)printf("%s > %s: ", - tcpport_string(sport), tcpport_string(dport)); + ND_PRINT("%s > %s: ", + tcpport_string(ndo, sport), tcpport_string(ndo, dport)); } } - TCHECK(*pgm); + ND_TCHECK_SIZE(pgm); - (void)printf("PGM, length %u", pgm->pgm_length); + ND_PRINT("PGM, length %u", GET_BE_U_2(pgm->pgm_length)); - if (!vflag) + if (!ndo->ndo_vflag) return; - if (length > pgm->pgm_length) - length = pgm->pgm_length; - - (void)printf(" 0x%02x%02x%02x%02x%02x%02x ", + pgm_type_val = GET_U_1(pgm->pgm_type); + ND_PRINT(" 0x%02x%02x%02x%02x%02x%02x ", pgm->pgm_gsid[0], pgm->pgm_gsid[1], pgm->pgm_gsid[2], pgm->pgm_gsid[3], pgm->pgm_gsid[4], pgm->pgm_gsid[5]); - switch (pgm->pgm_type) { + bp += sizeof(struct pgm_header); + switch (pgm_type_val) { case PGM_SPM: { - struct pgm_spm *spm; + const struct pgm_spm *spm; - spm = (struct pgm_spm *)(pgm + 1); - TCHECK(*spm); + spm = (const struct pgm_spm *)bp; + ND_TCHECK_SIZE(spm); + bp += sizeof(struct pgm_spm); - switch (EXTRACT_16BITS(&spm->pgms_nla_afi)) { - case AFI_IP: - addr_size = sizeof(struct in_addr); - nla_af = AF_INET; + switch (GET_BE_U_2(spm->pgms_nla_afi)) { + case AFNUM_IP: + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv4); break; -#ifdef INET6 - case AFI_IP6: - addr_size = sizeof(struct in6_addr); - nla_af = AF_INET6; + case AFNUM_IP6: + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv6); break; -#endif default: - goto trunc; - break; + goto invalid; } - bp = (u_char *) (spm + 1); - TCHECK2(*bp, addr_size); - nla = bp; - bp += addr_size; - - inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf)); - (void)printf("SPM seq %u trail %u lead %u nla %s", - EXTRACT_32BITS(&spm->pgms_seq), - EXTRACT_32BITS(&spm->pgms_trailseq), - EXTRACT_32BITS(&spm->pgms_leadseq), - nla_buf); - break; - } - case PGM_POLL: { - struct pgm_poll *poll; - - poll = (struct pgm_poll *)(pgm + 1); - TCHECK(*poll); - (void)printf("POLL seq %u round %u", - EXTRACT_32BITS(&poll->pgmp_seq), - EXTRACT_16BITS(&poll->pgmp_round)); - bp = (u_char *) (poll + 1); + ND_PRINT("SPM seq %u trail %u lead %u nla %s", + GET_BE_U_4(spm->pgms_seq), + GET_BE_U_4(spm->pgms_trailseq), + GET_BE_U_4(spm->pgms_leadseq), + nla_buf); break; } - case PGM_POLR: { - struct pgm_polr *polr; - u_int32_t ivl, rnd, mask; - - polr = (struct pgm_polr *)(pgm + 1); - TCHECK(*polr); - switch (EXTRACT_16BITS(&polr->pgmp_nla_afi)) { - case AFI_IP: - addr_size = sizeof(struct in_addr); - nla_af = AF_INET; + case PGM_POLL: { + const struct pgm_poll *pgm_poll; + uint32_t ivl, rnd, mask; + + pgm_poll = (const struct pgm_poll *)bp; + ND_TCHECK_SIZE(pgm_poll); + bp += sizeof(struct pgm_poll); + + switch (GET_BE_U_2(pgm_poll->pgmp_nla_afi)) { + case AFNUM_IP: + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv4); break; -#ifdef INET6 - case AFI_IP6: - addr_size = sizeof(struct in6_addr); - nla_af = AF_INET6; + case AFNUM_IP6: + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv6); break; -#endif default: - goto trunc; - break; + goto invalid; } - bp = (u_char *) (polr + 1); - TCHECK2(*bp, addr_size); - nla = bp; - bp += addr_size; - inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf)); + ivl = GET_BE_U_4(bp); + bp += sizeof(uint32_t); - TCHECK2(*bp, sizeof(u_int32_t)); - ivl = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); + rnd = GET_BE_U_4(bp); + bp += sizeof(uint32_t); - TCHECK2(*bp, sizeof(u_int32_t)); - rnd = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); + mask = GET_BE_U_4(bp); + bp += sizeof(uint32_t); - TCHECK2(*bp, sizeof(u_int32_t)); - mask = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - - (void)printf("POLR seq %u round %u nla %s ivl %u rnd 0x%08x " - "mask 0x%08x", EXTRACT_32BITS(&polr->pgmp_seq), - EXTRACT_16BITS(&polr->pgmp_round), nla_buf, ivl, rnd, mask); + ND_PRINT("POLL seq %u round %u nla %s ivl %u rnd 0x%08x " + "mask 0x%08x", GET_BE_U_4(pgm_poll->pgmp_seq), + GET_BE_U_2(pgm_poll->pgmp_round), nla_buf, ivl, rnd, + mask); + break; + } + case PGM_POLR: { + const struct pgm_polr *polr_msg; + + polr_msg = (const struct pgm_polr *)bp; + ND_TCHECK_SIZE(polr_msg); + ND_PRINT("POLR seq %u round %u", + GET_BE_U_4(polr_msg->pgmp_seq), + GET_BE_U_2(polr_msg->pgmp_round)); + bp += sizeof(struct pgm_polr); break; } case PGM_ODATA: { - struct pgm_data *odata; - - odata = (struct pgm_data *)(pgm + 1); - TCHECK(*odata); - (void)printf("ODATA trail %u seq %u", - EXTRACT_32BITS(&odata->pgmd_trailseq), - EXTRACT_32BITS(&odata->pgmd_seq)); - bp = (u_char *) (odata + 1); + const struct pgm_data *odata; + + odata = (const struct pgm_data *)bp; + ND_PRINT("ODATA trail %u seq %u", + GET_BE_U_4(odata->pgmd_trailseq), + GET_BE_U_4(odata->pgmd_seq)); + bp += sizeof(struct pgm_data); break; } case PGM_RDATA: { - struct pgm_data *rdata; - - rdata = (struct pgm_data *)(pgm + 1); - TCHECK(*rdata); - (void)printf("RDATA trail %u seq %u", - EXTRACT_32BITS(&rdata->pgmd_trailseq), - EXTRACT_32BITS(&rdata->pgmd_seq)); - bp = (u_char *) (rdata + 1); + const struct pgm_data *rdata; + + rdata = (const struct pgm_data *)bp; + ND_PRINT("RDATA trail %u seq %u", + GET_BE_U_4(rdata->pgmd_trailseq), + GET_BE_U_4(rdata->pgmd_seq)); + bp += sizeof(struct pgm_data); break; } case PGM_NAK: case PGM_NULLNAK: case PGM_NCF: { - struct pgm_nak *nak; - const void *source, *group; - int source_af, group_af; -#ifdef INET6 + const struct pgm_nak *nak; char source_buf[INET6_ADDRSTRLEN], group_buf[INET6_ADDRSTRLEN]; -#else - char source_buf[INET_ADDRSTRLEN], group_buf[INET_ADDRSTRLEN]; -#endif - nak = (struct pgm_nak *)(pgm + 1); - TCHECK(*nak); + nak = (const struct pgm_nak *)bp; + ND_TCHECK_SIZE(nak); + bp += sizeof(struct pgm_nak); /* * Skip past the source, saving info along the way * and stopping if we don't have enough. */ - switch (EXTRACT_16BITS(&nak->pgmn_source_afi)) { - case AFI_IP: - addr_size = sizeof(struct in_addr); - source_af = AF_INET; + switch (GET_BE_U_2(nak->pgmn_source_afi)) { + case AFNUM_IP: + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, source_buf, sizeof(source_buf)); + bp += sizeof(nd_ipv4); break; -#ifdef INET6 - case AFI_IP6: - addr_size = sizeof(struct in6_addr); - source_af = AF_INET6; + case AFNUM_IP6: + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, source_buf, sizeof(source_buf)); + bp += sizeof(nd_ipv6); break; -#endif default: - goto trunc; - break; + goto invalid; } - bp = (u_char *) (nak + 1); - TCHECK2(*bp, addr_size); - source = bp; - bp += addr_size; /* * Skip past the group, saving info along the way * and stopping if we don't have enough. */ - switch (EXTRACT_16BITS(bp)) { - case AFI_IP: - addr_size = sizeof(struct in_addr); - group_af = AF_INET; + bp += (2 * sizeof(uint16_t)); + switch (GET_BE_U_2(bp)) { + case AFNUM_IP: + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, group_buf, sizeof(group_buf)); + bp += sizeof(nd_ipv4); break; -#ifdef INET6 - case AFI_IP6: - addr_size = sizeof(struct in6_addr); - group_af = AF_INET6; + case AFNUM_IP6: + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, group_buf, sizeof(group_buf)); + bp += sizeof(nd_ipv6); break; -#endif default: - goto trunc; - break; + goto invalid; } - bp += (2 * sizeof(u_int16_t)); - TCHECK2(*bp, addr_size); - group = bp; - bp += addr_size; /* * Options decoding can go here. */ - inet_ntop(source_af, source, source_buf, sizeof(source_buf)); - inet_ntop(group_af, group, group_buf, sizeof(group_buf)); - switch (pgm->pgm_type) { + switch (pgm_type_val) { case PGM_NAK: - (void)printf("NAK "); + ND_PRINT("NAK "); break; case PGM_NULLNAK: - (void)printf("NNAK "); + ND_PRINT("NNAK "); break; case PGM_NCF: - (void)printf("NCF "); + ND_PRINT("NCF "); break; default: break; } - (void)printf("(%s -> %s), seq %u", - source_buf, group_buf, EXTRACT_32BITS(&nak->pgmn_seq)); + ND_PRINT("(%s -> %s), seq %u", + source_buf, group_buf, GET_BE_U_4(nak->pgmn_seq)); + break; + } + + case PGM_ACK: + case PGM_ACK2: { + const struct pgm_ack *ack; + + ack = (const struct pgm_ack *)bp; + ND_TCHECK_SIZE(ack); + ND_PRINT("ACK seq %u", + GET_BE_U_4(ack->pgma_rx_max_seq)); + bp += sizeof(struct pgm_ack); break; } case PGM_SPMR: - (void)printf("SPMR"); + ND_PRINT("SPMR"); break; default: - (void)printf("UNKNOWN type %0x02x", pgm->pgm_type); + ND_PRINT("UNKNOWN type 0x%02x", pgm_type_val); break; } - if (pgm->pgm_options & PGM_OPT_BIT_PRESENT) { + if (GET_U_1(pgm->pgm_options) & PGM_OPT_BIT_PRESENT) { /* * make sure there's enough for the first option header */ - if (!TTEST2(*bp, PGM_MIN_OPT_LEN)) { - (void)printf("[|OPT]"); - return; - } + ND_TCHECK_LEN(bp, PGM_MIN_OPT_LEN); /* * That option header MUST be an OPT_LENGTH option * (see the first paragraph of section 9.1 in RFC 3208). */ - opt_type = *bp++; + opt_type = GET_U_1(bp); + bp++; if ((opt_type & PGM_OPT_MASK) != PGM_OPT_LENGTH) { - (void)printf("[First option bad, should be PGM_OPT_LENGTH, is %u]", opt_type & PGM_OPT_MASK); + ND_PRINT("[First option bad, should be PGM_OPT_LENGTH, is %u]", opt_type & PGM_OPT_MASK); return; } - opt_len = *bp++; + opt_len = GET_U_1(bp); + bp++; if (opt_len != 4) { - (void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len); + ND_PRINT("[Bad OPT_LENGTH option, length %u != 4]", opt_len); return; } - opts_len = EXTRACT_16BITS(bp); + opts_len = GET_BE_U_2(bp); + bp += sizeof(uint16_t); if (opts_len < 4) { - (void)printf("[Bad total option length %u < 4]", opts_len); + ND_PRINT("[Bad total option length %u < 4]", opts_len); return; } - bp += sizeof(u_int16_t); - (void)printf(" OPTS LEN %d", opts_len); + ND_PRINT(" OPTS LEN %u", opts_len); opts_len -= 4; while (opts_len) { if (opts_len < PGM_MIN_OPT_LEN) { - (void)printf("[Total option length leaves no room for final option]"); + ND_PRINT("[Total option length leaves no room for final option]"); return; } - opt_type = *bp++; - opt_len = *bp++; + opt_type = GET_U_1(bp); + bp++; + opt_len = GET_U_1(bp); + bp++; if (opt_len < PGM_MIN_OPT_LEN) { - (void)printf("[Bad option, length %u < %u]", opt_len, + ND_PRINT("[Bad option, length %u < %u]", opt_len, PGM_MIN_OPT_LEN); break; } if (opts_len < opt_len) { - (void)printf("[Total option length leaves no room for final option]"); + ND_PRINT("[Total option length leaves no room for final option]"); return; } - if (!TTEST2(*bp, opt_len - 2)) { - (void)printf(" [|OPT]"); - return; - } + ND_TCHECK_LEN(bp, opt_len - 2); switch (opt_type & PGM_OPT_MASK) { case PGM_OPT_LENGTH: - if (opt_len != 4) { - (void)printf("[Bad OPT_LENGTH option, length %u != 4]", opt_len); +#define PGM_OPT_LENGTH_LEN (2+2) + if (opt_len != PGM_OPT_LENGTH_LEN) { + ND_PRINT("[Bad OPT_LENGTH option, length %u != %u]", + opt_len, PGM_OPT_LENGTH_LEN); return; } - (void)printf(" OPTS LEN (extra?) %d", EXTRACT_16BITS(bp)); - bp += sizeof(u_int16_t); - opts_len -= 4; + ND_PRINT(" OPTS LEN (extra?) %u", GET_BE_U_2(bp)); + bp += 2; + opts_len -= PGM_OPT_LENGTH_LEN; break; case PGM_OPT_FRAGMENT: - if (opt_len != 16) { - (void)printf("[Bad OPT_FRAGMENT option, length %u != 16]", opt_len); +#define PGM_OPT_FRAGMENT_LEN (2+2+4+4+4) + if (opt_len != PGM_OPT_FRAGMENT_LEN) { + ND_PRINT("[Bad OPT_FRAGMENT option, length %u != %u]", + opt_len, PGM_OPT_FRAGMENT_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - seq = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - offset = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - len = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" FRAG seq %u off %u len %u", seq, offset, len); - opts_len -= 16; + bp += 2; + seq = GET_BE_U_4(bp); + bp += 4; + offset = GET_BE_U_4(bp); + bp += 4; + len = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" FRAG seq %u off %u len %u", seq, offset, len); + opts_len -= PGM_OPT_FRAGMENT_LEN; break; case PGM_OPT_NAK_LIST: - flags1 = *bp++; - flags2 = *bp++; - opt_len -= sizeof(u_int32_t); /* option header */ - (void)printf(" NAK LIST"); + bp += 2; + opt_len -= 4; /* option header */ + ND_PRINT(" NAK LIST"); while (opt_len) { - if (opt_len < sizeof(u_int32_t)) { - (void)printf("[Option length not a multiple of 4]"); + if (opt_len < 4) { + ND_PRINT("[Option length not a multiple of 4]"); return; } - TCHECK2(*bp, sizeof(u_int32_t)); - (void)printf(" %u", EXTRACT_32BITS(bp)); - bp += sizeof(u_int32_t); - opt_len -= sizeof(u_int32_t); - opts_len -= sizeof(u_int32_t); + ND_PRINT(" %u", GET_BE_U_4(bp)); + bp += 4; + opt_len -= 4; + opts_len -= 4; } break; case PGM_OPT_JOIN: - if (opt_len != 8) { - (void)printf("[Bad OPT_JOIN option, length %u != 8]", opt_len); +#define PGM_OPT_JOIN_LEN (2+2+4) + if (opt_len != PGM_OPT_JOIN_LEN) { + ND_PRINT("[Bad OPT_JOIN option, length %u != %u]", + opt_len, PGM_OPT_JOIN_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - seq = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" JOIN %u", seq); - opts_len -= 8; + bp += 2; + seq = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" JOIN %u", seq); + opts_len -= PGM_OPT_JOIN_LEN; break; case PGM_OPT_NAK_BO_IVL: - if (opt_len != 12) { - (void)printf("[Bad OPT_NAK_BO_IVL option, length %u != 12]", opt_len); +#define PGM_OPT_NAK_BO_IVL_LEN (2+2+4+4) + if (opt_len != PGM_OPT_NAK_BO_IVL_LEN) { + ND_PRINT("[Bad OPT_NAK_BO_IVL option, length %u != %u]", + opt_len, PGM_OPT_NAK_BO_IVL_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - offset = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - seq = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" BACKOFF ivl %u ivlseq %u", offset, seq); - opts_len -= 12; + bp += 2; + offset = GET_BE_U_4(bp); + bp += 4; + seq = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" BACKOFF ivl %u ivlseq %u", offset, seq); + opts_len -= PGM_OPT_NAK_BO_IVL_LEN; break; case PGM_OPT_NAK_BO_RNG: - if (opt_len != 12) { - (void)printf("[Bad OPT_NAK_BO_RNG option, length %u != 12]", opt_len); +#define PGM_OPT_NAK_BO_RNG_LEN (2+2+4+4) + if (opt_len != PGM_OPT_NAK_BO_RNG_LEN) { + ND_PRINT("[Bad OPT_NAK_BO_RNG option, length %u != %u]", + opt_len, PGM_OPT_NAK_BO_RNG_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - offset = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - seq = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" BACKOFF max %u min %u", offset, seq); - opts_len -= 12; + bp += 2; + offset = GET_BE_U_4(bp); + bp += 4; + seq = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" BACKOFF max %u min %u", offset, seq); + opts_len -= PGM_OPT_NAK_BO_RNG_LEN; break; case PGM_OPT_REDIRECT: - flags1 = *bp++; - flags2 = *bp++; - switch (EXTRACT_16BITS(bp)) { - case AFI_IP: - addr_size = sizeof(struct in_addr); - nla_af = AF_INET; +#define PGM_OPT_REDIRECT_FIXED_LEN (2+2+2+2) + if (opt_len < PGM_OPT_REDIRECT_FIXED_LEN) { + ND_PRINT("[Bad OPT_REDIRECT option, length %u < %u]", + opt_len, PGM_OPT_REDIRECT_FIXED_LEN); + return; + } + bp += 2; + nla_afnum = GET_BE_U_2(bp); + bp += 2+2; + switch (nla_afnum) { + case AFNUM_IP: + if (opt_len != PGM_OPT_REDIRECT_FIXED_LEN + sizeof(nd_ipv4)) { + ND_PRINT("[Bad OPT_REDIRECT option, length %u != %u + address size]", + opt_len, PGM_OPT_REDIRECT_FIXED_LEN); + return; + } + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv4); + opts_len -= PGM_OPT_REDIRECT_FIXED_LEN + sizeof(nd_ipv4); break; -#ifdef INET6 - case AFI_IP6: - addr_size = sizeof(struct in6_addr); - nla_af = AF_INET6; + case AFNUM_IP6: + if (opt_len != PGM_OPT_REDIRECT_FIXED_LEN + sizeof(nd_ipv6)) { + ND_PRINT("[Bad OPT_REDIRECT option, length %u != %u + address size]", + opt_len, PGM_OPT_REDIRECT_FIXED_LEN); + return; + } + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv6); + opts_len -= PGM_OPT_REDIRECT_FIXED_LEN + sizeof(nd_ipv6); break; -#endif default: - goto trunc; - break; - } - bp += (2 * sizeof(u_int16_t)); - if (opt_len != 4 + addr_size) { - (void)printf("[Bad OPT_REDIRECT option, length %u != 4 + address size]", opt_len); - return; + goto invalid; } - TCHECK2(*bp, addr_size); - nla = bp; - bp += addr_size; - inet_ntop(nla_af, nla, nla_buf, sizeof(nla_buf)); - (void)printf(" REDIRECT %s", (char *)nla); - opts_len -= 4 + addr_size; + ND_PRINT(" REDIRECT %s", nla_buf); break; case PGM_OPT_PARITY_PRM: - if (opt_len != 8) { - (void)printf("[Bad OPT_PARITY_PRM option, length %u != 8]", opt_len); +#define PGM_OPT_PARITY_PRM_LEN (2+2+4) + if (opt_len != PGM_OPT_PARITY_PRM_LEN) { + ND_PRINT("[Bad OPT_PARITY_PRM option, length %u != %u]", + opt_len, PGM_OPT_PARITY_PRM_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - len = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" PARITY MAXTGS %u", len); - opts_len -= 8; + bp += 2; + len = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" PARITY MAXTGS %u", len); + opts_len -= PGM_OPT_PARITY_PRM_LEN; break; case PGM_OPT_PARITY_GRP: - if (opt_len != 8) { - (void)printf("[Bad OPT_PARITY_GRP option, length %u != 8]", opt_len); +#define PGM_OPT_PARITY_GRP_LEN (2+2+4) + if (opt_len != PGM_OPT_PARITY_GRP_LEN) { + ND_PRINT("[Bad OPT_PARITY_GRP option, length %u != %u]", + opt_len, PGM_OPT_PARITY_GRP_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - seq = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" PARITY GROUP %u", seq); - opts_len -= 8; + bp += 2; + seq = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" PARITY GROUP %u", seq); + opts_len -= PGM_OPT_PARITY_GRP_LEN; break; case PGM_OPT_CURR_TGSIZE: - if (opt_len != 8) { - (void)printf("[Bad OPT_CURR_TGSIZE option, length %u != 8]", opt_len); +#define PGM_OPT_CURR_TGSIZE_LEN (2+2+4) + if (opt_len != PGM_OPT_CURR_TGSIZE_LEN) { + ND_PRINT("[Bad OPT_CURR_TGSIZE option, length %u != %u]", + opt_len, PGM_OPT_CURR_TGSIZE_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - len = EXTRACT_32BITS(bp); - bp += sizeof(u_int32_t); - (void)printf(" PARITY ATGS %u", len); - opts_len -= 8; + bp += 2; + len = GET_BE_U_4(bp); + bp += 4; + ND_PRINT(" PARITY ATGS %u", len); + opts_len -= PGM_OPT_CURR_TGSIZE_LEN; break; case PGM_OPT_NBR_UNREACH: - if (opt_len != 4) { - (void)printf("[Bad OPT_NBR_UNREACH option, length %u != 4]", opt_len); +#define PGM_OPT_NBR_UNREACH_LEN (2+2) + if (opt_len != PGM_OPT_NBR_UNREACH_LEN) { + ND_PRINT("[Bad OPT_NBR_UNREACH option, length %u != %u]", + opt_len, PGM_OPT_NBR_UNREACH_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - (void)printf(" NBR_UNREACH"); - opts_len -= 4; + bp += 2; + ND_PRINT(" NBR_UNREACH"); + opts_len -= PGM_OPT_NBR_UNREACH_LEN; break; case PGM_OPT_PATH_NLA: - (void)printf(" PATH_NLA [%d]", opt_len); - bp += opt_len; + ND_PRINT(" PATH_NLA [%u]", opt_len); + bp += opt_len - 2; opts_len -= opt_len; break; case PGM_OPT_SYN: - if (opt_len != 4) { - (void)printf("[Bad OPT_SYN option, length %u != 4]", opt_len); +#define PGM_OPT_SYN_LEN (2+2) + if (opt_len != PGM_OPT_SYN_LEN) { + ND_PRINT("[Bad OPT_SYN option, length %u != %u]", + opt_len, PGM_OPT_SYN_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - (void)printf(" SYN"); - opts_len -= 4; + bp += 2; + ND_PRINT(" SYN"); + opts_len -= PGM_OPT_SYN_LEN; break; case PGM_OPT_FIN: - if (opt_len != 4) { - (void)printf("[Bad OPT_FIN option, length %u != 4]", opt_len); +#define PGM_OPT_FIN_LEN (2+2) + if (opt_len != PGM_OPT_FIN_LEN) { + ND_PRINT("[Bad OPT_FIN option, length %u != %u]", + opt_len, PGM_OPT_FIN_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - (void)printf(" FIN"); - opts_len -= 4; + bp += 2; + ND_PRINT(" FIN"); + opts_len -= PGM_OPT_FIN_LEN; break; case PGM_OPT_RST: - if (opt_len != 4) { - (void)printf("[Bad OPT_RST option, length %u != 4]", opt_len); +#define PGM_OPT_RST_LEN (2+2) + if (opt_len != PGM_OPT_RST_LEN) { + ND_PRINT("[Bad OPT_RST option, length %u != %u]", + opt_len, PGM_OPT_RST_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - (void)printf(" RST"); - opts_len -= 4; + bp += 2; + ND_PRINT(" RST"); + opts_len -= PGM_OPT_RST_LEN; break; case PGM_OPT_CR: - (void)printf(" CR"); - bp += opt_len; + ND_PRINT(" CR"); + bp += opt_len - 2; opts_len -= opt_len; break; case PGM_OPT_CRQST: - if (opt_len != 4) { - (void)printf("[Bad OPT_CRQST option, length %u != 4]", opt_len); +#define PGM_OPT_CRQST_LEN (2+2) + if (opt_len != PGM_OPT_CRQST_LEN) { + ND_PRINT("[Bad OPT_CRQST option, length %u != %u]", + opt_len, PGM_OPT_CRQST_LEN); + return; + } + bp += 2; + ND_PRINT(" CRQST"); + opts_len -= PGM_OPT_CRQST_LEN; + break; + + case PGM_OPT_PGMCC_DATA: +#define PGM_OPT_PGMCC_DATA_FIXED_LEN (2+2+4+2+2) + if (opt_len < PGM_OPT_PGMCC_DATA_FIXED_LEN) { + ND_PRINT("[Bad OPT_PGMCC_DATA option, length %u < %u]", + opt_len, PGM_OPT_PGMCC_DATA_FIXED_LEN); + return; + } + bp += 2; + offset = GET_BE_U_4(bp); + bp += 4; + nla_afnum = GET_BE_U_2(bp); + bp += 2+2; + switch (nla_afnum) { + case AFNUM_IP: + if (opt_len != PGM_OPT_PGMCC_DATA_FIXED_LEN + sizeof(nd_ipv4)) { + ND_PRINT("[Bad OPT_PGMCC_DATA option, length %u != %u + address size]", + opt_len, PGM_OPT_PGMCC_DATA_FIXED_LEN); + return; + } + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv4); + opts_len -= PGM_OPT_PGMCC_DATA_FIXED_LEN + sizeof(nd_ipv4); + break; + case AFNUM_IP6: + if (opt_len != PGM_OPT_PGMCC_DATA_FIXED_LEN + sizeof(nd_ipv6)) { + ND_PRINT("[Bad OPT_PGMCC_DATA option, length %u != %u + address size]", + opt_len, PGM_OPT_PGMCC_DATA_FIXED_LEN); + return; + } + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv6); + opts_len -= PGM_OPT_PGMCC_DATA_FIXED_LEN + sizeof(nd_ipv6); + break; + default: + goto invalid; + } + + ND_PRINT(" PGMCC DATA %u %s", offset, nla_buf); + break; + + case PGM_OPT_PGMCC_FEEDBACK: +#define PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN (2+2+4+2+2) + if (opt_len < PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN) { + ND_PRINT("[Bad PGM_OPT_PGMCC_FEEDBACK option, length %u < %u]", + opt_len, PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN); return; } - flags1 = *bp++; - flags2 = *bp++; - (void)printf(" CRQST"); - opts_len -= 4; + bp += 2; + offset = GET_BE_U_4(bp); + bp += 4; + nla_afnum = GET_BE_U_2(bp); + bp += 2+2; + switch (nla_afnum) { + case AFNUM_IP: + if (opt_len != PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN + sizeof(nd_ipv4)) { + ND_PRINT("[Bad OPT_PGMCC_FEEDBACK option, length %u != %u + address size]", + opt_len, PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN); + return; + } + ND_TCHECK_LEN(bp, sizeof(nd_ipv4)); + addrtostr(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv4); + opts_len -= PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN + sizeof(nd_ipv4); + break; + case AFNUM_IP6: + if (opt_len != PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN + sizeof(nd_ipv6)) { + ND_PRINT("[Bad OPT_PGMCC_FEEDBACK option, length %u != %u + address size]", + opt_len, PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN); + return; + } + ND_TCHECK_LEN(bp, sizeof(nd_ipv6)); + addrtostr6(bp, nla_buf, sizeof(nla_buf)); + bp += sizeof(nd_ipv6); + opts_len -= PGM_OPT_PGMCC_FEEDBACK_FIXED_LEN + sizeof(nd_ipv6); + break; + default: + goto invalid; + } + + ND_PRINT(" PGMCC FEEDBACK %u %s", offset, nla_buf); break; default: - (void)printf(" OPT_%02X [%d] ", opt_type, opt_len); - bp += opt_len; + ND_PRINT(" OPT_%02X [%u] ", opt_type, opt_len); + bp += opt_len - 2; opts_len -= opt_len; break; } @@ -748,12 +839,13 @@ pgm_print(register const u_char *bp, register u_int length, } } - (void)printf(" [%u]", EXTRACT_16BITS(&pgm->pgm_length)); + ND_PRINT(" [%u]", length); + if (ndo->ndo_packettype == PT_PGM_ZMTP1 && + (pgm_type_val == PGM_ODATA || pgm_type_val == PGM_RDATA)) + zmtp1_datagram_print(ndo, bp, + GET_BE_U_2(pgm->pgm_length)); return; - -trunc: - fputs("[|pgm]", stdout); - if (ch != '\0') - putchar('>'); +invalid: + nd_print_invalid(ndo); }