github.com/zaolin/u-root@v0.0.0-20200428085104-64aaafd46c6d/pkg/dhclient/dhcp6.go (about) 1 // Copyright 2017-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package dhclient 6 7 import ( 8 "fmt" 9 "net" 10 "net/url" 11 "os" 12 13 "github.com/insomniacslk/dhcp/dhcpv4" 14 "github.com/insomniacslk/dhcp/dhcpv6" 15 "github.com/vishvananda/netlink" 16 "golang.org/x/sys/unix" 17 ) 18 19 // Packet6 implements Packet for IPv6 DHCP. 20 type Packet6 struct { 21 p *dhcpv6.Message 22 iface netlink.Link 23 } 24 25 // NewPacket6 wraps a DHCPv6 packet with some convenience methods. 26 func NewPacket6(iface netlink.Link, p *dhcpv6.Message) *Packet6 { 27 return &Packet6{ 28 p: p, 29 iface: iface, 30 } 31 } 32 33 // Message returns the unwrapped DHCPv6 packet. 34 func (p *Packet6) Message() (*dhcpv4.DHCPv4, *dhcpv6.Message) { 35 return nil, p.p 36 } 37 38 // Link returns the interface this packet was received for. 39 func (p *Packet6) Link() netlink.Link { 40 return p.iface 41 } 42 43 // Configure6 adds IPv6 addresses, routes, and DNS servers to the system. 44 func Configure6(iface netlink.Link, packet *dhcpv6.Message) error { 45 p := NewPacket6(iface, packet) 46 return p.Configure() 47 } 48 49 // Configure configures interface using this packet. 50 func (p *Packet6) Configure() error { 51 l := p.Lease() 52 if l == nil { 53 return fmt.Errorf("no lease returned") 54 } 55 56 // Add the address to the iface. 57 dst := &netlink.Addr{ 58 IPNet: &net.IPNet{ 59 IP: l.IPv6Addr, 60 Mask: net.IPMask(net.ParseIP("ffff:ffff:ffff:ffff::")), 61 }, 62 PreferedLft: int(l.PreferredLifetime), 63 ValidLft: int(l.ValidLifetime), 64 // Optimistic DAD (Duplicate Address Detection) means we can 65 // use the address before DAD is complete. The DHCP server's 66 // job was to give us a unique IP so there is little risk of a 67 // collision. 68 Flags: unix.IFA_F_OPTIMISTIC, 69 } 70 if err := netlink.AddrReplace(p.iface, dst); err != nil { 71 if os.IsExist(err) { 72 return fmt.Errorf("add/replace %s to %v: %v", dst, p.iface, err) 73 } 74 } 75 76 if ips := p.DNS(); ips != nil { 77 if err := WriteDNSSettings(ips, nil, ""); err != nil { 78 return err 79 } 80 } 81 return nil 82 } 83 84 func (p *Packet6) String() string { 85 return fmt.Sprintf("IPv6 DHCP Lease IP %s", p.Lease().IPv6Addr) 86 } 87 88 // Lease returns lease information assigned. 89 func (p *Packet6) Lease() *dhcpv6.OptIAAddress { 90 iana := p.p.Options.OneIANA() 91 if iana == nil { 92 return nil 93 } 94 95 iaAddrOpt := iana.Options.GetOne(dhcpv6.OptionIAAddr) 96 iaAddr, ok := iaAddrOpt.(*dhcpv6.OptIAAddress) 97 if !ok { 98 return nil 99 } 100 return iaAddr 101 } 102 103 // DNS returns DNS servers assigned. 104 func (p *Packet6) DNS() []net.IP { 105 return p.p.Options.DNS() 106 } 107 108 // Boot returns the boot file URL and parameters assigned. 109 func (p *Packet6) Boot() (*url.URL, error) { 110 uri := p.p.Options.BootFileURL() 111 if len(uri) == 0 { 112 return nil, fmt.Errorf("packet does not contain boot file URL") 113 } 114 return url.Parse(uri) 115 } 116 117 // ISCSIBoot returns the target address and volume name to boot from if 118 // they were part of the DHCP message. 119 // 120 // Parses the DHCPv6 Boot File for iSCSI target and volume as specified by RFC 121 // 4173 and RFC 5970. 122 func (p *Packet6) ISCSIBoot() (*net.TCPAddr, string, error) { 123 uri := p.p.Options.BootFileURL() 124 if len(uri) == 0 { 125 return nil, "", fmt.Errorf("packet does not contain boot file URL") 126 } 127 return parseISCSIURI(uri) 128 }