From 8166b9a9db9b180c02622ebf3dcff01ff2b2e0a6 Mon Sep 17 00:00:00 2001 From: Andrea Barberio Date: Tue, 23 Apr 2019 23:09:44 +0100 Subject: [dhcpv6] Solicit messages derive default IAID from MAC address IAID must be set by the client. This patch generates the IAID from the MAC address of the interface. To do so, a new WithIAID modifier is added, the interface of NewSolicitWithCID now requires a hwaddr parameter, and NewAdvertiseFromSolicit copies the IA_NA option from the solicit if present. Signed-off-by: Andrea Barberio --- dhcpv6/dhcpv6message.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'dhcpv6/dhcpv6message.go') diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index 9237c1b..707a65d 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -42,8 +42,15 @@ func GetTime() uint32 { return uint32((now.Nanoseconds() / 1000000000) % 0xffffffff) } -// NewSolicitWithCID creates a new SOLICIT message with CID. -func NewSolicitWithCID(duid Duid, modifiers ...Modifier) (*Message, error) { +// NewSolicit creates a new SOLICIT message, using the given hardware address to +// derive the IAID in the IA_NA option. +func NewSolicit(hwaddr net.HardwareAddr, modifiers ...Modifier) (*Message, error) { + duid := Duid{ + Type: DUID_LLT, + HwType: iana.HWTypeEthernet, + Time: GetTime(), + LinkLayerAddr: hwaddr, + } m, err := NewMessage() if err != nil { return nil, err @@ -57,12 +64,13 @@ func NewSolicitWithCID(duid Duid, modifiers ...Modifier) (*Message, error) { }) m.AddOption(oro) m.AddOption(&OptElapsedTime{}) - // FIXME use real values for IA_NA - iaNa := &OptIANA{} - iaNa.IaId = [4]byte{0xfa, 0xce, 0xb0, 0x0c} - iaNa.T1 = 0xe10 - iaNa.T2 = 0x1518 - m.AddOption(iaNa) + if len(hwaddr) < 4 { + return nil, errors.New("short hardware addrss: less than 4 bytes") + } + l := len(hwaddr) + var iaid [4]byte + copy(iaid[:], hwaddr[l-4:l]) + modifiers = append([]Modifier{WithIAID(iaid)}, modifiers...) // Apply modifiers for _, mod := range modifiers { mod(m) @@ -70,18 +78,6 @@ func NewSolicitWithCID(duid Duid, modifiers ...Modifier) (*Message, error) { return m, nil } -// NewSolicit creates a new SOLICIT message with DUID-LLT, using the -// given network interface's hardware address and current time -func NewSolicit(ifaceHWAddr net.HardwareAddr, modifiers ...Modifier) (*Message, error) { - duid := Duid{ - Type: DUID_LLT, - HwType: iana.HWTypeEthernet, - Time: GetTime(), - LinkLayerAddr: ifaceHWAddr, - } - return NewSolicitWithCID(duid, modifiers...) -} - // NewAdvertiseFromSolicit creates a new ADVERTISE packet based on an SOLICIT packet. func NewAdvertiseFromSolicit(sol *Message, modifiers ...Modifier) (*Message, error) { if sol == nil { -- cgit v1.2.3