Commit Graph

407 Commits

Author SHA1 Message Date
Francois-Xavier Le Bail
835bf2c11c CHDLC: Modernize packet parsing
Enable ND_LONGJMP_FROM_TCHECK and remove the "trunc" labels.
Use ND_ICHECK_U() for length checks and add standard "invalid" sections.
Remove a ndo_ll_hdr_len increment in a truncation case.
Update chdlc_print() to a void function.
Remove a now useless variable.
2022-08-10 16:32:25 +02:00
Francois-Xavier Le Bail
7d2bc7fcb5 Fix some typos
[skip ci]
2022-07-04 14:03:15 +02:00
Denis Ovsienko
dceb3b47e5 Fix building on AIX/POWER with XL C.
"./netdissect.h", line 283.1: 1506-277 (S) Syntax error: possible
  missing ';' or ','?
make: 1254-004 The error code from the last command is 1.
2022-06-15 10:47:01 +01:00
Francois-Xavier Le Bail
7c9e185b02 Add some const qualifiers 2022-04-02 13:21:29 +02:00
Guy Harris
e84f58700f Fail if nd_push_buffer() or nd_push_snaplen() fails.
Always call ndo->ndo_error with a memory-allocation error if they fail.

Add WARN_UNUSED_RESULT for compilers that support it, and use it for
those routines, so that any future code that doesn't check for failure
gets a warning.
2022-04-01 15:28:33 -07:00
Guy Harris
6a681e6a16 Have routines that set the snapend take a buffer pointer and length as args.
Have nd_push_buffer() take a snapshot length, not a snapshot end, as
its last argument.

Replace nd_push_snapend() and nd_change_snapend() with nd_push_snaplen()
and nd_change_snaplen(), both of which take a pointer into the packet
buffer and snapshot length relative to that pointer as arguments.  Have
those routines check the snapshot length to make sure it's not bigger
than the number of bytes in the packet past the pointer, and silently
ignore the requst if it is.

Using a length rather than a pointer avoids the possibility of the
calculation of the snapshot end overflowing and resulting in a snapshot
end *before* the point in the buffer.

Add a test for this, with a capture file containing an IPv6 packet with
an extremely large "jumbo" packet size.

Revert the "Make sure we don't set the snapend before the beginning of
the packet." changes, as they no longer apply with this change (which
also makes sure we don't set the snapend before the beginning of the
packet).
2022-03-31 02:29:19 -07:00
Guy Harris
be43281053 Make sure we don't set the snapend before the beginning of the packet.
If a caller attempts to set it after the current snapend, just silently
ignore the attempt.

If they try to set it before the beginning of the packet, report it as a
bug and quit dissection.  Add a new setjmp() return value meaning "bug"
rather than "truncated packet", add an "nd_bug_longjmp()" routine for
reporting bugs where we should quit dissecting, and use that in this
case.
2022-03-29 00:51:21 -07:00
Guy Harris
518e1bf60b Move nd_trunc_longjmp() to netdissect.h with the other nd_ functions.
It's not particularly tied to the data-extraction functions; this makes
it available elsewhere.
2022-03-28 12:44:50 -07:00
Guy Harris
2122c3a243 Support more Realtek protocols than RRCP.
The Ethertype 0x8899 is used by Realtek for several over-the-wire
protocols, as well as for supplying tag information to a host CPU from a
Realtek chip.  Add code to handle protocols other than RRCP, although we
don't fully dissect all of them.
2022-01-23 01:44:16 -08:00
Francois-Xavier Le Bail
f5060b0613 Update tok2strbuf() to a static function
It is only used in util-print.c.
2022-01-18 11:09:52 +01:00
Nathan O'Sullivan
63b104dc5b Add --print-sampling option to print every Nth packet
New option `--print-sampling=NTH` will parse and print every NTH packet,
with all other packets producing no output. This option enables
`--print` and `-S` flags.

Print sampling is useful for real-time inspection of an interface with
a high packet rate, or initial inspection of large capture files.
2022-01-17 19:43:59 +00:00
Francois-Xavier Le Bail
97e4fe30d8 WHOIS: Add its own printer source file and printer function
Like all other text protocols.

This is a follow-up to 185b7ce04b.
2022-01-14 13:34:49 +01:00
Francois-Xavier Le Bail
ef5323efe6 Update the ND_LCHECK*() macros to ND_ICHECK*() macros
ICHECK like Invalid-Check.
Reminder: If the checked expression is true an error message is printed
and a 'goto invalid' is executed.

This change adds the parameter 'operator'.

