X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/a97e56cb1124eec90a989e7aa75973343c656f1e..3d6e7bd1e42a7316fb2020348a8d3e86b1346b36:/print-sctp.c diff --git a/print-sctp.c b/print-sctp.c index 4e079b0b..f625dce6 100644 --- a/print-sctp.c +++ b/print-sctp.c @@ -33,20 +33,19 @@ * SUCH DAMAGE. */ -#define NETDISSECT_REWORKED +/* \summary: Stream Control Transmission Protocol (SCTP) printer */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include +#include -#include "interface.h" +#include "netdissect.h" #include "addrtoname.h" -#include "extract.h" /* must come after interface.h */ +#include "extract.h" #include "ip.h" -#ifdef INET6 #include "ip6.h" -#endif /* Definitions from: * @@ -154,36 +153,36 @@ static const struct tok sctp_chunkid_str[] = { /* the sctp common header */ struct sctpHeader{ - u_int16_t source; - u_int16_t destination; - u_int32_t verificationTag; - u_int32_t adler32; + uint16_t source; + uint16_t destination; + uint32_t verificationTag; + uint32_t adler32; }; /* various descriptor parsers */ struct sctpChunkDesc{ - u_int8_t chunkID; - u_int8_t chunkFlg; - u_int16_t chunkLength; + uint8_t chunkID; + uint8_t chunkFlg; + uint16_t chunkLength; }; struct sctpParamDesc{ - u_int16_t paramType; - u_int16_t paramLength; + uint16_t paramType; + uint16_t paramLength; }; struct sctpRelChunkDesc{ struct sctpChunkDesc chk; - u_int32_t serialNumber; + uint32_t serialNumber; }; struct sctpVendorSpecificParam { struct sctpParamDesc p; /* type must be 0xfffe */ - u_int32_t vendorId; /* vendor ID from RFC 1700 */ - u_int16_t vendorSpecificType; - u_int16_t vendorSpecificLen; + uint32_t vendorId; /* vendor ID from RFC 1700 */ + uint16_t vendorSpecificType; + uint16_t vendorSpecificLen; }; @@ -195,53 +194,53 @@ struct sctpVendorSpecificParam { /* this is used for init ack, too */ struct sctpInitiation{ - u_int32_t initTag; /* tag of mine */ - u_int32_t rcvWindowCredit; /* rwnd */ - u_int16_t NumPreopenStreams; /* OS */ - u_int16_t MaxInboundStreams; /* MIS */ - u_int32_t initialTSN; + uint32_t initTag; /* tag of mine */ + uint32_t rcvWindowCredit; /* rwnd */ + uint16_t NumPreopenStreams; /* OS */ + uint16_t MaxInboundStreams; /* MIS */ + uint32_t initialTSN; /* optional param's follow in sctpParamDesc form */ }; struct sctpV4IpAddress{ struct sctpParamDesc p; /* type is set to SCTP_IPV4_PARAM_TYPE, len=10 */ - u_int32_t ipAddress; + uint32_t ipAddress; }; struct sctpV6IpAddress{ struct sctpParamDesc p; /* type is set to SCTP_IPV6_PARAM_TYPE, len=22 */ - u_int8_t ipAddress[16]; + uint8_t ipAddress[16]; }; struct sctpDNSName{ struct sctpParamDesc param; - u_int8_t name[1]; + uint8_t name[1]; }; struct sctpCookiePreserve{ struct sctpParamDesc p; /* type is set to SCTP_COOKIE_PRESERVE, len=8 */ - u_int32_t extraTime; + uint32_t extraTime; }; struct sctpTimeStamp{ - u_int32_t ts_sec; - u_int32_t ts_usec; + uint32_t ts_sec; + uint32_t ts_usec; }; /* wire structure of my cookie */ struct cookieMessage{ - u_int32_t TieTag_curTag; /* copied from assoc if present */ - u_int32_t TieTag_hisTag; /* copied from assoc if present */ + uint32_t TieTag_curTag; /* copied from assoc if present */ + uint32_t TieTag_hisTag; /* copied from assoc if present */ int32_t cookieLife; /* life I will award this cookie */ struct sctpTimeStamp timeEnteringState; /* the time I built cookie */ struct sctpInitiation initAckISent; /* the INIT-ACK that I sent to my peer */ - u_int32_t addressWhereISent[4]; /* I make this 4 ints so I get 128bits for future */ + uint32_t addressWhereISent[4]; /* I make this 4 ints so I get 128bits for future */ int32_t addrtype; /* address type */ - u_int16_t locScope; /* V6 local scope flag */ - u_int16_t siteScope; /* V6 site scope flag */ + uint16_t locScope; /* V6 local scope flag */ + uint16_t siteScope; /* V6 site scope flag */ /* at the end is tacked on the INIT chunk sent in * its entirety and of course our * signature. @@ -273,15 +272,15 @@ struct sctpSendableInit{ */ struct sctpSelectiveAck{ - u_int32_t highestConseqTSN; - u_int32_t updatedRwnd; - u_int16_t numberOfdesc; - u_int16_t numDupTsns; + uint32_t highestConseqTSN; + uint32_t updatedRwnd; + uint16_t numberOfdesc; + uint16_t numDupTsns; }; struct sctpSelectiveFrag{ - u_int16_t fragmentStart; - u_int16_t fragmentEnd; + uint16_t fragmentStart; + uint16_t fragmentEnd; }; @@ -295,8 +294,8 @@ struct sctpUnifiedSack{ */ struct sctpHBrequest { - u_int32_t time_value_1; - u_int32_t time_value_2; + uint32_t time_value_1; + uint32_t time_value_2; }; /* here is what I read and respond with to. */ @@ -312,7 +311,7 @@ struct sctpHBsender{ struct sctpParamDesc hb; struct sctpHBrequest rtt; int8_t addrFmt[SCTP_ADDRMAX]; - u_int16_t userreq; + uint16_t userreq; }; @@ -333,15 +332,15 @@ struct sctpUnifiedAbortLight{ struct sctpUnifiedAbortHeavy{ struct sctpHeader mh; struct sctpChunkDesc uh; - u_int16_t causeCode; - u_int16_t causeLen; + uint16_t causeCode; + uint16_t causeLen; }; /* For the graceful shutdown we must carry * the tag (in common header) and the highest consequitive acking value */ struct sctpShutdown { - u_int32_t TSN_Seen; + uint32_t TSN_Seen; }; struct sctpUnifiedShutdown{ @@ -354,8 +353,8 @@ struct sctpUnifiedShutdown{ * that is defined as a operation error. */ struct sctpOpErrorCause{ - u_int16_t cause; - u_int16_t causeLen; + uint16_t cause; + uint16_t causeLen; }; struct sctpUnifiedOpError{ @@ -367,15 +366,15 @@ struct sctpUnifiedStreamError{ struct sctpHeader mh; struct sctpChunkDesc uh; struct sctpOpErrorCause c; - u_int16_t strmNum; - u_int16_t reserved; + uint16_t strmNum; + uint16_t reserved; }; struct staleCookieMsg{ struct sctpHeader mh; struct sctpChunkDesc uh; struct sctpOpErrorCause c; - u_int32_t moretime; + uint32_t moretime; }; /* the following is used in all sends @@ -388,10 +387,10 @@ struct sctpUnifiedSingleMsg{ }; struct sctpDataPart{ - u_int32_t TSN; - u_int16_t streamId; - u_int16_t sequence; - u_int32_t payloadtype; + uint32_t TSN; + uint16_t streamId; + uint16_t sequence; + uint32_t payloadtype; }; struct sctpUnifiedDatagram{ @@ -401,13 +400,13 @@ struct sctpUnifiedDatagram{ struct sctpECN_echo{ struct sctpChunkDesc uh; - u_int32_t Lowest_TSN; + uint32_t Lowest_TSN; }; struct sctpCWR{ struct sctpChunkDesc uh; - u_int32_t TSN_reduced_at; + uint32_t TSN_reduced_at; }; static const struct tok ForCES_channels[] = { @@ -496,48 +495,35 @@ void sctp_print(netdissect_options *ndo, const u_char *bp2, /* beginning of enclosing */ u_int sctpPacketLength) /* ip packet */ { + u_int sctpPacketLengthRemaining; const struct sctpHeader *sctpPktHdr; const struct ip *ip; -#ifdef INET6 const struct ip6_hdr *ip6; -#endif - const void *endPacketPtr; u_short sourcePort, destPort; int chunkCount; const struct sctpChunkDesc *chunkDescPtr; - const void *nextChunk; const char *sep; int isforces = 0; - - sctpPktHdr = (const struct sctpHeader*) bp; - endPacketPtr = (const u_char*)sctpPktHdr+sctpPacketLength; - - if( (u_long) endPacketPtr > (u_long) ndo->ndo_snapend) - endPacketPtr = (const void *) ndo->ndo_snapend; - ip = (struct ip *)bp2; -#ifdef INET6 - if (IP_V(ip) == 6) - ip6 = (const struct ip6_hdr *)bp2; - else - ip6 = NULL; -#endif /*INET6*/ - ND_TCHECK(*sctpPktHdr); - if (sctpPacketLength < sizeof(struct sctpHeader)) { ND_PRINT((ndo, "truncated-sctp - %ld bytes missing!", - (long)sctpPacketLength-sizeof(struct sctpHeader))); + (long)(sizeof(struct sctpHeader) - sctpPacketLength))); return; } - - /* sctpPacketLength -= sizeof(struct sctpHeader); packet length */ - /* is now only as long as the payload */ + sctpPktHdr = (const struct sctpHeader*) bp; + ND_TCHECK(*sctpPktHdr); + sctpPacketLengthRemaining = sctpPacketLength; sourcePort = EXTRACT_16BITS(&sctpPktHdr->source); destPort = EXTRACT_16BITS(&sctpPktHdr->destination); -#ifdef INET6 + ip = (const struct ip *)bp2; + if (IP_V(ip) == 6) + ip6 = (const struct ip6_hdr *)bp2; + else + ip6 = NULL; + if (ip6) { ND_PRINT((ndo, "%s.%d > %s.%d: sctp", ip6addr_string(ndo, &ip6->ip6_src), @@ -545,7 +531,6 @@ void sctp_print(netdissect_options *ndo, ip6addr_string(ndo, &ip6->ip6_dst), destPort)); } else -#endif /*INET6*/ { ND_PRINT((ndo, "%s.%d > %s.%d: sctp", ipaddr_string(ndo, &ip->ip_src), @@ -563,40 +548,48 @@ void sctp_print(netdissect_options *ndo, isforces = 1; } + bp += sizeof(struct sctpHeader); + sctpPacketLengthRemaining -= sizeof(struct sctpHeader); + if (ndo->ndo_vflag >= 2) sep = "\n\t"; else sep = " ("; /* cycle through all chunks, printing information on each one */ - for (chunkCount = 0, - chunkDescPtr = (const struct sctpChunkDesc *) - ((const u_char*) sctpPktHdr + sizeof(struct sctpHeader)); - chunkDescPtr != NULL && - ( (const void *) - ((const u_char *) chunkDescPtr + sizeof(struct sctpChunkDesc)) - <= endPacketPtr); - - chunkDescPtr = (const struct sctpChunkDesc *) nextChunk, chunkCount++) + for (chunkCount = 0, chunkDescPtr = (const struct sctpChunkDesc *)bp; + sctpPacketLengthRemaining != 0; + chunkCount++) { - u_int16_t chunkLength; - const u_char *chunkEnd; - u_int16_t align; + uint16_t chunkLength, chunkLengthRemaining; + uint16_t align; + chunkDescPtr = (const struct sctpChunkDesc *)bp; + if (sctpPacketLengthRemaining < sizeof(*chunkDescPtr)) { + ND_PRINT((ndo, "%s%d) [chunk descriptor cut off at end of packet]", sep, chunkCount+1)); + break; + } ND_TCHECK(*chunkDescPtr); chunkLength = EXTRACT_16BITS(&chunkDescPtr->chunkLength); if (chunkLength < sizeof(*chunkDescPtr)) { - ND_PRINT((ndo, "%s%d) [Bad chunk length %u]", sep, chunkCount+1, chunkLength)); + ND_PRINT((ndo, "%s%d) [Bad chunk length %u, < size of chunk descriptor]", sep, chunkCount+1, chunkLength)); break; } + chunkLengthRemaining = chunkLength; - ND_TCHECK2(*((u_int8_t *)chunkDescPtr), chunkLength); - chunkEnd = ((const u_char*)chunkDescPtr + chunkLength); - - align=chunkLength % 4; + align = chunkLength % 4; if (align != 0) align = 4 - align; - nextChunk = (const void *) (chunkEnd + align); + if (sctpPacketLengthRemaining < align) { + ND_PRINT((ndo, "%s%d) [Bad chunk length %u, > remaining data in packet]", sep, chunkCount+1, chunkLength)); + break; + } + + ND_TCHECK2(*bp, chunkLength); + + bp += sizeof(*chunkDescPtr); + sctpPacketLengthRemaining -= sizeof(*chunkDescPtr); + chunkLengthRemaining -= sizeof(*chunkDescPtr); ND_PRINT((ndo, "%s%d) ", sep, chunkCount+1)); ND_PRINT((ndo, "[%s] ", tok2str(sctp_chunkid_str, "Unknown chunk type: 0x%x", @@ -606,6 +599,8 @@ void sctp_print(netdissect_options *ndo, case SCTP_DATA : { const struct sctpDataPart *dataHdrPtr; + uint32_t ppid; + u_int payload_size; if ((chunkDescPtr->chunkFlg & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED) @@ -629,9 +624,13 @@ void sctp_print(netdissect_options *ndo, == SCTP_DATA_LAST_FRAG) ) ND_PRINT((ndo, " ")); - dataHdrPtr=(const struct sctpDataPart*)(chunkDescPtr+1); + if (chunkLengthRemaining < sizeof(*dataHdrPtr)) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); + return; + } + dataHdrPtr=(const struct sctpDataPart*)bp; - u_int32_t ppid = EXTRACT_32BITS(&dataHdrPtr->payloadtype); + ppid = EXTRACT_32BITS(&dataHdrPtr->payloadtype); ND_PRINT((ndo, "[TSN: %u] ", EXTRACT_32BITS(&dataHdrPtr->TSN))); ND_PRINT((ndo, "[SID: %u] ", EXTRACT_16BITS(&dataHdrPtr->streamId))); ND_PRINT((ndo, "[SSEQ %u] ", EXTRACT_16BITS(&dataHdrPtr->sequence))); @@ -644,70 +643,92 @@ void sctp_print(netdissect_options *ndo, (ppid == SCTP_PPID_FORCES_LP); } - const u_char *payloadPtr = (const u_char *) (dataHdrPtr + 1); - if (EXTRACT_16BITS(&chunkDescPtr->chunkLength) < - sizeof(struct sctpDataPart) + sizeof(struct sctpChunkDesc) + 1) { - ND_PRINT((ndo, "bogus chunk length %u]", EXTRACT_16BITS(&chunkDescPtr->chunkLength))); + bp += sizeof(*dataHdrPtr); + sctpPacketLengthRemaining -= sizeof(*dataHdrPtr); + chunkLengthRemaining -= sizeof(*dataHdrPtr); + payload_size = chunkLengthRemaining; + if (payload_size == 0) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); return; } - u_int payload_size = EXTRACT_16BITS(&chunkDescPtr->chunkLength) - - (sizeof(struct sctpDataPart) + sizeof(struct sctpChunkDesc)); - if (isforces) { - forces_print(ndo, payloadPtr, payload_size); + forces_print(ndo, bp, payload_size); } else if (ndo->ndo_vflag >= 2) { /* if verbose output is specified */ /* at the command line */ switch (ppid) { case SCTP_PPID_M3UA : - print_m3ua(payloadPtr, payload_size); + m3ua_print(ndo, bp, payload_size); break; default: ND_PRINT((ndo, "[Payload")); if (!ndo->ndo_suppress_default_print) { ND_PRINT((ndo, ":")); - ND_DEFAULTPRINT(payloadPtr, payload_size); + ND_DEFAULTPRINT(bp, payload_size); } ND_PRINT((ndo, "]")); break; } } + bp += payload_size; + sctpPacketLengthRemaining -= payload_size; + chunkLengthRemaining -= payload_size; break; } case SCTP_INITIATION : { const struct sctpInitiation *init; - init=(const struct sctpInitiation*)(chunkDescPtr+1); + if (chunkLengthRemaining < sizeof(*init)) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); + return; + } + init=(const struct sctpInitiation*)bp; ND_PRINT((ndo, "[init tag: %u] ", EXTRACT_32BITS(&init->initTag))); ND_PRINT((ndo, "[rwnd: %u] ", EXTRACT_32BITS(&init->rcvWindowCredit))); ND_PRINT((ndo, "[OS: %u] ", EXTRACT_16BITS(&init->NumPreopenStreams))); ND_PRINT((ndo, "[MIS: %u] ", EXTRACT_16BITS(&init->MaxInboundStreams))); ND_PRINT((ndo, "[init TSN: %u] ", EXTRACT_32BITS(&init->initialTSN))); + bp += sizeof(*init); + sctpPacketLengthRemaining -= sizeof(*init); + chunkLengthRemaining -= sizeof(*init); -#if(0) /* ALC you can add code for optional params here */ - if( (init+1) < chunkEnd ) +#if 0 /* ALC you can add code for optional params here */ + if( chunkLengthRemaining != 0 ) ND_PRINT((ndo, " @@@@@ UNFINISHED @@@@@@%s\n", "Optional params present, but not printed.")); #endif + bp += chunkLengthRemaining; + sctpPacketLengthRemaining -= chunkLengthRemaining; + chunkLengthRemaining = 0; break; } case SCTP_INITIATION_ACK : { const struct sctpInitiation *init; - init=(const struct sctpInitiation*)(chunkDescPtr+1); + if (chunkLengthRemaining < sizeof(*init)) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); + return; + } + init=(const struct sctpInitiation*)bp; ND_PRINT((ndo, "[init tag: %u] ", EXTRACT_32BITS(&init->initTag))); ND_PRINT((ndo, "[rwnd: %u] ", EXTRACT_32BITS(&init->rcvWindowCredit))); ND_PRINT((ndo, "[OS: %u] ", EXTRACT_16BITS(&init->NumPreopenStreams))); ND_PRINT((ndo, "[MIS: %u] ", EXTRACT_16BITS(&init->MaxInboundStreams))); ND_PRINT((ndo, "[init TSN: %u] ", EXTRACT_32BITS(&init->initialTSN))); + bp += sizeof(*init); + sctpPacketLengthRemaining -= sizeof(*init); + chunkLengthRemaining -= sizeof(*init); -#if(0) /* ALC you can add code for optional params here */ - if( (init+1) < chunkEnd ) +#if 0 /* ALC you can add code for optional params here */ + if( chunkLengthRemaining != 0 ) ND_PRINT((ndo, " @@@@@ UNFINISHED @@@@@@%s\n", "Optional params present, but not printed.")); #endif + bp += chunkLengthRemaining; + sctpPacketLengthRemaining -= chunkLengthRemaining; + chunkLengthRemaining = 0; break; } case SCTP_SELECTIVE_ACK: @@ -717,38 +738,77 @@ void sctp_print(netdissect_options *ndo, int fragNo, tsnNo; const u_char *dupTSN; - sack=(const struct sctpSelectiveAck*)(chunkDescPtr+1); + if (chunkLengthRemaining < sizeof(*sack)) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); + return; + } + sack=(const struct sctpSelectiveAck*)bp; ND_PRINT((ndo, "[cum ack %u] ", EXTRACT_32BITS(&sack->highestConseqTSN))); ND_PRINT((ndo, "[a_rwnd %u] ", EXTRACT_32BITS(&sack->updatedRwnd))); ND_PRINT((ndo, "[#gap acks %u] ", EXTRACT_16BITS(&sack->numberOfdesc))); ND_PRINT((ndo, "[#dup tsns %u] ", EXTRACT_16BITS(&sack->numDupTsns))); + bp += sizeof(*sack); + sctpPacketLengthRemaining -= sizeof(*sack); + chunkLengthRemaining -= sizeof(*sack); /* print gaps */ - for (frag = ( (const struct sctpSelectiveFrag *) - ((const struct sctpSelectiveAck *) sack+1)), - fragNo=0; - (const void *)frag < nextChunk && fragNo < EXTRACT_16BITS(&sack->numberOfdesc); - frag++, fragNo++) + for (fragNo=0; + chunkLengthRemaining != 0 && fragNo < EXTRACT_16BITS(&sack->numberOfdesc); + bp += sizeof(*frag), sctpPacketLengthRemaining -= sizeof(*frag), chunkLengthRemaining -= sizeof(*frag), fragNo++) { + if (chunkLengthRemaining < sizeof(*frag)) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); + return; + } + frag = (const struct sctpSelectiveFrag *)bp; ND_PRINT((ndo, "\n\t\t[gap ack block #%d: start = %u, end = %u] ", fragNo+1, EXTRACT_32BITS(&sack->highestConseqTSN) + EXTRACT_16BITS(&frag->fragmentStart), EXTRACT_32BITS(&sack->highestConseqTSN) + EXTRACT_16BITS(&frag->fragmentEnd))); - + } /* print duplicate TSNs */ - for (dupTSN = (const u_char *)frag, tsnNo=0; - (const void *) dupTSN < nextChunk && tsnNonumDupTsns); - dupTSN += 4, tsnNo++) + for (tsnNo=0; + chunkLengthRemaining != 0 && tsnNonumDupTsns); + bp += 4, sctpPacketLengthRemaining -= 4, chunkLengthRemaining -= 4, tsnNo++) { + if (chunkLengthRemaining < 4) { + ND_PRINT((ndo, "bogus chunk length %u]", chunkLength)); + return; + } + dupTSN = (const u_char *)bp; ND_PRINT((ndo, "\n\t\t[dup TSN #%u: %u] ", tsnNo+1, - EXTRACT_32BITS(dupTSN))); - + EXTRACT_32BITS(dupTSN))); + } + break; + } + default : + { + bp += chunkLengthRemaining; + sctpPacketLengthRemaining -= chunkLengthRemaining; + chunkLengthRemaining = 0; break; } } - if (ndo->ndo_vflag < 2) - sep = ", ("; + /* + * Any extra stuff at the end of the chunk? + * XXX - report this? + */ + bp += chunkLengthRemaining; + sctpPacketLengthRemaining -= chunkLengthRemaining; + + if (ndo->ndo_vflag < 2) + sep = ", ("; + + if (align != 0) { + /* + * Fail if the alignment padding isn't in the captured data. + * Otherwise, skip it. + */ + ND_TCHECK2(*bp, align); + bp += align; + sctpPacketLengthRemaining -= align; + } } return;