Add flag to configure MTU

This commit is contained in:
Viktor Liu
2025-07-23 21:53:31 +02:00
parent 91e7423989
commit b2dcad828c
42 changed files with 536 additions and 327 deletions

View File

@@ -36,6 +36,7 @@ type SharedSocket struct {
conn4 *socket.Conn
conn6 *socket.Conn
port int
mtu int
routerMux sync.RWMutex
router routing.Router
packetDemux chan rcvdPacket
@@ -56,12 +57,19 @@ var writeSerializerOptions = gopacket.SerializeOptions{
FixLengths: true,
}
// Maximum overhead for IP + UDP headers on raw socket
// IPv4: max 60 bytes (20 base + 40 options) + UDP 8 bytes = 68 bytes
// IPv6: 40 bytes + UDP 8 bytes = 48 bytes
// We use the maximum (68) for both IPv4 and IPv6
const maxIPUDPOverhead = 68
// Listen creates an IPv4 and IPv6 raw sockets, starts a reader and routing table routines
func Listen(port int, filter BPFFilter) (_ net.PacketConn, err error) {
func Listen(port int, filter BPFFilter, mtu int) (_ net.PacketConn, err error) {
ctx, cancel := context.WithCancel(context.Background())
rawSock := &SharedSocket{
ctx: ctx,
cancel: cancel,
mtu: mtu,
port: port,
packetDemux: make(chan rcvdPacket),
}
@@ -222,8 +230,10 @@ func (s *SharedSocket) Close() error {
// read start a read loop for a specific receiver and sends the packet to the packetDemux channel
func (s *SharedSocket) read(receiver receiver) {
// Buffer reuse is safe: packetDemux is unbuffered, so read() blocks until
// ReadFrom() synchronously processes the packet before next iteration
buf := make([]byte, s.mtu+maxIPUDPOverhead)
for {
buf := make([]byte, 1500)
n, addr, err := receiver(s.ctx, buf, 0)
select {
case <-s.ctx.Done():