summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/ztpv6/parse_vendor_options.go
blob: 13983f4246867db5177edd629091f98c44602c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ztpv6

import (
	"errors"
	"fmt"
	"strconv"
	"strings"

	"github.com/insomniacslk/dhcp/dhcpv6"
	"github.com/insomniacslk/dhcp/iana"
)

var (
	errVendorOptionMalformed = errors.New("malformed vendor option")
)

// VendorData contains fields extracted from Option 17 data
type VendorData struct {
	VendorName, Model, Serial string
}

// ParseVendorData will try to parse dhcp6 Vendor Specific Information options
// ( 16 and 17) data looking for more specific vendor data (like model, serial
// number, etc). If the options are missing we will just return nil
func ParseVendorData(packet dhcpv6.DHCPv6) (*VendorData, error) {
	// check for both options 16 and 17 if both are present will use opt 17
	opt16 := packet.GetOneOption(dhcpv6.OptionVendorClass)
	opt17 := packet.GetOneOption(dhcpv6.OptionVendorOpts)
	if (opt16 == nil) && (opt17 == nil) {
		return nil, errors.New("no vendor options or vendor class found")
	}

	vd := VendorData{}
	vData := []string{}

	if opt17 != nil {
		vo := opt17.(*dhcpv6.OptVendorOpts).VendorOpts
		for _, opt := range vo {
			vData = append(vData, string(opt.(*dhcpv6.OptionGeneric).OptionData))
		}
	} else {
		data := opt16.(*dhcpv6.OptVendorClass).Data
		for _, d := range data {
			vData = append(vData, string(d))
		}
	}
	for _, d := range vData {
		switch {
		// Arista;DCS-0000;00.00;ZZZ00000000
		case strings.HasPrefix(d, "Arista;"):
			p := strings.Split(d, ";")
			if len(p) < 4 {
				return nil, errVendorOptionMalformed
			}

			vd.VendorName = p[0]
			vd.Model = p[1]
			vd.Serial = p[3]
			return &vd, nil

		// ZPESystems:NSC:000000000
		case strings.HasPrefix(d, "ZPESystems:"):
			p := strings.Split(d, ":")
			if len(p) < 3 {
				return nil, errVendorOptionMalformed
			}

			vd.VendorName = p[0]
			vd.Model = p[1]
			vd.Serial = p[2]
			return &vd, nil

		// For Ciena the class identifier (opt 60) is written in the following format:
		//    {vendor iana code}-{product}-{type}
		// For Ciena the iana code is 1271
		// The product type is a number that maps to a Ciena product
		// The type is used to identified different subtype of the product.
		// An example can be ‘1271-23422Z11-123’.
		case strings.HasPrefix(d, strconv.Itoa(int(iana.EnterpriseIDCienaCorporation))):
			v := strings.Split(d, "-")
			if len(v) < 3 {
				return nil, errVendorOptionMalformed
			}
			innerMessage, err := packet.GetInnerMessage()
			if err != nil {
				return nil, fmt.Errorf("Unable to get inner message: %v", err)
			}
			duid := innerMessage.Options.ClientID()
			vd.VendorName = iana.EnterpriseIDCienaCorporation.String()
			vd.Model = v[1] + "-" + v[2]
			vd.Serial = string(duid.EnterpriseIdentifier)
			return &vd, nil
		}
	}
	return nil, errors.New("failed to parse vendor option data")
}