Pārlūkot izejas kodu

plugins/server_id: Reject non-matching ServerIDs

From [RFC8415](https://tools.ietf.org/html/rfc8415#section-16.4):
```
Servers MUST discard any received Request message that meets any of
   the following conditions:
[...]
   -  the contents of the Server Identifier option do not match the
      server's DUID.
```

Closes #36

Signed-off-by: Anatole Denis <anatole@unverle.fr>
Anatole Denis 6 gadi atpakaļ
vecāks
revīzija
e23dd4d1f9
2 mainītis faili ar 41 papildinājumiem un 1 dzēšanām
  1. 2 1
      plugins/server_id/plugin.go
  2. 39 0
      plugins/server_id/plugin_test.go

+ 2 - 1
plugins/server_id/plugin.go

@@ -33,7 +33,8 @@ func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
 	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)
+			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)

+ 39 - 0
plugins/server_id/plugin_test.go

@@ -0,0 +1,39 @@
+package serverid
+
+import (
+	"testing"
+
+	"github.com/insomniacslk/dhcp/dhcpv6"
+)
+
+func makeTestDUID(uuid string) *dhcpv6.Duid {
+	return &dhcpv6.Duid{
+		Type: dhcpv6.DUID_UUID,
+		Uuid: []byte(uuid),
+	}
+}
+
+func TestRejectBadServerIDV6(t *testing.T) {
+	req, err := dhcpv6.NewMessage()
+	if err != nil {
+		t.Fatal(err)
+	}
+	V6ServerID = makeTestDUID("0000000000000000")
+
+	req.MessageType = dhcpv6.MessageTypeRebind
+	dhcpv6.WithClientID(*makeTestDUID("1000000000000000"))(req)
+	dhcpv6.WithServerID(*makeTestDUID("0000000000000001"))(req)
+
+	stub, err := dhcpv6.NewReplyFromMessage(req)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	resp, stop := Handler6(req, stub)
+	if resp != nil {
+		t.Error("server_id is sending a response message to a request with mismatched ServerID")
+	}
+	if !stop {
+		t.Error("server_id did not interrupt processing on a request with mismatched ServerID")
+	}
+}