2022-09-13 13:00:54 +00:00
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ICMPRouter sends ICMP messages and listens for their responses
|
|
|
|
type ICMPRouter interface {
|
|
|
|
// Serve starts listening for responses to the requests until context is done
|
|
|
|
Serve(ctx context.Context) error
|
2022-09-28 18:02:46 +00:00
|
|
|
// Request sends an ICMP message. Implementations should not modify pk after the function returns.
|
2022-09-13 13:00:54 +00:00
|
|
|
Request(pk *ICMP, responder FunnelUniPipe) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upstream of raw packets
|
|
|
|
type Upstream interface {
|
|
|
|
// ReceivePacket waits for the next raw packet from upstream
|
|
|
|
ReceivePacket(ctx context.Context) (RawPacket, error)
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:39:51 +00:00
|
|
|
// Router routes packets between Upstream and ICMPRouter. Currently it rejects all other type of ICMP packets
|
2022-09-13 13:00:54 +00:00
|
|
|
type Router struct {
|
|
|
|
upstream Upstream
|
|
|
|
returnPipe FunnelUniPipe
|
2022-09-20 10:39:51 +00:00
|
|
|
icmpRouter ICMPRouter
|
2022-09-13 13:00:54 +00:00
|
|
|
ipv4Src netip.Addr
|
|
|
|
ipv6Src netip.Addr
|
|
|
|
logger *zerolog.Logger
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:39:51 +00:00
|
|
|
// GlobalRouterConfig is the configuration shared by all instance of Router.
|
|
|
|
type GlobalRouterConfig struct {
|
|
|
|
ICMPRouter ICMPRouter
|
|
|
|
IPv4Src netip.Addr
|
|
|
|
IPv6Src netip.Addr
|
|
|
|
Zone string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouter(globalConfig *GlobalRouterConfig, upstream Upstream, returnPipe FunnelUniPipe, logger *zerolog.Logger) *Router {
|
2022-09-13 13:00:54 +00:00
|
|
|
return &Router{
|
|
|
|
upstream: upstream,
|
|
|
|
returnPipe: returnPipe,
|
2022-09-20 10:39:51 +00:00
|
|
|
icmpRouter: globalConfig.ICMPRouter,
|
|
|
|
ipv4Src: globalConfig.IPv4Src,
|
|
|
|
ipv6Src: globalConfig.IPv6Src,
|
2022-09-13 13:00:54 +00:00
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) Serve(ctx context.Context) error {
|
|
|
|
icmpDecoder := NewICMPDecoder()
|
|
|
|
encoder := NewEncoder()
|
|
|
|
for {
|
|
|
|
rawPacket, err := r.upstream.ReceivePacket(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
icmpPacket, err := icmpDecoder.Decode(rawPacket)
|
|
|
|
if err != nil {
|
|
|
|
r.logger.Err(err).Msg("Failed to decode ICMP packet from quic datagram")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if icmpPacket.TTL <= 1 {
|
|
|
|
if err := r.sendTTLExceedMsg(icmpPacket, rawPacket, encoder); err != nil {
|
|
|
|
r.logger.Err(err).Msg("Failed to return ICMP TTL exceed error")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
icmpPacket.TTL--
|
|
|
|
|
2022-09-20 10:39:51 +00:00
|
|
|
if err := r.icmpRouter.Request(icmpPacket, r.returnPipe); err != nil {
|
2022-09-13 13:00:54 +00:00
|
|
|
r.logger.Err(err).
|
|
|
|
Str("src", icmpPacket.Src.String()).
|
|
|
|
Str("dst", icmpPacket.Dst.String()).
|
|
|
|
Interface("type", icmpPacket.Type).
|
|
|
|
Msg("Failed to send ICMP packet")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) sendTTLExceedMsg(pk *ICMP, rawPacket RawPacket, encoder *Encoder) error {
|
|
|
|
var srcIP netip.Addr
|
|
|
|
if pk.Dst.Is4() {
|
|
|
|
srcIP = r.ipv4Src
|
|
|
|
} else {
|
|
|
|
srcIP = r.ipv6Src
|
|
|
|
}
|
|
|
|
ttlExceedPacket := NewICMPTTLExceedPacket(pk.IP, rawPacket, srcIP)
|
|
|
|
|
|
|
|
encodedTTLExceed, err := encoder.Encode(ttlExceedPacket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return r.returnPipe.SendPacket(pk.Src, encodedTTLExceed)
|
|
|
|
}
|