Commit Graph

442 Commits

Author SHA1 Message Date
John Thacker ca230a59e0 wiretap, pcapng: Distinguish WTAP_ENCAP_UNKNOWN and _NONE
WTAP_ENCAP_UNKNOWN is used for two different cases:
1. Encapsulation type values that are unsupported by libwiretap or
bogus values (and thus "unknown" to libwiretap).

2. An initial state where the encapsulation type is "not yet" known
for a file type like pcapng without a single encapsulation type in the
header, before any packets or interfaces that set the encapsulation type
have been read. (If the file has no packets, this may be the value after
the file is entirely read in.) This can be the value when an output file
is written out simultaneously with reading an input file, rather than
reading the entire input file first, and, e.g., there is a custom block
before any IDBs.

The first case can never be handled when writing out a file, but the
second case can possibly be handled, so long as (for pcapng) IDBs
are available to write when they become necessary, or (for file
types like pcap with a single link-layer type in the header) the
writer waits until a link-layer type is seen to create the output
header. (It is possible, of course, that writing would fail in the
middle if an unsupported encapsulation type appears, or if the
encapsulation becomes per-packet for file types that don't support that,
but that is an unavoidable risk when writing without reading the entire
input file(s).)

Introduce WTAP_ENCAP_NONE for the second case, and use it for pcapng,
where we guarantee that any necessary IDBs will be passed along.
Continue to use WTAP_ENCAP_UNKNOWN for the first case.

Allow pcapng files to open a file for writing with WTAP_ENCAP_NONE.

There are some other file types that support per-packet link-types,
and could also use WTAP_ENCAP_NONE, but they require more work to
generate IDBs. (Note that all of them currently are impossible to
write to pcapng when they have multiple encapsulations, even if
the encapsulations are all supported by pcapng, because they don't
properly generate IDBs.)

Remove the workaround in ef43fd48b4
for tshark writing to pcapng when the source file is WTAP_ENCAP_UNKNOWN,
since now such files will be WTAP_ENCAP_NONE and work properly (and
also work in editcap, mergcap, etc.)

Along with 8cddc32d35, fix #18449.
2023-02-07 13:33:20 +00:00
João Valverde 7a33d04056 wiretap: Fix pcapng UTF-8 validation
g_utf_8_make_valid() replaces embedded NULs with unicode
replacement characters and this behaviour does not match
the pcapng spec; the first NULL should terminate the string
instead.

Use ws_utf8_make_valid() which provides the correct behaviour.
2023-02-06 15:03:49 +00:00
John Thacker 67a01bdf26 wiretap: Preserve NRBs with editcap, mergecap, and tshark
Use a pointer to the growing array of NRBs from the source
file, as with DSBs, so as to handle reading NRBs in the middle
of a file in one-pass mode.

Write NRBs when reading a file with editcap, or in tshark when
not dissecting packets and writing our own NRB. Continue not
to write the NRB if we're supplying our own list of address info
instead.

If we have already read the entire source file in (such as in
two-pass tshark), move all the NRBs to the beginning of the file
before packets, as done with DSBs.

When merging files with mergecap, write both sets of NRBs. (There
is no attempt to merge the NRBs by looking for common entries.)

Check for name resolution data in the middle of dumping a file,
not just at the end, and check for DSBs at the end of a file,
after all the packets. This means that Wireshark no longer writes
the NRB at the very end of the file after all the packets (which
is worse for future one-pass reads), and DSBs after all packets
are preserved.

Ping #15502
2023-01-30 13:29:49 +00:00
John Thacker 0d2a2d3777 wiretap, pcapng: process all initial internal blocks in pcapng_open
pcapng allows multiple link-layer types, and allows new link-layer types
in the middle of a file. Many (most) other capture types allow a single
link-layer type, which must be specified in the initial header.

When reading files and writing their contents to another file (which
may be of a different type), many programs using the wiretap API want
want to know the link-layer type upon initially opening the source
file, so that they can check if that encapsulation can be written to
the output file, and so that they can write the output file header.

