Skip to content

Commit

Permalink
Avoid sending loopback info to non-loopback
Browse files Browse the repository at this point in the history
This prevents confusing a non-loopback interface with information
intended for some same host.
  • Loading branch information
edaniels committed May 2, 2023
1 parent 608f20b commit 17c664e
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,21 @@ func (c *Conn) sendQuestion(name string) {
return
}

c.writeToSocket(0, rawQuery)
c.writeToSocket(0, rawQuery, false)
}

func (c *Conn) writeToSocket(ifIndex int, b []byte) {
func (c *Conn) writeToSocket(ifIndex int, b []byte, onlyLooback bool) {
if ifIndex != 0 {
ifc, _ := net.InterfaceByIndex(ifIndex)
ifc, err := net.InterfaceByIndex(ifIndex)
if err != nil {
c.log.Warnf("Failed to get interface interface for %d: %v", ifIndex, err)
return
}
if onlyLooback && ifc.Flags&net.FlagLoopback == 0 {
// avoid accidentally tricking the destination that itself is the same as us
c.log.Warnf("Interface is not loopback %d", ifIndex)
return
}
if err := c.socket.SetMulticastInterface(ifc); err != nil {
c.log.Warnf("Failed to set multicast interface for %d: %v", ifIndex, err)
} else {
Expand All @@ -246,6 +255,10 @@ func (c *Conn) writeToSocket(ifIndex int, b []byte) {
return
}
for ifcIdx := range c.ifaces {
if onlyLooback && c.ifaces[ifcIdx].Flags&net.FlagLoopback == 0 {
// avoid accidentally tricking the destination that itself is the same as us
continue
}
if err := c.socket.SetMulticastInterface(&c.ifaces[ifcIdx]); err != nil {
c.log.Warnf("Failed to set multicast interface for %d: %v", c.ifaces[ifcIdx].Index, err)
} else {
Expand Down Expand Up @@ -289,7 +302,7 @@ func (c *Conn) sendAnswer(name string, ifIndex int, dst net.IP) {
return
}

c.writeToSocket(ifIndex, rawAnswer)
c.writeToSocket(ifIndex, rawAnswer, dst.IsLoopback())
}

func (c *Conn) start(inboundBufferSize int, config *Config) { //nolint gocognit
Expand Down

0 comments on commit 17c664e

Please sign in to comment.