From a3ce4ba6230eaf0fceb9c67321bd1a3aa6d2e983 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Fri, 27 Dec 2019 08:00:00 -0800 Subject: v6: add IdentityOptions IANA and IATA options may contain Status Code and IAAddr options. Signed-off-by: Chris Koch --- dhcpv6/option_nontemporaryaddress.go | 57 +++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'dhcpv6/option_nontemporaryaddress.go') diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index 3ea46fe..01971f8 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -25,6 +25,45 @@ func (d *Duration) Unmarshal(buf *uio.Lexer) { d.Duration = time.Duration(t) * time.Second } +// IdentityOptions implement the options allowed for IA_NA and IA_TA messages. +// +// The allowed options are identified in RFC 3315 Appendix B. +type IdentityOptions struct { + Options +} + +// Address returns the addresses assigned to the identity. +func (io IdentityOptions) Address() []*OptIAAddress { + opts := io.Options.Get(OptionIAAddr) + var iaAddrs []*OptIAAddress + for _, o := range opts { + iaAddrs = append(iaAddrs, o.(*OptIAAddress)) + } + return iaAddrs +} + +// OneAddress returns one address (of potentially many) assigned to the identity. +func (io IdentityOptions) OneAddress() *OptIAAddress { + a := io.Address() + if len(a) == 0 { + return nil + } + return a[0] +} + +// Status returns the status code associated with this option. +func (io IdentityOptions) Status() *OptStatusCode { + opt := io.Options.GetOne(OptionStatusCode) + if opt == nil { + return nil + } + sc, ok := opt.(*OptStatusCode) + if !ok { + return nil + } + return sc +} + // OptIANA implements the identity association for non-temporary addresses // option. // @@ -34,7 +73,7 @@ type OptIANA struct { IaId [4]byte T1 time.Duration T2 time.Duration - Options Options + Options IdentityOptions } func (op *OptIANA) Code() OptionCode { @@ -58,22 +97,6 @@ func (op *OptIANA) String() string { op.IaId, op.T1, op.T2, op.Options) } -// AddOption adds an option at the end of the IA_NA options -func (op *OptIANA) AddOption(opt Option) { - op.Options.Add(opt) -} - -// GetOneOption will get an option of the give type from the Options field, if -// it is present. It will return `nil` otherwise -func (op *OptIANA) GetOneOption(code OptionCode) Option { - return op.Options.GetOne(code) -} - -// DelOption will remove all the options that match a Option code. -func (op *OptIANA) DelOption(code OptionCode) { - op.Options.Del(code) -} - // ParseOptIANA builds an OptIANA structure from a sequence of bytes. The // input data does not include option code and length bytes. func ParseOptIANA(data []byte) (*OptIANA, error) { -- cgit v1.2.3 From 9f507afe0e85d11469f610304e001eb825c1c61e Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Sat, 28 Dec 2019 01:35:02 -0800 Subject: netconf: use new IANA and IAAddress getters Signed-off-by: Chris Koch --- dhcpv6/option_nontemporaryaddress.go | 6 +++--- netboot/netconf.go | 13 ++----------- 2 files changed, 5 insertions(+), 14 deletions(-) (limited to 'dhcpv6/option_nontemporaryaddress.go') diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index 01971f8..0b6012e 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -32,8 +32,8 @@ type IdentityOptions struct { Options } -// Address returns the addresses assigned to the identity. -func (io IdentityOptions) Address() []*OptIAAddress { +// Addresses returns the addresses assigned to the identity. +func (io IdentityOptions) Addresses() []*OptIAAddress { opts := io.Options.Get(OptionIAAddr) var iaAddrs []*OptIAAddress for _, o := range opts { @@ -44,7 +44,7 @@ func (io IdentityOptions) Address() []*OptIAAddress { // OneAddress returns one address (of potentially many) assigned to the identity. func (io IdentityOptions) OneAddress() *OptIAAddress { - a := io.Address() + a := io.Addresses() if len(a) == 0 { return nil } diff --git a/netboot/netconf.go b/netboot/netconf.go index 78ddff8..60468a7 100644 --- a/netboot/netconf.go +++ b/netboot/netconf.go @@ -41,19 +41,11 @@ func GetNetConfFromPacketv6(d *dhcpv6.Message) (*NetConf, error) { } netconf := NetConf{} - // get IP configuration - iaaddrs := make([]*dhcpv6.OptIAAddress, 0) - for _, o := range iana.Options { - if o.Code() == dhcpv6.OptionIAAddr { - iaaddrs = append(iaaddrs, o.(*dhcpv6.OptIAAddress)) - } - } - netmask := net.IPMask(net.ParseIP("ffff:ffff:ffff:ffff::")) - for _, iaaddr := range iaaddrs { + for _, iaaddr := range iana.Options.Addresses() { netconf.Addresses = append(netconf.Addresses, AddrConf{ IPNet: net.IPNet{ IP: iaaddr.IPv6Addr, - Mask: netmask, + Mask: net.CIDRMask(64, 128), }, PreferredLifetime: iaaddr.PreferredLifetime, ValidLifetime: iaaddr.ValidLifetime, @@ -70,7 +62,6 @@ func GetNetConfFromPacketv6(d *dhcpv6.Message) (*NetConf, error) { if domains != nil { netconf.DNSSearchList = domains.Labels } - return &netconf, nil } -- cgit v1.2.3