Before this change, '<' comparison was hard coded.
We can do now:
ND_ICHECK_U(length, <, HEADER_LEN);
ND_ICHECK_U(length, ==, 24);
ND_ICHECK_U(length, !=, 8);
ND_ICHECK_ZU(length, <, sizeof(struct my_struct));
ND_ICHECKMSG_U("message length", msg_tlen, <, 4);
...
(Any comparison operator)

Remark: The change of names from ND_LCHECK*() to ND_ICHECK*() is
because something else than a length(L) can be checked.

Moreover:
Place the 'message' parameter at the beginning of ND_ICHECKMSG_U()
and ND_ICHECKMSG_ZU() paramaters lists.
2021-12-29 15:11:32 +01:00
Francois-Xavier Le Bail
0ef022a25a Keep the link-layer dissectors names sorted 2021-12-15 18:11:04 +01:00
Rui Paulo
8ce37b54ed Initial support to parse QUIC packets. 2021-11-09 09:11:34 +01:00
Quentin Armitage
d3eb92550a VRRP: Add support for IPv6
Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
2021-11-02 09:30:21 +00:00
Denis Ovsienko
0a29523dfc netdissect.h requires funcattrs.h
...because the former uses PRINTFLIKE_FUNCPTR().  However, it didn't
have the #include.  However, the problem was not visible because most
files that included netdissect.h also included (directly or through
another header) funcattrs.h beforehand.  The only exceptions were the
following two files, which apparently have not been compiled anywhere
for a long time:

In file included from missing/strdup.c:38:
./netdissect.h:254:8: error: expected ':', ',', ';', '}' or
'__attribute__' before 'PRINTFLIKE_FUNCPTR'

In file included from missing/snprintf.c:45:
./netdissect.h:254:8: error: expected ':', ',', ';', '}' or
'__attribute__' before 'PRINTFLIKE_FUNCPTR'

Add the missing #include to netdissect.h to eliminate this space for
failure.
2021-07-29 21:12:06 +01:00
Francois-Xavier Le Bail
1609b127e2 netdissect.h: Keep the printers declarations names sorted
Moreover:
Declare macsec_print() on one line.
Remove an extra space.

[skip ci]
2021-05-22 14:01:57 +02:00
Francois-Xavier Le Bail
29973d83e8 Zephyr: Update 'length' type to unsigned
It is unsigned in the calling function udp_print().
2021-05-22 09:45:44 +02:00
Denis Ovsienko
032d569e68 Spell "indent" where it belongs. 2021-03-28 13:44:35 +01:00
Francois-Xavier Le Bail
8d55c5932e VTP: Fix a warning
The warning on AppVeyor (Visual Studio 2017) was:
print-vtp.c(122): warning C4028: formal parameter 3 different from
declaration
2021-02-28 14:52:04 +01:00
Francois-Xavier Le Bail
73c5d1e292 Fix a typo in ND_LCHECK_ZU definition
[skip ci]
2021-01-17 10:33:39 +01:00
Francois-Xavier Le Bail
014b308d08 Add ND_LCHECKMSG_U, ND_LCHECK_U, ND_LCHECKMSG_ZU and ND_LCHECK_ZU macros
They check length < minimum for invalid packet with or without a custom
message, format %u or %zu.

%zu (ND_LCHECKMSG_ZU and ND_LCHECK_ZU) is useful when minimum is a
sizeof(...).

[skip ci]
2021-01-16 14:33:10 +01:00
Francois-Xavier Le Bail
f867bc9f40 Remove the no more used nd_print() function
The function to use now is: nd_printjnp().
(Added by commit 635e3cc92b)
2021-01-09 14:11:01 +01:00
Francois-Xavier Le Bail
90ddb17d27 Add a nd_printjn() function
It prints a counted filename (or other ASCII string), part of
the packet buffer, filtering out non-printable characters.
Stop if truncated (via GET_U_1/longjmp) or after n bytes,
whichever is first.
The suffix comes from: j:longJmp, n:after N bytes.
2021-01-08 16:09:41 +01:00
Guy Harris
399c5ebdd0 Squelch some signed vs. unsigned warnings.
They showed up on 64-bit OpenBSD 6.6.
2021-01-01 21:08:32 -08:00
Francois-Xavier Le Bail
2eb0237470 Remove the no more used nd_printzp() function
The function to use now is: nd_printjnp().
(Added by comit 635e3cc92b)
2020-12-10 22:52:57 +01:00
Denis Ovsienko
4350892475 CDP: Modernize packet parsing style.
Enable ND_LONGJMP_FROM_TCHECK. Remove many redundant ND_TCHECK*()
instances. Report invalid packets as invalid with a reason, not
truncated. Test whether the invalid packet is entirely in the buffer.

Remove redundant and add plenty of new length checks. Reduce scope for
some variables. Consistently decrement length when advancing the current
pointer. Resort to ND_TCHECK_LEN() only when there are no better ways to
test the packet buffer space.

Do not pass caplen to cdp_print() and remove an improvised snapshot end
guard. Move TLV type-specific code from cdp_print() into standalone
small functions and upgrade a TLV name lookup table into a more
developed TLV registry table along the lines of what was done for
OpenFlow.

In cdp_print_addr() instead of comparing pointers test the address
counter only and trust the loop to do the length checks.

Replace cdp_get_number() with GET_*() macros.

Rewrite the code to make it easier to follow and make indentation more
consistent while at it.

Remove a stray CDP case block from print-chdlc.c.
2020-12-08 23:50:23 +00:00
Francois-Xavier Le Bail
635e3cc92b Add a nd_printjnp() function
It prints a null-padded filename (or other ASCII string), part of
the packet buffer, filtering out non-printable characters.
Stop if truncated (via GET_U_1/longjmp) or after n bytes or before
the null char, whichever occurs first.
The suffix comes from: j:longJmp, n:after N bytes, p:null-Padded.
2020-11-01 12:50:29 +01:00
Francois-Xavier Le Bail
7bd4c9482d Use __func__ from C99 in the debug macro ND_DEBUG 2020-10-31 23:40:09 +01:00
Denis Ovsienko
e1fd57e084 Wrap some common code up as uint2tokary().
I like the ternary operator (in programming languages that define it with
the same associativity as humans tend to presume), but sometimes a lookup
function is better.
2020-09-30 21:48:28 +01:00
Denis Ovsienko
74e3481289 Rename nd_trunc() to nd_trunc_longjmp(). 2020-09-27 21:48:16 +01:00
Denis Ovsienko
424961546a Introduce and use ND_LONGJMP_FROM_TCHECK.
Apply this to the 4 the recently converted protocols.

HOW TO USE:

Define this in a print-*.c file before including netdissect.h to make
all its ND_TCHECK*() instances longjmp(). Only do this after verifying
that every ND_TCHECK*() in the file is correct and that in every
possible case it is safe to call longjmp() without incurring resource
leaks or any other problems. If it is not safe or feasible to convert
whole file, consider converting one function at a time.
2020-09-27 21:45:46 +01:00
Denis Ovsienko
4e2e9c244a OpenFlow: Modernize packet parsing style.
Most OpenFlow functions operated on the (cp, ep, len) tuple, returned a
pointer to tell the calling function where to decode next (which was not
a good idea because the calling functions had the values of cp and len in
the first place) and set the pointer to ep to indicate truncated data.
Change them to use the (cp, len) tuple, to be void and to use nd_trunc().
Update the header comment to explain this new convention.

Now if a low-level function gets an offset wrong, as in the bug recently
fixed in of10_queue_props_print(), and manages to return, the error will
not propagate into the calling function, which will be able to continue
the decoding.

Since cp does not have to be current anymore when the [void] function
returns, remove a number of cp increments just before the return.
Introduce OF_FWD(), which modifies both cp and len in sync, and
OF_CHK_FWD(), which also does a bounds check, and use these macros
instead of incrementing cp directly in every function that has both cp
and len arguments. Update the code to account for len that is
decrementing as cp is incrementing.

Commit ad69daa2 was fine for its time, but the introduction of the GET
macros had made it obsolete. Instead of reintroducing ep back merge
of_header_body_print() into openflow_print() to simplify the handling of
pointers, sizes and error conditions, all in the style above.

While at it, subtract OF_HEADER_FIXLEN from the declared OpenFlow message
length before passing it to of10_header_body_print() and make the latter
the only function that has to know about the common header size (clarify
it in the comment). This and the sufficiently current cp and len
disencumber the following functions of type-specific length calculations:
of10_features_reply_print(), of10_flow_mod_print(),
of10_stats_request_print(), of10_stats_reply_print(),
of10_packet_out_print(), of10_packet_in_print() and of10_error_print().

The resulting code in most cases addresses the not yet processed part of
the declared structure with just cp and len: nextfunc(ndo, cp, len) and
ND_TCHECK_LEN(cp, len) do the right thing regardless of how far the
current decoder has made it into the current structure. This makes all
the cp0 and len0 snapshot variables obsolete, so remove them too.

Update a test case output: the missing closing quote now correctly means
that the snapshot end is inside the "data" field of a BSN_SHELL_COMMAND
vendor message, not after it.
2020-09-24 18:58:45 +01:00
Denis Ovsienko
e9204baafd Loopback/CTP: Fixup loopback_print() declaration. [skip ci] 2020-09-19 00:54:41 +01:00
Denis Ovsienko
c208467f18 AHCP: Modernize packet parsing style.
Similarly to earlier commits, follow up from commit 19b51ded and make
changes along the same lines. Also make all functions void, lose a few
excess ND_TCHECK_LEN() instances, update comments, reduce scope for a
couple variables, rearrange some line breaks and call ND_TCHECK_LEN()
for unknown protocol versions.
2020-09-19 00:54:13 +01:00
Francois-Xavier Le Bail
a2a16f6b4c Use a switch to manage the setjmp() return values
Also rename 'ndo_truncated' to 'ndo_early_end'.

The current case (truncated packet) uses ND_TRUNCATED value.

Prepare to add other cases when the current packet cannot be processed
any more ('Invalid' cases, etc.).
2020-09-18 13:27:48 +02:00
Denis Ovsienko
5c2027db88 Rename min() and max() to ND_MIN() and ND_MAX(). [skip ci]
As discussed on tcpdump-workers, it was a place for a potential clash
with non-macros.
2020-08-24 21:53:58 +01:00
Denis Ovsienko
50a72922c5 Rx: Make UDP ports 16-bit to compile (GH #868).
Gisle Vanem reported that GCC 7.1 for DJGPP sees u_int and uint32_t as
two different types, hence the forward declaration for rx_cache_find()
was different from the actual function. UDP port numbers are 16-bit and
udp_print() appropriately uses uint16_t for them. Use the same type in
the downstream Rx-specific code to make things simpler and consistent.
2020-08-13 01:02:52 +01:00
Francois-Xavier Le Bail
b30f3843b9 Apply the last step of the new way to update the link-layer header length
All the link-layer dissectors are now void functions.

All the functions were moved to the void_printers[] array.
Rename this array to printers[].
Remove the uint_printers[] array, now empty.
Remove the 'ndo_void_printer' flag field, now useless, from
netdissect_options.
Remove other transitional code.
2020-08-06 22:55:05 +02:00
Francois-Xavier Le Bail
0c93b15d38 Rework "Update more link-layer dissectors to void functions"
It's a follow-up to commit 81dbf4a0b0.

There is no need to update ether_print(), ether_common_print() and
ether_switch_tag_print() to void functions: back to u_int functions.

There is also no need to add a flag parameter to ether_print(),
ether_common_print() and ether_switch_tag_print(): Remove it.
2020-08-06 22:36:16 +02:00
Francois-Xavier Le Bail
6855c11117 PFLOG: Update the link-layer dissector to a void function
Moreover:
Use GET_U_1() when needed.
Remove trailing "_if" from the protocol name.
2020-08-06 17:28:03 +02:00
Francois-Xavier Le Bail
b846e24423 AppleTalk: Update the link-layer dissector to a void function
Moreover:
Remove trailing "_if" from the protocol name.
2020-08-06 16:27:24 +02:00
Francois-Xavier Le Bail
5e9a51fdd5 CHDLC: Update the link-layer dissector to a void function
Moreover:
Remove trailing "_if" from the protocol name.
2020-08-06 16:27:24 +02:00
Francois-Xavier Le Bail
496c43c10b PPP: Update the link-layer dissectors to void functions
Moreover:
Remove trailing "_if" from some protocol names.
Update the outputs of two tests accordingly.
2020-08-06 16:25:52 +02:00
Francois-Xavier Le Bail
aa8bf0f527 Remove the unused DLT_LANE8023 link-layer dissector
DLT_LANE8023 was never defined in libpcap.
It was, perhaps, defined in some SuSE libpcap update, but no evidence
of this.
It is not defined in OpenSuSE Leap 15.2 (information from Guy).
2020-08-06 09:44:45 +02:00
Francois-Xavier Le Bail
4262b29164 FR: Update the link-layer dissectors to void functions
Moreover:
Remove trailing "_if" from the protocol name.
Add two comments.
2020-08-04 17:25:01 +02:00
Francois-Xavier Le Bail
b1327f7a88 ATM: Update the link-layer dissector to a void function
Moreover:
Remove trailing "_if" from the protocol name.
2020-08-02 22:11:51 +02:00
Francois-Xavier Le Bail
bdf7eacfa1 IPFC: Update the link-layer dissector to a void function
Moreover:
Remove trailing "_if" from the protocol name.
2020-08-02 22:11:51 +02:00
Francois-Xavier Le Bail
941452b132 Token Ring: Update the link-layer dissector to a void function
Moreover:
Remove trailing "_if" from the protocol name.
2020-08-02 22:11:51 +02:00