Skip to content

Commit

Permalink
Implement disableDHCP functionality:
Browse files Browse the repository at this point in the history
If the Hardware object specifies that
DHCP is disabled we don't respond to DHCP
packets.

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Oct 23, 2024
1 parent f8b5d6d commit 632bc4c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/backend/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type dhcp struct {
LeaseTime int `yaml:"leaseTime"` // DHCP option 51.
Arch string `yaml:"arch"` // DHCP option 93.
DomainSearch []string `yaml:"domainSearch"` // DHCP option 119.
Disabled bool // If true, no DHCP response should be sent.
Netboot netboot `yaml:"netboot"`
}

Expand Down Expand Up @@ -305,6 +306,9 @@ func (w *Watcher) translate(r dhcp) (*data.DHCP, *data.Netboot, error) {
// domain search
d.DomainSearch = r.DomainSearch

// disabled
d.Disabled = r.Disabled

// allow machine to netboot
n.AllowNetboot = r.Netboot.AllowPXE

Expand Down
2 changes: 2 additions & 0 deletions internal/backend/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func (b *Backend) GetByIP(ctx context.Context, ip net.IP) (*data.DHCP, *data.Net

return nil, nil, err
}
d.Disabled = i.DisableDHCP

// Facility is used in the default HookOS iPXE script so we get it from the hardware metadata, if set.
facility := ""
if hardwareList.Items[0].Spec.Metadata != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/dhcp/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type DHCP struct {
LeaseTime uint32 // DHCP option 51.
Arch string // DHCP option 93.
DomainSearch []string // DHCP option 119.
Disabled bool // If true, no DHCP response should be sent.
}

// Netboot holds info used in netbooting a client.
Expand Down
12 changes: 12 additions & 0 deletions internal/dhcp/handler/reservation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func (h *Handler) Handle(ctx context.Context, conn *ipv4.PacketConn, p data.Pack

return
}
if d.Disabled {
log.Info("DHCP is disabled for this MAC address, no response sent")
span.SetStatus(codes.Ok, "disabled DHCP response")

return
}

Check warning on line 91 in internal/dhcp/handler/reservation/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/dhcp/handler/reservation/handler.go#L87-L91

Added lines #L87 - L91 were not covered by tests
log.Info("received DHCP packet", "type", p.Pkt.MessageType().String())
reply = h.updateMsg(ctx, p.Pkt, d, n, dhcpv4.MessageTypeOffer)
log = log.WithValues("type", dhcpv4.MessageTypeOffer.String())
Expand All @@ -98,6 +104,12 @@ func (h *Handler) Handle(ctx context.Context, conn *ipv4.PacketConn, p data.Pack

return
}
if d.Disabled {
log.Info("DHCP is disabled for this MAC address, no response sent")
span.SetStatus(codes.Ok, "disabled DHCP response")

return
}

Check warning on line 112 in internal/dhcp/handler/reservation/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/dhcp/handler/reservation/handler.go#L108-L112

Added lines #L108 - L112 were not covered by tests
log.Info("received DHCP packet", "type", p.Pkt.MessageType().String())
reply = h.updateMsg(ctx, p.Pkt, d, n, dhcpv4.MessageTypeAck)
log = log.WithValues("type", dhcpv4.MessageTypeAck.String())
Expand Down

0 comments on commit 632bc4c

Please sign in to comment.