They should be able to wait until a link-layer type is seen before
creating the output type, but don't. (Wireshark reads the entire file
in intially, so this isn't a problem, but that isn't much of an option
for some command line tools, particularly when operating on a pipe or
FIFO.) Note that regardless, if a new link-layer type is encountered
partway through a file, they would still have to fail in the middle
of reading and writing.

However, to make this a little bit easier for such file types, pcapng
block types that are handled strictly internally and not passed back
to the reader can be processed initially in pcapng_open(). (Note
that for DSBs and NRBs, any blocks processed in pcapng_open() will
automatically be sent to the callbacks when the callbacks are added
later.) Previously we just processed all the IDBs immediately after
the initial SHB, instead of all the internal block types.

Fix #18581. Ping #15502.
2023-01-26 05:17:50 +00:00
John Thacker c65d5a0a80 wiretap: Reprocess Name Resolution Blocks during redissect
Keep name resolution information as mandatory elements for
NRBs, and when the ipv4 or ipv6 callback is set, have name
resolution entries from already read NRBs sent to the callback.
rescan_packets can use this when redissecting to reobtain the
name resolution entries from the NRB, similar to what is done
with Decryption Secrets Blocks. (This can also later be used
if we read NRBs and DSBs in pcapng_open before the first packet,
and before the callbacks are set.)

This doesn't yet make the changes to wtap_dumper to write them out,
but is a step towards that too. (It's not clear in cases where we
dissect packets whether we want to copy the entire NRB, or only
write out actually used addresses as done now. For copying without
reading a file, like with editcap, we presumably do want to copy them.)

Fix #13425. Ping #15502
2023-01-21 22:25:03 +00:00
João Valverde a74027ad00 wiretap: Validate UTF-8 encoding for pcapng
Validate UTF-8 encoding for pcapng string options. To
avoid two unnecessary memory allocations for invalid strings and
make the code cleaner a new wtap_block_add_string_option_owned()
function is used.

Add UTF-8 debug check for wiretap API.

Fixes #18703.
2022-12-09 19:17:05 +00:00
Martin Mathieson 793365d0e0 Fix some issues seen with cppcheck 2022-11-22 20:55:24 +00:00
Chuck Craft 085cbd27e1 pcapng: ws_debug - display option code in decimal 2022-11-08 22:15:44 -06:00
Chuck Craft 8de2b2e860 addr_resolv: add support for static hostname entries
Ping #18075
2022-10-31 14:45:56 +00:00
Gerald Combs 8700e9c407 wiretap: Make some pcapng logs noisy.
Switch some pcapng log messages from debug to noisy.
2022-10-11 07:10:16 +00:00
Stephen Hemminger 005169491e pcapng: add support displaying hash from pcapng
Add support for displaying one or more packet hashes that
have been recorded in EPB options.

A patch to add support for EPB hash option is pending for next
DPDK release.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
2022-08-20 06:12:28 +00:00
Roland Knall 885d6b7f73 wiretap: Fix urls in comments
The old ones are no longer valid and lead to 404
2022-07-10 16:05:30 +00:00
Guy Harris 1ee8ead845 Provide the section number for blocks, and show it.
Add a "section number" field to wtap_rec, with a presence flag, and
provide the section number (0-based) for pcapng files.

