plugins/server_id: Abort when ServerID is nil

For V6ServerID to be nil, either:
 * The handler is running before setupServer6 ran (should be impossible)
 * Some other code modified V6ServerID outside of this plugin
   which is probably worth aborting as well.
   Unexport V6ServerID (and v4ServerID) to prevent this second one

Signed-off-by: Anatole Denis <anatole@unverle.fr>
This commit is contained in:
Anatole Denis 2019-09-11 18:48:51 +02:00 committed by Anatole Denis
parent e23dd4d1f9
commit 4a73abd6b6
2 changed files with 17 additions and 16 deletions

View File

@ -19,31 +19,32 @@ func init() {
plugins.RegisterPlugin("server_id", setupServerID6, setupServerID4)
}
// V6ServerID is the DUID of the v6 server
// v6ServerID is the DUID of the v6 server
var (
V6ServerID *dhcpv6.Duid
V4ServerID net.IP
v6ServerID *dhcpv6.Duid
v4ServerID net.IP
)
// Handler6 handles DHCPv6 packets for the server_id plugin.
func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
if V6ServerID == nil {
if v6ServerID == nil {
log.Fatal("BUG: Plugin is running uninitialized!")
return resp, false
}
if opt := req.GetOneOption(dhcpv6.OptionServerID); opt != nil {
sid := opt.(*dhcpv6.OptServerId)
if !sid.Sid.Equal(*V6ServerID) {
log.Infof("requested server ID does not match this server's ID. Got %v, want %v", sid.Sid, *V6ServerID)
if !sid.Sid.Equal(*v6ServerID) {
log.Infof("requested server ID does not match this server's ID. Got %v, want %v", sid.Sid, *v6ServerID)
return nil, true
}
}
dhcpv6.WithServerID(*V6ServerID)(resp)
dhcpv6.WithServerID(*v6ServerID)(resp)
return resp, false
}
// Handler4 handles DHCPv4 packets for the server_id plugin.
func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
if V4ServerID == nil || resp == nil {
if v4ServerID == nil || resp == nil {
return resp, false
}
if req.OpCode != dhcpv4.OpcodeBootRequest {
@ -52,14 +53,14 @@ func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
}
if req.ServerIPAddr != nil &&
!req.ServerIPAddr.Equal(net.IPv4zero) &&
!req.ServerIPAddr.Equal(V4ServerID) {
!req.ServerIPAddr.Equal(v4ServerID) {
// This request is not for us, drop it.
log.Infof("requested server ID does not match this server's ID. Got %v, want %v", req.ServerIPAddr, V4ServerID)
log.Infof("requested server ID does not match this server's ID. Got %v, want %v", req.ServerIPAddr, v4ServerID)
return nil, true
}
resp.ServerIPAddr = make(net.IP, net.IPv4len)
copy(resp.ServerIPAddr[:], V4ServerID)
resp.UpdateOption(dhcpv4.OptServerIdentifier(V4ServerID))
copy(resp.ServerIPAddr[:], v4ServerID)
resp.UpdateOption(dhcpv4.OptServerIdentifier(v4ServerID))
return resp, false
}
@ -75,7 +76,7 @@ func setupServerID4(args ...string) (handler.Handler4, error) {
if serverID.To4() == nil {
return nil, errors.New("not a valid IPv4 address")
}
V4ServerID = serverID
v4ServerID = serverID
return Handler4, nil
}
@ -99,14 +100,14 @@ func setupServerID6(args ...string) (handler.Handler6, error) {
}
switch duidType {
case "ll", "duid-ll", "duid_ll":
V6ServerID = &dhcpv6.Duid{
v6ServerID = &dhcpv6.Duid{
Type: dhcpv6.DUID_LL,
// sorry, only ethernet for now
HwType: iana.HWTypeEthernet,
LinkLayerAddr: hwaddr,
}
case "llt", "duid-llt", "duid_llt":
V6ServerID = &dhcpv6.Duid{
v6ServerID = &dhcpv6.Duid{
Type: dhcpv6.DUID_LLT,
// sorry, zero-time for now
Time: 0,

View File

@ -18,7 +18,7 @@ func TestRejectBadServerIDV6(t *testing.T) {
if err != nil {
t.Fatal(err)
}
V6ServerID = makeTestDUID("0000000000000000")
v6ServerID = makeTestDUID("0000000000000000")
req.MessageType = dhcpv6.MessageTypeRebind
dhcpv6.WithClientID(*makeTestDUID("1000000000000000"))(req)