-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package forwarder | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"gvisor.dev/gvisor/pkg/tcpip/header" | ||
"gvisor.dev/gvisor/pkg/tcpip/stack" | ||
) | ||
|
||
// handleICMP handles ICMP packets from the network stack | ||
func (f *Forwarder) handleICMP(id stack.TransportEndpointID, pkt stack.PacketBufferPtr) bool { | ||
ctx, cancel := context.WithTimeout(f.ctx, 5*time.Second) | ||
defer cancel() | ||
|
||
lc := net.ListenConfig{} | ||
// TODO: support non-root | ||
conn, err := lc.ListenPacket(ctx, "ip4:icmp", "0.0.0.0") | ||
if err != nil { | ||
f.logger.Error("Failed to create ICMP socket for %v: %v", id, err) | ||
return false | ||
} | ||
defer func() { | ||
if err := conn.Close(); err != nil { | ||
f.logger.Debug("Failed to close ICMP socket: %v", err) | ||
} | ||
}() | ||
|
||
dstIP := net.IP(id.LocalAddress.AsSlice()) | ||
dst := &net.IPAddr{IP: dstIP} | ||
|
||
// Get the complete ICMP message (header + data) | ||
fullPacket := stack.PayloadSince(pkt.TransportHeader()) | ||
payload := fullPacket.AsSlice() | ||
|
||
icmpHdr := header.ICMPv4(pkt.TransportHeader().View().AsSlice()) | ||
|
||
// For Echo Requests, send and handle response | ||
if icmpHdr.Type() == header.ICMPv4Echo { | ||
_, err = conn.WriteTo(payload, dst) | ||
if err != nil { | ||
f.logger.Error("Failed to write ICMP packet for %v: %v", id, err) | ||
return false | ||
} | ||
|
||
f.logger.Trace("Forwarded ICMP packet %v type=%v code=%v", | ||
id, icmpHdr.Type(), icmpHdr.Code()) | ||
|
||
return f.handleEchoResponse(conn, id) | ||
} | ||
|
||
// TODO: forward other ICMP types | ||
|
||
return true | ||
} | ||
|
||
func (f *Forwarder) handleEchoResponse(conn net.PacketConn, id stack.TransportEndpointID) bool { | ||
if err := conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil { | ||
f.logger.Error("Failed to set read deadline for ICMP response: %v", err) | ||
return false | ||
} | ||
|
||
response := make([]byte, f.endpoint.mtu) | ||
n, _, err := conn.ReadFrom(response) | ||
if err != nil { | ||
if !isTimeout(err) { | ||
f.logger.Error("Failed to read ICMP response: %v", err) | ||
} | ||
return false | ||
} | ||
|
||
ipHdr := make([]byte, header.IPv4MinimumSize) | ||
ip := header.IPv4(ipHdr) | ||
ip.Encode(&header.IPv4Fields{ | ||
TotalLength: uint16(header.IPv4MinimumSize + n), | ||
TTL: 64, | ||
Protocol: uint8(header.ICMPv4ProtocolNumber), | ||
SrcAddr: id.LocalAddress, | ||
DstAddr: id.RemoteAddress, | ||
}) | ||
ip.SetChecksum(^ip.CalculateChecksum()) | ||
|
||
fullPacket := make([]byte, 0, len(ipHdr)+n) | ||
fullPacket = append(fullPacket, ipHdr...) | ||
fullPacket = append(fullPacket, response[:n]...) | ||
|
||
if err := f.InjectIncomingPacket(fullPacket); err != nil { | ||
f.logger.Error("Failed to inject ICMP response: %v", err) | ||
return false | ||
} | ||
|
||
f.logger.Trace("Forwarded ICMP echo reply for %v", id) | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters