Skip to content

Commit

Permalink
Reworked logic on the dhcp reply (#8297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdurand committed Sep 12, 2024
1 parent 0009032 commit 43cca14
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions go/cmd/pfdhcp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (d *Interfaces) readConfig() {
backend = ConfNet.PoolBackend
}
var dstReplyIp string

if ConfNet.DhcpReplyIp == "" {
dstReplyIp = "giaddr"
} else {
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/pfdhcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (I *Interface) ServeDHCP(ctx context.Context, p dhcp.Packet, msgType dhcp.M
if VIP[I.Name] || handler.available.Listen() {

defer recoverName(options)

answer.DstIP = handler.dstIp
var Options map[string]string
Options = make(map[string]string)
for option, value := range options {
Expand Down Expand Up @@ -556,7 +556,7 @@ func (I *Interface) ServeDHCP(ctx context.Context, p dhcp.Packet, msgType dhcp.M
}
GlobalOptions = options
leaseDuration := handler.leaseDuration
answer.DstIP = handler.dstIp

// Add network options on the fly
x, err := decodeOptions(NetScope.IP.String())
if err == nil {
Expand Down
32 changes: 24 additions & 8 deletions go/cmd/pfdhcp/workers_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

dhcp "github.com/inverse-inc/dhcp4"
"github.com/inverse-inc/go-utils/log"
)

type job struct {
Expand All @@ -23,22 +24,37 @@ func doWork(id int, element job) {
var ans Answer
if ans = element.handler.ServeDHCP(element.localCtx, element.DHCPpacket, element.msgType, element.clientAddr, element.srvAddr); ans.D != nil {
ipStr, portStr, _ := net.SplitHostPort(element.clientAddr.String())
if !(element.DHCPpacket.GIAddr().Equal(net.IPv4zero) && net.ParseIP(ipStr).Equal(net.IPv4zero)) {
ctx = log.AddToLogContext(ctx, "mac", ans.MAC.String())
log.LoggerWContext(ctx).Debug("Giaddr " + element.DHCPpacket.GIAddr().String())

// If giaddr is 0.0.0.0 and source ip is 0.0.0.0 (broadcast)
if element.DHCPpacket.GIAddr().Equal(net.IPv4zero) && net.ParseIP(ipStr).Equal(net.IPv4zero) {
log.LoggerWContext(ctx).Debug("Broadcast")
client, _ := NewRawClient(element.Int.intNet)
client.sendDHCP(ans.MAC, ans.D, ans.IP, element.Int.Ipv4)
client.Close()
} else {
// Non broadcast
dstPort, _ := strconv.Atoi(portStr)
if !(net.ParseIP(ipStr).Equal(element.DHCPpacket.GIAddr())) {
if !(element.DHCPpacket.GIAddr().Equal(net.IPv4zero)) {
// If the source ip is equal to the giaddr then send it to the source ip
if net.ParseIP(ipStr).Equal(element.DHCPpacket.GIAddr()) {
log.LoggerWContext(ctx).Debug("L3 coming from the dhcp relay " + element.DHCPpacket.GIAddr().String())
sendUnicastDHCP(ans.D, ans.SrcIP, net.ParseIP(ipStr), bootpServer, dstPort)
} else {
// Probably L2
if element.DHCPpacket.GIAddr().Equal(net.IPv4zero) {
log.LoggerWContext(ctx).Debug("L2 - no giaddr, send it to " + ipStr)
sendUnicastDHCP(ans.D, ans.SrcIP, net.ParseIP(ipStr), bootpServer, dstPort)
} else {
if ans.DstIP == "giaddr" {
log.LoggerWContext(ctx).Debug("L3 - reply to giaddr " + element.DHCPpacket.GIAddr().String())
sendUnicastDHCP(ans.D, ans.SrcIP, element.DHCPpacket.GIAddr(), bootpServer, dstPort)
} else {
log.LoggerWContext(ctx).Debug("L3 - sent to source IP" + ipStr)
sendUnicastDHCP(ans.D, ans.SrcIP, net.ParseIP(ipStr), bootpServer, dstPort)
}
}
}
sendUnicastDHCP(ans.D, ans.SrcIP, net.ParseIP(ipStr), bootpServer, dstPort)
} else {
client, _ := NewRawClient(element.Int.intNet)
client.sendDHCP(ans.MAC, ans.D, ans.IP, element.Int.Ipv4)
client.Close()
}
}
}

0 comments on commit 43cca14

Please sign in to comment.