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 16c3026
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 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
58 changes: 26 additions & 32 deletions internal/backend/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,8 @@ func (b *Backend) GetByMac(ctx context.Context, mac net.HardwareAddr) (*data.DHC
}
}

d, err := toDHCPData(i.DHCP)
d, n, err := transform(i, hardwareList.Items[0].Spec.Metadata)
if err != nil {
err = fmt.Errorf("failed to convert hardware to DHCP data: %w", err)
span.SetStatus(codes.Error, err.Error())

return nil, nil, err
}
// 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 {
if hardwareList.Items[0].Spec.Metadata.Facility != nil {
facility = hardwareList.Items[0].Spec.Metadata.Facility.FacilityCode
}
}
n, err := toNetbootData(i.Netboot, facility)
if err != nil {
err = fmt.Errorf("failed to convert hardware to netboot data: %w", err)
span.SetStatus(codes.Error, err.Error())

return nil, nil, err
Expand Down Expand Up @@ -153,23 +138,8 @@ func (b *Backend) GetByIP(ctx context.Context, ip net.IP) (*data.DHCP, *data.Net
}
}

d, err := toDHCPData(i.DHCP)
if err != nil {
err = fmt.Errorf("failed to convert hardware to DHCP data: %w", err)
span.SetStatus(codes.Error, err.Error())

return nil, nil, err
}
// 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 {
if hardwareList.Items[0].Spec.Metadata.Facility != nil {
facility = hardwareList.Items[0].Spec.Metadata.Facility.FacilityCode
}
}
n, err := toNetbootData(i.Netboot, facility)
d, n, err := transform(i, hardwareList.Items[0].Spec.Metadata)
if err != nil {
err = fmt.Errorf("failed to convert hardware to netboot data: %w", err)
span.SetStatus(codes.Error, err.Error())

return nil, nil, err
Expand Down Expand Up @@ -302,3 +272,27 @@ func toNetbootData(i *v1alpha1.Netboot, facility string) (*data.Netboot, error)

return n, nil
}

// transform returns data.DHCP and data.Netboot from part a v1alpha1.Interface and *v1alpha1.HardwareMetadata.
func transform(i v1alpha1.Interface, m *v1alpha1.HardwareMetadata) (*data.DHCP, *data.Netboot, error) {
d, err := toDHCPData(i.DHCP)
if err != nil {
return nil, nil, fmt.Errorf("failed to convert hardware to DHCP data: %w", 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 m != nil {
if m.Facility != nil {
facility = m.Facility.FacilityCode
}
}

n, err := toNetbootData(i.Netboot, facility)
if err != nil {
return nil, nil, fmt.Errorf("failed to convert hardware to netboot data: %w", err)
}

return d, n, nil
}
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", "type", p.Pkt.MessageType().String())
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", "type", p.Pkt.MessageType().String())
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 16c3026

Please sign in to comment.