Display it (1-based) if present.
2022-04-22 18:14:29 -07:00
Dr. Lars Völker ef43fd48b4 tshark: improve robustness for PCAPNGs not starting with IDBs
The PCAPNG code assumes that PCAPNGs start with IDBs, which might not be
true. This patch adds a workaround for Tshark to process such files.
2022-01-16 08:55:24 +00:00
João Valverde 0ccd69e530 Replace g_strdup_printf() with ws_strdup_printf()
Use macros from inttypes.h.
2021-12-19 21:21:58 +00:00
João Valverde 22ee2764a7 Replace g_snprintf() with snprintf() (dissectors)
Use macros from inttypes.h with format strings.
2021-12-19 20:25:11 +00:00
João Valverde fe5248717f Replace g_snprintf() with snprintf()
Use macros from inttypes.h with format strings.
2021-12-19 20:06:13 +00:00
Gerald Combs fcf945f747 wiretap: Fix an underflow in pcapng.
Make sure account for our rounded option length when we process
opt_endofopt. Fixes #17755.
2021-12-02 04:06:59 +00:00
Michael Tuexen 2b52d14d62 BBLog: Fix the handling of skipped blocks 2021-11-20 00:10:28 +01:00
Federico Di Pierro d95935d969 dissectors: support sysdig new block type BLOCK_TYPE_SYSDIG_EVENT_V2_LARGE
The new block type enlarge payload lens up to 4B,
to support larger payload sizes.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
2021-10-29 08:04:21 +00:00
João Valverde 5362d0c31b ws_getopt: Rename struct and macros
This is part of the API and should also be renamed to avoid conflicts.
2021-09-30 13:59:28 +00:00
Guy Harris 58d053923e pcapng: clean up block length checks.
Use the same style of message for too-short block errors ("pcapng: total
block length XXX of {a,an} XXX is too small...").

Add an additional check for the "skip" Netflix cutom block, to make sure
it has enough room for the 4-byte "skipped" value.
2021-09-09 00:19:36 -07:00
Alexis La Goutte 5fc6549178 pcapng: Fix Branch condition evaluate to a garbage value
wblock->internal is not initialized on pcapng_read_custom_block function
pcapng.c:3747:9: warning: Branch condition evaluates to a garbage value [core.uninitialized.Branch]
2021-09-07 05:56:36 +00:00
Alexis La Goutte 7740e9ae27 pcapng(wiretap): Fix Dead Store found by Clang Analyzer
pcapng.c:2675:5: warning: Value stored to 'opt_cont_buf_len' is never read [deadcode.DeadStores]
2021-09-06 04:21:14 +00:00
Guy Harris 030b06ba3c pcapng: write packet and Netflix custom blocks the same as other blocks.
Use compute_options_size() to get the total size of all the options, and
use write_options() to write out the options for those blocks, as we do
for other blocks.

Get rid of wtap_block_option_get_value_size() and
wtap_block_get_options_size_padded(); they're no longer needed, and
their notion of an option's "size" is "size in a pcapng file", so that
doesn't belong in code that's intended to support all file types.
2021-08-30 16:29:04 -07:00
Guy Harris dd8c23c83d pcapng: remove include of wtap_opttypes.h.
It's already included by wtap/wtap.h, which this file includes, and it
doesn't need to be included that early, so that #include isn't
necessary.
2021-08-29 01:00:02 -07:00
Guy Harris 266d707dff pcapng: whitespace and other cleanups.
Remove some extra blank lines, add a blank line where it belongs, and
get rid of a stray #-only line.
2021-08-28 22:29:21 -07:00
Guy Harris 10be4d1611 pcapng: fix handling of byte-swapped sysdig event blocks.
We weren't setting wblock->rec->rec_header.syscall_header.nparams for
byte-swapped event blocks.
2021-08-28 16:00:41 -07:00
Guy Harris 9b132c2353 pcapng: fix output of comment and DNS server name options.
Copy to the buffer the string value, not the structure that contains it.
2021-08-27 18:14:02 -07:00
Guy Harris 4b7bfe35c5 pcapng: process uint32 options like uint64 options.
Move the extraction of the option value from the option content from the
callers of pcapng_process_uint32_option() to
pcapng_process_uint32_option() itself.
2021-08-27 08:58:13 +00:00
Guy Harris a21972d6b9 pcapng: remove some old code left behind by the previous change. 2021-08-27 00:33:09 -07:00
Guy Harris 8acd0d1151 pcapng: export routines to do the option-section processing.
That way, add-on modules to handle block types not handled by the core
pcapng code can use pcap_process_options() to process a block's options
and can use the routines to handle the "standard" option value types to
handle particular options.

Also, allow both everything-is-little-endian and
everything-is-big-endian Custom Block types in pcap_process_options().
2021-08-27 06:48:47 +00:00
Michael Tuexen b17f354304 Add initial support for BBLog files 2021-08-27 04:48:21 +02:00
David Perry dc7089e831 Carry drop count/packet ID/queue ID as options on packet block 2021-07-19 21:25:40 +00:00
Guy Harris cc36b74139 pcapng: shuffle functions that process various option types.
Define them in the order in which wiretap/wtap_opttypes.h defines the
corresponding enum values for wtap_opttype_e.
2021-07-14 01:01:27 -07:00
Guy Harris 7b0c4950a5 wiretap: clean up option definitions a bit.
Spell out "DESCRIPTION" for the IDB description option, as it's spelled
out in the pcapng spec.

Put the #defines for various options in the same order as the block
types for them are in the pcapng spec.
2021-07-14 06:55:12 +00:00
Guy Harris fa9ee0f37a pcapng: process OPT_COMMENT in pcapng_process_options().
That option applies to all pcapng blocks, so handle it in common code.
2021-07-12 18:06:38 -07:00
David Perry 06ed6930dc Carry EPB flags as an option on the packet block
As requested by [this comment][1] on !2859, move `pack_flags` from a
dedicated field in `wtap_rec` to a block option on the packet block in
`wtap_rec.block`.

[1]: https://gitlab.com/wireshark/wireshark/-/merge_requests/2859#note_615984624
2021-07-12 12:41:57 -04:00
David Perry 73087d6fb4 Use wtap_blocks for packet comments
Mostly functioning proof of concept for #14329. This work is intended to
allow Wireshark to support multiple packet comments per packet.

Uses and expands upon the `wtap_block` API in `wiretap/wtap_opttypes.h`.
It attaches a `wtap_block` structure to `wtap_rec` in place of its
current `opt_comment` and `packet_verdict` members to hold OPT_COMMENT
and OPT_PKT_VERDICT option values.
2021-07-07 18:40:24 +00:00
Guy Harris 689ff5a3ad pcapng: centralize access to members of the wtap_optval_t union.
Have the "compute option size" and "write option" routines take a
wtap_optval_t * as an argument and choose the appropriate member of the
union.
2021-06-29 14:54:48 -07:00
Guy Harris c0dc9106dd pcapng: clean up some function names.
Consistently use pcapng_compute_XXX_option_size() for routines to
compute the size of an option of type XXX and pcapng_write_XXX_option()
for routins to write out an option of type XXX.

Sort the routines by the order in which their option type values are
defined.
2021-06-28 17:20:26 -07:00
Guy Harris 73e058032f pcapng: have more common code for writing options.
Have common routines that iterate over all the options, processing
comment and custom options in common code (as they're defined
independently of particular block types), with callbacks to handle the
options for particular block types.
2021-06-28 15:14:36 -07:00
Guy Harris c1082bd99e wiretap: have the "for each option" routine return a success/fail indication.
Have the routine it calls return a Boolean value, with "true" meaning
"keep going" and "false" meaning "stop iterating and return a failure
indication".  If the callback routine never returns "false", the routine
returns "true" as a success indication.
2021-06-27 13:25:16 -07:00
Guy Harris 9e6aa424f1 pcapng: have a common routine to write the "end of options" option. 2021-06-27 03:14:14 -07:00
Michael Tuexen 1f0a16e472 pcapng: add support for custom options 2021-06-23 04:38:27 +00:00
Guy Harris 5eda4c0128 pcapng: update a comment to reflect the name cleanups. 2021-06-17 15:55:18 -07:00
Guy Harris 9773b5a4a7 Just call the block type for custom blocks WTAP_BLOCK_CUSTOM.
No need to add a redundant "_BLOCK" at the end.
2021-06-17 15:04:44 -07:00
Guy Harris 6eb3703379 Make various names match the name of the systemd journal export block.
The name of the block, in the pcapng specification is the systemd
Journal Export Block; add "export" after "journal" in various
variable/enum/define names.
2021-06-17 13:52:45 -07:00
Guy Harris 2c9d262ed1 pcapng: indentation cleanups. 2021-06-16 20:03:00 -07:00
Guy Harris 8aceee774a pcapng: get rid of some now-redundant rounding up of the block length.
Now that it's being done in common code, we don't need to do it in the
routines to read sysdig event blocks, systemd journal export blocks, or
unknown blocks.

Add in a comment to match other comments while we're at it.
2021-06-16 18:59:34 -07:00