X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/3fc59aec3627fe7df7c8b86004233345736f40df..296d466cd6bbf2f7e75e15bb6a01268e88c76ed0:/netdissect.c diff --git a/netdissect.c b/netdissect.c index 60c0a750..e78ccbc7 100644 --- a/netdissect.c +++ b/netdissect.c @@ -22,9 +22,7 @@ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#ifdef HAVE_CONFIG_H #include -#endif #include "netdissect-stdinc.h" #include "netdissect.h" @@ -123,14 +121,14 @@ nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size) { #ifdef USE_LIBSMI if (smiLoadModule(module) == 0) { - nd_snprintf(errbuf, errbuf_size, "could not load MIB module %s", + snprintf(errbuf, errbuf_size, "could not load MIB module %s", module); return (-1); } nd_smi_module_loaded = 1; return (0); #else - nd_snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support", + snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support", module); return (-1); #endif @@ -149,42 +147,167 @@ nd_smi_version_string(void) int nd_push_buffer(netdissect_options *ndo, u_char *new_buffer, - const u_char *new_packetp, const u_char *new_snapend) + const u_char *new_packetp, const u_int newlen) { - struct netdissect_saved_info *ndsi; + struct netdissect_saved_packet_info *ndspi; - ndsi = (struct netdissect_saved_info *)malloc(sizeof(struct netdissect_saved_info)); - if (ndsi == NULL) + ndspi = (struct netdissect_saved_packet_info *)malloc(sizeof(struct netdissect_saved_packet_info)); + if (ndspi == NULL) return (0); /* fail */ - ndsi->ndsi_buffer = new_buffer; - ndsi->ndsi_packetp = ndo->ndo_packetp; - ndsi->ndsi_snapend = ndo->ndo_snapend; - ndsi->ndsi_prev = ndo->ndo_buffer_stack; + ndspi->ndspi_buffer = new_buffer; + ndspi->ndspi_packetp = ndo->ndo_packetp; + ndspi->ndspi_snapend = ndo->ndo_snapend; + ndspi->ndspi_prev = ndo->ndo_packet_info_stack; ndo->ndo_packetp = new_packetp; - ndo->ndo_snapend = new_snapend; - ndo->ndo_buffer_stack = ndsi; + ndo->ndo_snapend = new_packetp + newlen; + ndo->ndo_packet_info_stack = ndspi; return (1); /* success */ } + +/* + * In a given netdissect_options structure: + * + * push the current packet information onto the packet information + * stack; + * + * given a pointer into the packet and a length past that point in + * the packet, calculate a new snapshot end that's at the lower + * of the current snapshot end and that point in the packet; + * + * set the snapshot end to that new value. + */ +int +nd_push_snaplen(netdissect_options *ndo, const u_char *bp, const u_int newlen) +{ + struct netdissect_saved_packet_info *ndspi; + u_int snaplen_remaining; + + ndspi = (struct netdissect_saved_packet_info *)malloc(sizeof(struct netdissect_saved_packet_info)); + if (ndspi == NULL) + return (0); /* fail */ + ndspi->ndspi_buffer = NULL; /* no new buffer */ + ndspi->ndspi_packetp = ndo->ndo_packetp; + ndspi->ndspi_snapend = ndo->ndo_snapend; + ndspi->ndspi_prev = ndo->ndo_packet_info_stack; + + /* + * Push the saved previous data onto the stack. + */ + ndo->ndo_packet_info_stack = ndspi; + + /* + * Find out how many bytes remain after the current snapend. + * + * We're restricted to packets with at most UINT_MAX bytes; + * cast the result to u_int, so that we don't get truncation + * warnings on LP64 and LLP64 platforms. (ptrdiff_t is + * signed and we want an unsigned difference; the pointer + * should at most be equal to snapend, and must *never* + * be past snapend.) + */ + snaplen_remaining = (u_int)(ndo->ndo_snapend - bp); + + /* + * If the new snapend is smaller than the one calculated + * above, set the snapend to that value, otherwise leave + * it unchanged. + */ + if (newlen <= snaplen_remaining) { + /* Snapend isn't past the previous snapend */ + ndo->ndo_snapend = bp + newlen; + } + + return (1); /* success */ +} + +/* + * In a given netdissect_options structure: + * + * given a pointer into the packet and a length past that point in + * the packet, calculate a new snapshot end that's at the lower + * of the previous snapshot end - or, if there is no previous + * snapshot end, the current snapshot end - and that point in the + * packet; + * + * set the snapshot end to that new value. + * + * This is to change the current snapshot end. This may increase the + * snapshot end, as it may be used, for example, for a Jumbo Payload + * option in IPv6. It must not increase it past the snapshot length + * atop which the current one was pushed, however. + */ void -nd_pop_buffer(netdissect_options *ndo) +nd_change_snaplen(netdissect_options *ndo, const u_char *bp, const u_int newlen) { - struct netdissect_saved_info *ndsi; + struct netdissect_saved_packet_info *ndspi; + const u_char *previous_snapend; + u_int snaplen_remaining; - ndsi = ndo->ndo_buffer_stack; - ndo->ndo_packetp = ndsi->ndsi_packetp; - ndo->ndo_snapend = ndsi->ndsi_snapend; - ndo->ndo_buffer_stack = ndsi->ndsi_prev; + ndspi = ndo->ndo_packet_info_stack; + if (ndspi->ndspi_prev != NULL) + previous_snapend = ndspi->ndspi_prev->ndspi_snapend; + else + previous_snapend = ndo->ndo_snapend; - free(ndsi->ndsi_buffer); - free(ndsi); + /* + * Find out how many bytes remain after the previous + * snapend - or, if there is no previous snapend, after + * the current snapend. + * + * We're restricted to packets with at most UINT_MAX bytes; + * cast the result to u_int, so that we don't get truncation + * warnings on LP64 and LLP64 platforms. (ptrdiff_t is + * signed and we want an unsigned difference; the pointer + * should at most be equal to snapend, and must *never* + * be past snapend.) + */ + snaplen_remaining = (u_int)(previous_snapend - bp); + + /* + * If the new snapend is smaller than the one calculated + * above, set the snapend to that value, otherwise leave + * it unchanged. + */ + if (newlen <= snaplen_remaining) { + /* Snapend isn't past the previous snapend */ + ndo->ndo_snapend = bp + newlen; + } } void -nd_pop_all_buffers(netdissect_options *ndo) +nd_pop_packet_info(netdissect_options *ndo) { - while (ndo->ndo_buffer_stack != NULL) - nd_pop_buffer(ndo); + struct netdissect_saved_packet_info *ndspi; + + ndspi = ndo->ndo_packet_info_stack; + ndo->ndo_packetp = ndspi->ndspi_packetp; + ndo->ndo_snapend = ndspi->ndspi_snapend; + ndo->ndo_packet_info_stack = ndspi->ndspi_prev; + + free(ndspi->ndspi_buffer); + free(ndspi); +} + +void +nd_pop_all_packet_info(netdissect_options *ndo) +{ + while (ndo->ndo_packet_info_stack != NULL) + nd_pop_packet_info(ndo); +} + +NORETURN void +nd_trunc_longjmp(netdissect_options *ndo) +{ + longjmp(ndo->ndo_early_end, ND_TRUNCATED); +#ifdef _AIX + /* + * In AIX decorates longjmp() with "#pragma leaves", which tells + * XL C that the function is noreturn, but GCC remains unaware of that and + * yields a "'noreturn' function does return" warning. + */ + ND_UNREACHABLE +#endif /* _AIX */ }