Skip to content

net: sockets: Add SOCK_RAW support for AF_INET/AF_INET6 sockets #88044

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

Merged
merged 7 commits into from
Apr 9, 2025

Conversation

rlubos
Copy link
Collaborator

@rlubos rlubos commented Apr 2, 2025

Introduce support for raw IP sockets.

Fixes #86827

@github-actions github-actions bot added area: Sockets Networking sockets area: Networking Release Notes To be mentioned in the release notes labels Apr 2, 2025
@rlubos rlubos force-pushed the net/af-inet-raw-socket branch 2 times, most recently from b4d4adb to 88fffdd Compare April 2, 2025 14:02
@jukkar
Copy link
Member

jukkar commented Apr 2, 2025

Just wondering should we have a page in network documentation or doxygen comments in the code that describes how the various options are working (I am referring to the table in the issue of the PR). We mostly follow Linux but there are some deviations so perhaps these should be mentioned somewhere.

@jukkar jukkar assigned jukkar and rlubos and unassigned kartben and fabiobaltieri Apr 2, 2025
@rlubos
Copy link
Collaborator Author

rlubos commented Apr 2, 2025

Just wondering should we have a page in network documentation or doxygen comments in the code that describes how the various options are working (I am referring to the table in the issue of the PR). We mostly follow Linux but there are some deviations so perhaps these should be mentioned somewhere.

That's a good idea, we have a dedicated documentation page for sockets, perhaps it'd make most sense to add a table in there summarizing what socket types we do support.

Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

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

Some minor comments.
I am thinking about the tests, how can we verify that we have tested all combinations? Should we have a table in the test describing what we are testing and have a dedicated test function for each "cell" in the table, WDYT?


raw_pkt = net_pkt_clone(pkt, CLONE_TIMEOUT);
if (!raw_pkt) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

Cursor will be incorrect now. Perhaps have here goto to the end of the function as the cursor is restored there.

@@ -757,6 +797,10 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,
if (proto != ETH_P_ALL && proto != IPPROTO_RAW) {
continue; /* wrong protocol */
}
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_INET_RAW) && raw_ip_pkt) {
if (conn->proto != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we do the check like this

if (conn->proto != IPPROTO_IP) {

to make it clear what we are checking against (value 0 is a bit vague).

@@ -793,6 +837,15 @@ enum net_verdict net_conn_input(struct net_pkt *pkt,

continue; /* packet was consumed */
}
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_INET_RAW) && raw_ip_pkt) {
Copy link
Member

Choose a reason for hiding this comment

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

Note that few lines above, the code is still checking IPPROTO_RAW for packet socket, we need to remove/change those checks from packet socket

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, do you want me to extend this PR with AF_PACKET/IPPROTO_RAW removal?

Copy link
Member

@jukkar jukkar Apr 3, 2025

Choose a reason for hiding this comment

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

I think we need to remove the IPPROTO_RAW from AF_PACKET, that was actually the point of the original issue.

@@ -664,6 +680,11 @@ ssize_t zsock_sendmsg_ctx(struct net_context *ctx, const struct msghdr *msg,
k_timepoint_t buf_timeout, end;
int status;

if (net_context_get_type(ctx) == SOCK_RAW) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we prevent sendmsg for raw sockets?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably no good reason now that I think of it, I can drop this restriction and add some tests for sendmsg()/recvmsg().

@@ -1479,6 +1503,11 @@ ssize_t zsock_recvmsg_ctx(struct net_context *ctx, struct msghdr *msg,
enum net_sock_type sock_type = net_context_get_type(ctx);
size_t i, max_len = 0;

if (net_context_get_type(ctx) == SOCK_RAW) {
Copy link
Member

Choose a reason for hiding this comment

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

Why prevent recvmsg for raw sockets?

@rlubos
Copy link
Collaborator Author

rlubos commented Apr 3, 2025

I am thinking about the tests, how can we verify that we have tested all combinations? Should we have a table in the test describing what we are testing and have a dedicated test function for each "cell" in the table, WDYT?

I can refer to the table from the issue, and extend the test suite a bit. We'll likely duplicate some tests, but that's probably not a big deal. And now I think we should also add tests verifying that all this still works if we enable AF_PACKET support.

@rlubos rlubos force-pushed the net/af-inet-raw-socket branch from 88fffdd to e96c469 Compare April 3, 2025 12:03
@rlubos
Copy link
Collaborator Author

rlubos commented Apr 3, 2025

I have addressed the initial feedback, will work on extending the testsuite even more.

@rlubos rlubos added the DNM This PR should not be merged (Do Not Merge) label Apr 3, 2025
@@ -2711,6 +2851,14 @@ int net_context_send(struct net_context *context,
addrlen = 0;
}

if (dst_check) {
if (!(context->flags & NET_CONTEXT_REMOTE_ADDR_SET) ||
!net_sin(&context->remote)->sin_port) {
Copy link
Member

Choose a reason for hiding this comment

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

Minor nit, could be written as
net_sin(&context->remote)->sin_port == 0
which is slightly easier to read

@rlubos rlubos force-pushed the net/af-inet-raw-socket branch from e96c469 to 6309400 Compare April 3, 2025 14:17
@rlubos
Copy link
Collaborator Author

rlubos commented Apr 3, 2025

In last push:

  • Dropped AF_PACKET/IPPROTO_RAW socket support.
  • Added table in sockets documentation with the summary of what is supported, let's see how it renders

@rlubos rlubos force-pushed the net/af-inet-raw-socket branch 2 times, most recently from 1479c42 to d617ec2 Compare April 4, 2025 09:18
@rlubos rlubos force-pushed the net/af-inet-raw-socket branch from d617ec2 to 8f7173a Compare April 4, 2025 09:45
Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

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

The documentation looks great. I think we need to mention in migration doc that the AF_PACKET will not accept IPPROTO_RAW as a protocol any more.

Register connection type along with family and protocol, so that it's
possible to differentiate between connection listening for raw IP
datagrams and TCP/UDP/other packets.

Signed-off-by: Robert Lubos <[email protected]>
@rlubos rlubos force-pushed the net/af-inet-raw-socket branch from 8f7173a to 2255341 Compare April 7, 2025 14:17
@rlubos rlubos removed the DNM This PR should not be merged (Do Not Merge) label Apr 7, 2025
@rlubos rlubos force-pushed the net/af-inet-raw-socket branch from 2255341 to d3f2108 Compare April 7, 2025 14:18
@rlubos
Copy link
Collaborator Author

rlubos commented Apr 7, 2025

Ok this should be more or less ready, changes since last week:

  • Refined the table in the socket documentation so that it renders better
  • Added migration guide entry about removed IPPROTO_RAW support from AF_PACKET
  • Added more test cases. I've also added summary in the test suite, based on table created by @jukkar, to make sure that all AF_INET/SOCK_RAW functionalities mentioned there are covered

@rlubos rlubos force-pushed the net/af-inet-raw-socket branch 2 times, most recently from b5f9065 to 615833b Compare April 7, 2025 14:52
@rlubos
Copy link
Collaborator Author

rlubos commented Apr 7, 2025

CI fixes

jukkar
jukkar previously approved these changes Apr 8, 2025
@jukkar
Copy link
Member

jukkar commented Apr 9, 2025

@pdgendt please take a look if you have time

@jukkar
Copy link
Member

jukkar commented Apr 9, 2025

cc: @go2sh please try if it solves your issues

pdgendt
pdgendt previously approved these changes Apr 9, 2025
Copy link
Collaborator

@pdgendt pdgendt left a comment

Choose a reason for hiding this comment

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

Some minor comments

rlubos added 6 commits April 9, 2025 11:08
Introduce changes in the networking stack which allow to create raw IP
sockets, so that applications can send and receive raw IP datagrams.

Signed-off-by: Robert Lubos <[email protected]>
Add test suite verifying AF_INET/AF_INET6 and SOCK_RAW sockets.

Signed-off-by: Robert Lubos <[email protected]>
Add bullet point about raw IP sockets support.

Signed-off-by: Robert Lubos <[email protected]>
Add a table in sockets documentation summarizing what socket types are
being supported.

Signed-off-by: Robert Lubos <[email protected]>
IPPROTO_RAW is not a valid protocol type for AF_PACKET sockets, which
should only use IEEE 802.3 protocol numbers. Therefore remove support
for this type of sockets.

As an alternative, users can use AF_PACKET/SOCK_DGRAM or
AF_INET(6)/SOCK_RAW, depending on the actual use case.

Signed-off-by: Robert Lubos <[email protected]>
Add migration guide entry about dropped IPPROTO_RAW support from
AF_PACKET sockets, with possible alternatives.

Signed-off-by: Robert Lubos <[email protected]>
@rlubos rlubos dismissed stale reviews from pdgendt and jukkar via 847c208 April 9, 2025 09:08
@rlubos rlubos force-pushed the net/af-inet-raw-socket branch from 615833b to 847c208 Compare April 9, 2025 09:08
@rlubos rlubos requested a review from jukkar April 9, 2025 09:10
@kartben kartben merged commit 52eb3e3 into zephyrproject-rtos:main Apr 9, 2025
25 checks passed
@rlubos rlubos deleted the net/af-inet-raw-socket branch April 24, 2025 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking area: Sockets Networking sockets Release Notes To be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IPPROTO_RAW wrongly used with AF_PACKET
5 participants