Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnrc: Use existing utility function to extract IPv6 header #10071

Conversation

chrysn
Copy link
Member

@chrysn chrysn commented Sep 28, 2018

Different checks were applied in the two places, and got unioned into the utility function.


Contribution description

This change to GNRC socks is mainly motivated by seeing that the different places (gnrc_sock_recv and the utility function gnrc_ipv6_get_header) used different checks when pulling a ipv6_hdr out of a pktsnip. The new version adds a check in public function (replacing a hard-coded constant with a sizeof).

I'm not sure all those checks are actually necessary, so it might be worth checking with the GNRC architectural decisions whether a GNRC_NETTYPE_IPV6 marked snip can actually have its data NULL and a size of < 40, but without that checked, I'd err on the side of caution.

Impact-wise, this might add a few bytes of text as gnrc_ipv6_get_header is so far not used in the rest of the code; if that's a concern, we might consider making it inline-able (how's -flto coming?).

Testing procedure

Tested on native with a custom application that has both direct netapi threads and a gcoap server running. I did not try to produce situations in which any of the additional checks are triggered (see above: I'm not sure they're all necessary)

@miri64 miri64 self-requested a review September 28, 2018 13:43
@miri64 miri64 self-assigned this Sep 28, 2018
@miri64 miri64 added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: network Area: Networking labels Sep 28, 2018
Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure all those checks are actually necessary, so it might be worth checking with the GNRC architectural decisions whether a GNRC_NETTYPE_IPV6 marked snip can actually have its data NULL and a size of < 40, but without that checked, I'd err on the side of caution.

It can. A "user" could just allocate a packet snip with that type and set data and type to whatever and then send it to the IPv6 thread via NETAPI_RCV. There are no restriction given to that.

Impact-wise, this might add a few bytes of text as gnrc_ipv6_get_header is so far not used in the rest of the code; if that's a concern, we might consider making it inline-able (how's -flto coming?).

It could also, as in the original check, go into an assert(). Since as I pointed already out that this check might only be caused by user error anyway, this might be the better way to go for most if not all of the checks in gnrc_ipv6_get_header().

@@ -197,7 +197,7 @@ ipv6_hdr_t *gnrc_ipv6_get_header(gnrc_pktsnip_t *pkt)
{
ipv6_hdr_t *hdr = NULL;
gnrc_pktsnip_t *tmp = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
if ((tmp != NULL) && (tmp->data != NULL) && ipv6_hdr_is(tmp->data)) {
if ((tmp != NULL) && (tmp->data != NULL) && ipv6_hdr_is(tmp->data) && tmp->size >= sizeof (ipv6_hdr_t)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the top post of this review: I think it is a better idea to put this into an assert(). That needs to be then documented as a pre-condition for this function of course and would be an API change. But this way we can safe some bytes and as far as I can tell this function is only used within this PR and the unittests (though you wrote something about GNRC sock [which for me is this I wasn't quite able to parse in your OP).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated; all but tmp != NULL is now in asserts. (I'll happily change it to assert that there's an IPv6 header, but that felt like a legitimate result as opposed to the other checks).

My GNRC sock referece was about the change in gnrc_sock.c -- that originally does not use the gnrc_ipv6_get_header function but duplicates its functionality in gnrc_sock_recv, this patch makes it the first in-code user of gnrc_ipv6_get_header.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My GNRC sock referece was about the change in gnrc_sock.c -- that originally does not use the gnrc_ipv6_get_header function but duplicates its functionality in gnrc_sock_recv, this patch makes it the first in-code user of gnrc_ipv6_get_header.

I somehow was convinced that this PR edited gnrc_ipv6.c only...

@@ -197,7 +197,7 @@ ipv6_hdr_t *gnrc_ipv6_get_header(gnrc_pktsnip_t *pkt)
{
ipv6_hdr_t *hdr = NULL;
gnrc_pktsnip_t *tmp = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
if ((tmp != NULL) && (tmp->data != NULL) && ipv6_hdr_is(tmp->data)) {
if ((tmp != NULL) && (tmp->data != NULL) && ipv6_hdr_is(tmp->data) && tmp->size >= sizeof (ipv6_hdr_t)) {
Copy link
Member

@miri64 miri64 Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(please also stick to coding conventions and put the expression into parenthesis and consider the line length)

@chrysn chrysn force-pushed the deduplicate-ipv6-address-extraction branch 2 times, most recently from f471d9e to 8efef36 Compare September 28, 2018 15:46
@chrysn
Copy link
Member Author

chrysn commented Sep 28, 2018

Updated again as I had forgotten to update the commit message accordingly.

I hope I got the sizeof code style right now; at least uncrustify does not complain.

Thanks for the review.

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final optional remark. Apart from this I'm fine with the state of this PR. However, I still need to test this.

sys/include/net/gnrc/ipv6.h Outdated Show resolved Hide resolved
gnrc_sock_recv used to duplicate functionality of gnrc_ipv6_get_header,
but additionally checked whether the IPv6 snip is large enough.

All checks are now included in gnrc_ipv6_get_header, but as most of them
stem from programming / user errors, they were moved into asserts; this
constitutes an API change.
@chrysn chrysn force-pushed the deduplicate-ipv6-address-extraction branch from 8efef36 to f07308b Compare September 28, 2018 17:41
@miri64 miri64 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 28, 2018
Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested by running tests/gnrc_sock_udp on native, iotlab-m3 and samr21-xpro. Works like a charm! :-)

@miri64 miri64 merged commit a6cf9db into RIOT-OS:master Sep 28, 2018
@miri64 miri64 added the Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. label Sep 28, 2018
@chrysn chrysn deleted the deduplicate-ipv6-address-extraction branch October 1, 2018 08:23
@jia200x jia200x added this to the Release 2018.10 milestone Oct 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: network Area: Networking CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants