Skip to content

Conversation

Natolumin
Copy link
Member

Creating, then destroying a raw socket for every packet is very heavy. We can open the socket and keep it when first opening the (udp) listening socket, so that we are able to reuse the raw socket for further packets

Instead of managing the packet socket with manual syscalls, we can use the https://github.com/mdlayher/raw library (which we already imported before anyway) which will give us portability for free.
This does however mean that we cannot support dynamically setting the interface differently for each packet (at the moment) as the library requires an interface when binding to the socket; so this requires binding to a specific interface at startup (rather than relying on out-of-band data when receiving packets to choose the outgoing interface).
For DHCP servers this is a very common behavior (and we even required it until not that long ago) so I'm happy to take that tradeoff until we can update the upstream library to support the other usecase if needed.

Finally, if we can't setup the raw socket, we can then fallback to sending over broadcast (which is allowed by the RFC), ensuring that the server can keep working without for example CAP_NET_RAW

@codecov
Copy link

codecov bot commented Apr 24, 2021

Codecov Report

Attention: Patch coverage is 2.58621% with 113 lines in your changes missing coverage. Please review.

Project coverage is 34.58%. Comparing base (2fc7b54) to head (d16ae90).

Files with missing lines Patch % Lines
server/sendEthernet_linux.go 0.00% 90 Missing ⚠️
server/handle.go 11.11% 16 Missing ⚠️
server/serve.go 12.50% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #129      +/-   ##
==========================================
- Coverage   35.06%   34.58%   -0.48%     
==========================================
  Files          32       32              
  Lines        2045     2073      +28     
==========================================
  Hits          717      717              
- Misses       1237     1265      +28     
  Partials       91       91              
Flag Coverage Δ
integtests 20.70% <2.58%> (-0.59%) ⬇️
unittests 28.92% <0.00%> (-0.41%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@insomniacslk
Copy link
Member

For DHCP servers this is a very common behavior (and we even required it until not that long ago) so I'm happy to take that tradeoff until we can update the upstream library to support the other usecase if needed.

I am OK with this behaviour

@Natolumin
Copy link
Member Author

This version should also cover #131 now

Creating, then destroying a raw socket for every packet is very heavy.
We can open the socket and keep it when first opening the listening
socket, so that we are able to reuse it

This however requires knowing and binding to an interface at socket
creation time (which is fairly standard for dhcpv4) due to a limitation
of the library we use

Signed-off-by: Anatole Denis <anatole@unverle.fr>
When deferring to the system routing, the ip address is automatically
selected by the kernel.
We were previously using the message's ServerIPAddr, but that is
incorrect as a source address since that is the address of the next
server, and not necessarily the address of the current server or an
address that is routable by the client

Signed-off-by: Anatole Denis <anatole@unverle.fr>
Signed-off-by: Anatole Denis <anatole@unverle.fr>
With the deprecation of mdhlayer/raw and its replacement with
mdhlayer/packet, we lost BSD support for AF_PACKET sockets
Since the server code degrades gracefully if tryOpenRawSock fails,
compile a stub to let the server compile and start outside linux

Signed-off-by: Anatole Denis <anatole@unverle.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants